- Home /
Change angle of camera: eulerAngle issue
Hi,
in the pre-written script made by Unity, to make the camera follow an object, i would like the angle to not point towards "0" (eulerAngles.y here) but a bit more up, so that the camera is not pointing to the feet of my character.
But i can't find out how to modify this, when i directly change the eulerAngle to a number, it rotates itself around the character from left to right instead of up/down.
Would you know how i could achieve this? (make the camera look a bit up, towards the trunk of the character for example)
Here is the code of the script attached to the camera (ball is the target) :
// Calculate the current rotation angles
Debug.Log("ball.eulerAngles.y : "+ball.eulerAngles.y);
var wantedRotationAngle = ball.eulerAngles.y;/* HERE, if i change the angle, it goes left/right instead of up/down) */
var wantedHeight = ball.position.y + height;
var currentRotationAngle = transform.eulerAngles.y;
var currentHeight = transform.position.y;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// 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 ball
transform.position = ball.position;
transform.position -= currentRotation * Vector3.forward * distance;
// Set the height of the camera
transform.position.y = currentHeight;
// Always look at the ball
transform.LookAt (ball);
}
Thanks
You could simply add an empty game object to your guy and make the camera look at it. This way it is even easier to make go up/down or left/right simply modifying the transform of that object. The camera will follow blindly.
@fafase thanks fafase, yes actually you're right, i don't know why it works with an empty, the lookAt points to the correct direction, and not if i let the entire character as the target (in this case it looks at his feet) anyway, it works now, thanks ;-)
The script looks at `transform.position`, which is a single point. There's no hidden step where it figures the center of the guy. Characters are usually created with position at the feet, so you can place them on the ground. Standard modelling stuff. Try placing a cube at 0,0,0. You'll see that position is in the center, for them.
Empties work since they have a position, which is all the script cares about.
Answer by aldonaletto · Dec 04, 2012 at 03:16 PM
The camera always look at the target pivot, as @OwenReinolds said. A possible solution (as @fafase said) is to child an empty object to the character, adjust its position to where the camera should aim at, and assign it to ball - this way the camera will follow the empty object, not the character.
But there's another solution that would avoid changes in the character: instead of LookAt(ball) use the LookAt(Vector3) variant and define a position a little above the ball:
var offset: float = 0.5; // vertical distance above ball
...
// Always look at the ball
var pos = ball.position; // get the ball position...
pos.y += offset; // shift it vertically...
transform.LookAt (pos); // and turn the camera to the calculated position
}
Your answer
Follow this Question
Related Questions
Using lerp to go between 3 different locations isn't working 0 Answers
Clamping cameras with different rotation 0 Answers
How to make camera move like it does in the scene viewport? 0 Answers
camera follow obect on pan to z axis 0 Answers
Move camera when mouse is near the edges of the screen 1 Answer