- Home /
How do i on the y axis my camera from moving?
I a making 3D game where i use WASD to rotate the camera and arrow keys to move. i used this script for movement.
var speed : float = 5; //Controls the movement speed of the camera var x : float; var z : float;
function Update () {
x = Input.GetAxis("MoveX") speed Time.deltaTime; //calculates the cameras side to side movement
z = Input.GetAxis("MoveY") speed Time.deltaTime; //calculates the cameras forward back movements
transform.Translate(x, 0, z); //moves the camera according to the values calculated previously
}
It works, but when you are looking upwards and move, it moves with the camera upward, instead of straight forward. If it is relevant here is the camera look script also
var yMaxLimit = 80;
private var x = 0.0; private var y = 0.0;
@script AddComponentMenu("Camera-Control/Mouse Orbit")
function Start () { var angles = transform.eulerAngles; x = angles.y; y = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function LateUpdate () {
x += Input.GetAxis("LookX") * xSpeed * 0.02;
y -= Input.GetAxis("LookY") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
transform.rotation = rotation;
}
static function ClampAngle (angle : float, min : float, max : float) { if (angle < -360) angle += 360; if (angle > 360) angle -= 360; return Mathf.Clamp (angle, min, max);
}
Your answer
Follow this Question
Related Questions
Camera Scripting Help (Sims Style) 0 Answers
Moving Question 1 Answer
Fixed camera on rails. 2 Answers
Making my look script turn smoothly 0 Answers