- Home /
Object changing the angle of trajectory 2D,changing trajectory of an moving object 2D
So basically the object is traveling on Y axis and with the press of UpAarrow key, the trajectory will change for 45 degrees but if you press DownArrow the object will continue it's path on the value of X that's it's got to. It's 3am and I'm tired so I figured to just draw it. Also the same would go if the DOWN is pressed first, just flip the picture.
Answer by Shameness · Jun 30, 2018 at 05:35 PM
Use left & right arrow to change course of direction. Speed multiplied by Time delta so its smooth. x and y values are calculated with current z rotation.
float speed = 3f;
float x;
float y;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
x = Mathf.Cos(transform.eulerAngles.z * Mathf.Deg2Rad);
y = Mathf.Sin(transform.eulerAngles.z * Mathf.Deg2Rad);
transform.position += new Vector3(x, y, 0) * speed * Time.deltaTime;
if (Input.GetKeyDown(KeyCode.LeftArrow)){
transform.Rotate(new Vector3(0, 0, 45));
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
transform.Rotate(new Vector3(0, 0, -45));
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612165743im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Trajectory predetermining 1 Answer
2D Trajectory prediction in open space 1 Answer
How to check if a trajectory is blocked? 1 Answer
Calculating the Trajectory of Planetary Bodies 1 Answer
Drawing trajectory/path in game view 1 Answer