您的位置:首页 > 移动开发 > Unity3D

unity3d 抛物线

2015-11-04 13:52 429 查看
只做备忘,代码很乱

生成一个空物体,加上TrailRenderer, 挂上本脚本,运行,点按钮,可以看到抛物线

using UnityEngine;
using System.Collections;

public class MoveTest : MonoBehaviour {
public float angle = 80;
bool flag = false;
float ts;
// Use this for initialization
void Start () {
// StartCoroutine (init());
}

IEnumerator init(){
yield return new WaitForSeconds (1);
flag = true;
}
Vector3 pos = new Vector3(1,0,1).normalized;
public Vector3 dir;
// Update is called once per frame
void Update () {
if(!flag){
return;
}
Move (Time.deltaTime);
}

void Move(float time ){
// Vector3 dir = (new Vector3; (1, 0, 1).normalized);
// Vector3 tempX = 2*(new Vector3(1,0,1).normalized) * Time.deltaTime * Mathf.Cos (30*Mathf.Deg2Rad);
// Vector3 tempY = 2*dir * Time.deltaTime * Mathf.Sin (30*Mathf.Deg2Rad)-0.5f*(9.81f)*Time.deltaTime*Time.deltaTime*Vector3.forward;
float speed = 12;
// float angle = 80;
// //Vector3.Angle (pos.normalized, Vector3.right);
// Debug.LogWarning (angle);
ts += Time.deltaTime;
pos = new Vector3 (speed * ts * Mathf.Cos (angle*Mathf.Deg2Rad),
speed * ts * Mathf.Sin (angle *Mathf.Deg2Rad) - 0.5f * (9.81f * ts * ts)
,0)*time;
dir = dir.normalized;
pos = new Vector3 (dir.x*pos.x,pos.y,dir.z*pos.x);
//Debug.LogWarning (new Vector3(speed * Mathf.Cos(angle),0,(speed*Mathf.Sin(angle)-9.81f*ts)).magnitude);
//Debug.LogWarning (speed * Mathf.Cos(angle)+","+(speed*Mathf.Sin(angle)-9.81f*ts));
transform.position += pos;
//60* pos.normalized*Time.deltaTime;
//pos += Vector3.Cross (tempX, tempY) * 2;
//Debug.LogWarning (( 2*dir * Time.deltaTime * Mathf.Sin (30)).x+","+"dir="+dir.x+","+Mathf.Sin (30*Mathf.Deg2Rad));
//Debug.LogWarning (pos.x+","+pos.z);

}

void OnGUI(){
if(GUILayout.Button("ttt")){
flag = true;
ts = 0;
// StartCoroutine(MoveIE());
}
}

IEnumerator MoveIE(){
for (int i = 0; i < 500; i++) {
Move (0.01f);
yield return new WaitForSeconds(0.01f);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: