简体   繁体   中英

Camera approaching and distancing loop in Unity3D

i can't understand how can i make it so that my camera will approach an object and after reaching a certain distance from it,it will go back to the initial position, and so on. Here is my code:

    public class Camera : MonoBehaviour
   {
       public GameObject cameraLook;
       float speed = 10f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 distance = (transform.position - cameraLook.transform.position);
        Debug.Log(distance);
        transform.LookAt(cameraLook.transform.position);
        if (distance.x > 30)
        {
            go();
        }
        if (distance.x < 0)
        {
            goBack();
        }
        
    }
    void go()
    {
        transform.position += transform.forward * speed * Time.deltaTime;
    }
    void goBack()
    {
        transform.position-= transform.forward * speed * Time.deltaTime;
    }
}

Update is called once every frame.

What currently happens in your code is

  • Frame 1
  • You reach a distance of x > 30
  • you call go once
  • Frame 2
  • Now your distance might be < 30 but still > 0 due to the movement last frame
  • You do nothing at all anymore

or the same with

  • Frame 1
  • You reach a distance of < 0
  • you call go once
  • Frame 2
  • Now your distance might be > 0 but still < 30 due to the movement last frame
  • You do nothing at all anymore

Then also it is very "dangerous" / unreliable to simply check the global X axis difference between your objects. This assumes that your camera is definitely moving along the X axis.


Instead I would rather use something like eg

public class Camera : MonoBehaviour
{
    public GameObject cameraLook;

    [Toolbox("The minimum distance the camera should stay away from the object when moving closer")]
    [Min(0f)] public float minDistance = 0f;
    [Toolbox("The maximum distance the camera should move away from the object")]
    [Min(0f)] public float maxDistance = 30f;

    [Toolbox("How fast shall the camera travel. If your target objects moves have in mind that this needs to be bigger then the movement speed of your target object ;)")]
    [Min(0f)] public float moveUnitsPerSecond = 10f;
   
    [Toolbox("If true starts moving towards otherwise starts moving away")]
    public bool startMovingTowards = true;

    private bool isMovingTowards;

    private void Start()
    {
        isMovingTowards = startMovingTowards;
    }     

    void Update()
    {
        // first look at the target object
        transform.LookAt(cameraLook.transform.position);

        // get the desired final distance based on the move direction flag
        var targetDistance = isMovingTowards ? minDistance : maxDistance;

        // get the desired final position based on the desired distance
        // simply moving the distance backwards away from the object
        var targetPosition = cameraLook.transform.position - transform.forward * targetDistance;
        
        // Move smoothly with the given moveUnitsPerSecond to the desired final position
        transform.position = Vector3.MoveTowards(transform.position, targetPosition, moveUnitsPerSecond * Time.deltaTime);

        // is the desired final position reached (within a precision of 0.00001)
        if(transform.position == targetPosition)
        {
            // then simply invert the move direction
            isMovingTowards = !isMovingTowards;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM