- Home /
Need to rotate a 3rd person object to the camera rotation
I currently have a object which needs to rotate with the mouse and camera rotation, I am very new to Java scripting, here's the current code that I have set-up on my camera, it is a orbit camera from a tutorial.
var target : Transform;
var distance = 10.0;
var xSpeed = 250.0; var ySpeed = 120.0;
var yMinLimit = -20; var yMaxLimit = 80;
var zoomRate = 20;
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 () {
if (target) { x += Input.GetAxis("Mouse X") * xSpeed * 0.02; y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02; var test = 0; test = y; } distance += -(Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime) * zoomRate * Mathf.Abs(distance); if (distance < 2.5) { distance = 2.5; } if (distance > 20) { distance = 20; }
y = ClampAngle(y, yMinLimit, yMaxLimit);
//Debug.Log("y: "+y+" test: "+test);
if( y == yMinLimit && test == yMinLimit)
{
// This is to allow the camera to slide across the bottom if the player is too low in the y
distance += -(Input.GetAxis("Mouse Y") * Time.deltaTime) * 10 * Mathf.Abs(distance);
}
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, 2.0, -distance) + target.position;
//Debug.Log("Distance: "+distance);
transform.rotation = rotation;
transform.position = position;
}
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); }
I am looking for this particular camera and object rotation as seen in this game.
http://www.twiik.net/unity?guest=true
I really would like to learn how this can be done, my own preposition is that to get the camera to fallow the object x,y,z at a distance behind the object which is what this code does and make the object rotate with the mouse x,y rotation. I am new to coding in general so this makes it quite difficult to learn/understand Java with in 2 days. I would really appreciate it if you could explain a little about the solution as I'am here to learn, Any type of explanation on how to solve this issue will aid my progression as a learner.
Thanks for your time.
Answer by Yoerick · Nov 09, 2010 at 01:42 PM
You could use a combination of pre-made scripts that are included in the Unity package. For what I understand, you want to let the camera follow your object and rotate that object according to the mouse movement?
These are two scripts that are already written for you. Open your Unity project, go to "C:\program files\Unity\Editor\Standard packages" and double click the "Character Controller" and the "Scripts" unity packages. This will import the standard scripts into your current Unity project.
Now select the camera you want to follow the object. Go to Component -> Camera Control -> Smooth Follow. This will add the smooth follow script to your camera. Go to the script component and you should see a variable "Target" which is set to "none". Drag your object to this field. Now the script will make sure your camera allways looks at the object and also follows it. You can also adjust some variables like height and distance.
For the movement there's also a standard script called "FPS Input Controller". Select your object you want to move, go to Component -> Character -> FPS Input controller.
When you've added these two scripts you already have a basic Third Person controller.
However, writing your own Third Person Controller script is advised if you want to accomplish a specific effect. Here's a handy link: Third Person Controller. Especially page 4 is probably intereseting for you ;)
hope it works
Thank you, this is a great piece of writing to explain what I wanted your help is much appreciated.
Your answer
Follow this Question
Related Questions
Camera in fixed position which rotates with player. 0 Answers
Rotate an object to face mouse? 1 Answer
Making a ball move towards the mouse? 1 Answer
Turn with the mouse instead of with the camera 0 Answers
Wonky Camera Behaviours 0 Answers