- Home /
Help! I want to make a following camera but in with Y position is constant ?
i wanna make the camera follow my object with fixed distance between them ,but without changing the y position (Distance of y axis between object and camera ) . Knowing that the object moves right and left and jumps thanks a lot.
Answer by DiegoSLTS · Aug 13, 2016 at 07:09 PM
The most simple way I can think off is something like this...
public class Follow extends MonoBehaviour {
[SerializeField]
Transform target;
[SerializeField]
Vector3 offset;
void Update() {
Vector3 newPosition = new Vector3(target.positions.x + offset.x, offset.y, target.position.z + offset.z);
transform.position = newPosition;
}
}
Attach that script to the camera, drag and drop the object that should be followed as the target and set the offset to the fixed distance between the camera and the object.
thank u ^ ^
here is a slight change to adjust the position of the camera
using UnityEngine; using System.Collections;
public class CameraFollow : $$anonymous$$onoBehaviour {
public Transform thePlayer;
private Vector3 offset;
void Start() {
offset = transform.position - thePlayer.position;
}
void Update() {
Vector3 cameraNewPosition = new Vector3(thePlayer.position.x + offset.x, offset.y, thePlayer.position.z + offset.z);
transform.position = cameraNewPosition;
}
}
Answer by DevDavisC · Aug 13, 2016 at 07:30 PM
This should work fine
public class CameraFollow : MonoBehaviour {
private GameObject player;
//public float cameraSpeed = 5.0f;
// Use this for initialization
void Start () {
player = GameObject.FindGameObjectWithTag ("Player");
}
// Update is called once per frame
void FixedUpdate () {
//X position follow
Vector3 camPos = transform.position;
camPos.x = player.transform.position.x;
//camPos.z = player.transform.position.z; // only use if you want to follow from the player's z coordinate
transform.position = Vector3.Lerp (transform.position, camPos, 15 * Time.fixedDeltaTime);
}