Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How to move game object forward at angle?

Discussion in 'Scripting' started by Killercarrot102, Sep 4, 2023.

  1. Killercarrot102

    Killercarrot102

    Joined:
    Jun 20, 2022
    Posts:
    10
    I have a bullet that turns toward the player and i need it to move forward at the angle it is facing. So basically i am making a heat seeking bullet. Also i have this code for turning the bullet towards the player and it isnt working right:
    Code (CSharp):
    1. var dir = player.position - Camera.main.WorldToScreenPoint(transform.position);
    2.             var angle = MathF.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
    3.             transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
    4.  
    idk how the math works is that code.
     
  2. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    if the bullet turns toward the target, and only moves forward, it will move towards the target.
     
  3. Killercarrot102

    Killercarrot102

    Joined:
    Jun 20, 2022
    Posts:
    10
    idk how to move forward
     
  4. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    I'm not sure if your joking.. but if not, I'd look into beginner tutorials as they cover something as simple as move forward.

    Easiest way I know off hand is just translate:
    transform.Translate(0, 0, moveSpeed);