- Home /
Restrict Camera to move X Axis only
Hey guys, I know I should know this answer by now. But I'm not used to this Javascript stuff with the Smooth Follow script.
I've got it now to look at the angle I want, I just can't figure out how to restrict the Camera to move only left and right, I don't want it to move up and down or any other direction, just left and right, it's a 2.5D game, but I have it set to where the player can walk in all 4 directions, so I just need the Camera to move left and right.
Just to make things easier I'll go ahead and post the Camera code here so you you have to go searching for it.
// The target we are following
var target : Transform;
// The distance in the x-z plane to the target
var distance = 10.0;
// the height we want the camera to be above the target
var height = 5.0;
// How much we
var heightDamping = 2.0;
var rotationDamping = 3.0;
// Place the script in the Camera-Control group in the component menu
@script AddComponentMenu("Camera-Control/Smooth Follow")
function LateUpdate () {
// Early out if we don't have a target
if (!target)
return;
// Calculate the current rotation angles
var wantedRotationAngle = target.eulerAngles.y;
var wantedHeight = target.position.y + height;
var currentRotationAngle = transform.eulerAngles.y;
var currentHeight = transform.position.y;
// Damp the height
currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
// Set the height of the camera
transform.position.y = currentHeight;
// Always look at the target
transform.LookAt (target);
}
Answer by Avash · Dec 17, 2014 at 05:59 PM
The easiest way is probaly save old position before camera position is updated. Calculated x old z and y are the coordinates you want.
EDIT:
Javascript is not really that differend. If you can do it in c# you should be able to make it in javascript.
// Other variables
var lastPosition : Vector3;
function LateUpdate () {
lastPosition = transform.position;
// Your current code in LateUpdate()
var newPosition = transform.position;
var fixedPosition = Vector3(newPosition.x, lastPosition.y, lastPosition.z);
}
Well if you know anything about Javascript that would be greatly appreciated (I use C#) and know nothing about the Syntax for JS. But if you can't I understand.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
how to operate 2 cameras simultaneously 1 Answer
TPS Camera keeps moving when I don't move my finger! 0 Answers
Change Camera 3 Answers
How to rotate the camera smoothly for just 1 second? 2 Answers