There is a tower game object and a watcher trigger. Upon entering the watcher game target object, the tower turns toward the object that entered the trigger.
Now I use trasform.lookAt and everything suits me, but the question is, if we say I want to add a tower to the rotationSpeed that directly affects the speed of rotation, which method or algorithm should I use?

Now the code is:

 public class Watching : MonoBehaviour { public List<GameObject> targets; /* список объектов, которые вошли в триггер */ public GameObject controledObject; /* tower */ void OnTriggerEnter(Collider target) { targets.Add(target.gameObject); } void OnTriggerExit(Collider target) { targets.Remove(target.gameObject); } void Update () { if(this.targets.Count > 0) { GameObject target = this.targets[0]; controledObject.transform.LookAt(target.transform); } } } 
  • Explain why this. here this. , you are welcome. - CGLike
  • this refers to the current instance of the class and gives access to the targets field - ThisMan
  • But, just like in OnTriggerEnter(Exit) the scope allows you to get targets without this. - CGLike
  • one
    @CGLike, yes, there are just differences in writing the style (either this is everywhere, or you can do without it) - ThisMan

2 answers 2

C using LookAt unlikely to succeed. Quaternion can be used

in the Update method it will be like this:

 void Update () { if(this.targets.Count > 0) { GameObject target = this.targets[0]; var rotation = Quaternion.LookRotation(target.position - controledObject.transform.position); controledObject.transform.rotation = Quaternion.Slerp(controledObject.transform.rotation, rotation, Time.deltaTime * damping); } } 

where damping is a variable with which you can control the speed

    Methods for turning an object (there are a lot of them): transform.LookAt(); transform.Rotate(); transform.RotateAround(); Quaternion.LookRotation(); Also help: Mathf.Slerp();