- Home /
Camera Follow
Hello Unity3D i have a question about camera following.How can i make my character camera follow on the opponent except for the x-axis?For example when my characters camera follow the opponent flying into the air but it lose some of the visual view of my character.I want to camera to follow but not on the x axis.IF anyone knows how i can do this?Can you please tell me how?
P.S Heres what i got so far
var target : Transform;
var rotationSpeed = 5;
var myTransform : Transform;
function Awake() {
myTransform = transform;
}
function Start() {
if (GetComponent.<Rigidbody>())
GetComponent.<Rigidbody>().freezeRotation = true;
target = GameObject.FindWithTag("Dummy").transform;
// Make the rigid body not change rotation
}
function LateUpdate () {
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
}
Answer by Xarbrough · Jun 14, 2015 at 11:18 PM
float damping = 3f;
void LateUpdate()
{
// if target is not null
if (target)
{
//Set targetPosition y and z, but keep the x value at the current position
targetPosition = new Vector3 (transform.position.x, target.position.y, target.position.z);
transform.position = Vector3.SmoothDamp (transform.position, targetPosition, ref velocity, damping);
}
}
Try this. Just define a target position, which has the same x component as your current position. Then either Lerp (or SmoothDamp, which is similar) towards the target position, or do target.position - transform.position to get the vector in which you want to move. Then you could translate or addforce in the direction. Depends on how your movement should work.
what im trying to do is that i want the camera or player to not move on the x axis whenever i jump or send the opponent flying
Your answer
