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);
}
}
}
生成一个空物体,加上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);
}
}
}
相关文章推荐
- Unity3d使用高通Vuforia发布IOS工程遇见缺少插件问题解决
- Unity给力插件之Final IK
- Unity读取Excel数据
- Unity 3d性能优化技巧
- 【笨木头Unity】入门之旅005:正常人都要掌握的操作
- 【笨木头Unity】入门之旅004:地形初初初级介绍
- 【笨木头Unity】入门之旅003:HelloWorld
- 【笨木头Unity】入门之旅002:不谈对象,咱们谈组件
- 【笨木头Unity】入门之旅001:学游泳的第一步是下水
- Unity之iTween插件初探Move移动
- 项目记录01--客unity,服erlang--框架net
- Shader-颜色
- Unity3D研究院之Time.timeScale、游戏暂停(七十四)
- 使用Unity3D的50个技巧:Unity3D最佳实践
- git_community_book_notes:ch3_junior_usage
- git_community_book:ch2_basic_usage
- git_community_book_notes:Introduction
- Unity3D 相机跟随主角移动
- Unity3D Editor模式下批量修改prefab
- Unity3D 第三人称的游戏角色的单选和移动