- Home /
 
How to make the camera look ahead of the player when he is moving
I found a nice working camera script but it doesn't look ahead when the player is moving forward. I want it so that when I'm moving to the right it is like one or 2 units ahead so I can see where I'm going better. When I'm moving left I want it to do the same in the other direction. How would I achieve this? Here is the camera script:
 public GameObject cameraTarget; // object to look at or follow
     public GameObject player; // player object for moving
     public float smoothTime = 0.1f; // time for dampen
     public bool cameraFollowX = true; // camera follows on horizontal
     public bool cameraFollowY = true; // camera follows on vertical
     public bool cameraFollowHeight = true; // camera follow CameraTarget object height
     public float cameraHeight = 2.5f; // height of camera adjustable
     public Vector2 velocity; // speed of camera movement
     private Transform thisTransform; // camera Transform
     // Use this for initialization
     void Start()
     {
         thisTransform = transform;
     }
     // Update is called once per frame
     void FixedUpdate()
     {
         if (cameraFollowX)
         {
             thisTransform.position = new Vector3(Mathf.SmoothDamp(thisTransform.position.x, cameraTarget.transform.position.x, ref velocity.x, smoothTime), thisTransform.position.y, thisTransform.position.z);
         }
         if (cameraFollowY)
         {
             // to do
         }
         if (!cameraFollowX & cameraFollowHeight)
         {
             // to do
         }
     }
 
              Answer by Existingman · Jan 23, 2015 at 10:54 PM
so:
 if (cameraFollowX)
          {
 float offset = velocity.x * someConstant;
              thisTransform.position = new Vector3(Mathf.SmoothDamp(thisTransform.position.x + offset, cameraTarget.transform.position.x, ref velocity.x, smoothTime), thisTransform.position.y, thisTransform.position.z);
 
               in other words, multiply the velocity by some adjusting factor that you can experiment with, and then add the result to the x position that represents the players position in the script that you found. That way the camera will position itself as if focusing ahead of the player. If the player accellerates gently, then the camera will gradually move further ahead.
Answer by Vickylance · Mar 18, 2016 at 04:15 AM
Here is a script that I have modified from another script in the forums. using UnityEngine; using System.Collections;
 public class Camera2DFollow : MonoBehaviour
 {
     public float dampTime = 0.15f;
     public Transform target;
     
     private Vector3 velocity = Vector3.zero;
 
     void Update()
     {
         if (target)
         {
             Vector3 aheadPoint = target.position + new Vector3(target.GetComponent<Rigidbody2D>().velocity.x, 0, 0);
             Vector3 point = Camera.main.WorldToViewportPoint(aheadPoint);
             Vector3 delta = aheadPoint - Camera.main.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, point.z));
             Vector3 destination = transform.position + delta;
             transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);
         }
     }
 }
 
              Your answer
 
             Follow this Question
Related Questions
How to do screenshake without influencing the transform of the camera? 2 Answers
how to modify this code to place it inside a car and move it along the car. 0 Answers
Headbob jerky movement 0 Answers
Best Setting Size Orthographic Camera? 1 Answer
Help with Texture following Camera!/How to create an effect like this in Unity 3d? 1 Answer