Z
Z
Zimaell2020-08-05 17:14:17
Unity
Zimaell, 2020-08-05 17:14:17

How to make a smooth turn to a point?

I make a movement on checkpoints

spoiler
public class Unit : MonoBehaviour{
    public Vector3 CheckPoint;
    public int NumCheckPoint = 0;
    public Dictionary<int, Vector3> CheckPointsMove = new Dictionary<int, Vector3>();
    private float speed = 2f;
    private GameObject head;

  void Start(){
    head = gameObject.transform.Find("Head").gameObject;
    CheckPointsMove[0] = new Vector3(2, 1, 2);
    CheckPointsMove[1] = new Vector3(10, 1, 2);
    CheckPointsMove[2] = new Vector3(10, 1, 13);
    }

    void Update(){
        MoveEnemy();
        }

  void MoveEnemy(){
    head.transform.LookAt(new Vector3(CheckPoint.x, 0, CheckPoint.z));

    if(CheckPoint==transform.position){
      ++NumCheckPoint;
      foreach(KeyValuePair<int, Vector3> v in CheckPointsMove){
        if(v.Key == NumCheckPoint){
          CheckPoint = v.Value;
          }
        }
      }
    transform.position = Vector3.MoveTowards(transform.position, CheckPoint, speed*Time.deltaTime);
    }

for the test at the start, points are set along which it must move, as well as its tower with a muzzle, which must spin when turning (changing points).
Firstly, when approaching the point, it smoothly lowers the barrel of something (that is, looks at the point on the floor with the barrel), although the Y coordinates are 0, and secondly, when turning, it sharply turns towards the point.
Tell me to make a smooth turn of the tower to the point, and why does he lower the muzzle (looks at the point along Y)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Strokin, 2020-08-14
@Zimaell

Why is he looking at the floor?

head.transform.LookAt(new Vector3(CheckPoint.x, 0, CheckPoint.z)

0 means that it will look at a point at floor level, this is not a rotation angle, but the global coordinate of the point to look at. Do it in this way:
head.transform.LookAt(new Vector3(CheckPoint.x, head.transform.Position.y, CheckPoint.z)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question