- Home /
Make Camera and Character Face Same Direction
So i have this code(i know its a lot). [code]
var character : Transform; var distance : float = 5.0; //How far away to orbit var xSpeed : float = 50.0; //X sensitivity var ySpeed : float = 50.0; //Y sensitivity
var resetTime : float = 1.0; //How long to take to reset
private var x : float = 0.0; //Y rotation private var y : float = 0.0; //X rotation
private var rotation : Quaternion; //Current orientation
private var resetting : boolean = false; //resetting? private var resetTimer : float = 0.0; //How long we've been resetting
var speed = 8;//8run 4 walk var rotatespeed = 4; var walkonoff : boolean = false; var cameraDistanceMax: float = 10; var cameraDistanceMin: float = 5; var scrollSpeed: float = 1; @script AddComponentMenu("Camera-Control/Mouse Orbit and reset") //Add to menu
function LateUpdate() { //Every frame, do this as late as you can
if (character) {//There's a character
if(!resetting && Input.GetMouseButtonUp(0)) { //Released mouse button
resetting = true;
endX = x;
endY = y;
resetTimer = 0.0; //Reset the reset timer
}
if(!resetting) {
if (Input.GetMouseButtonDown(0)) { //Pushed mouse button
//Initialize the angles
var angles = transform.eulerAngles;
x = angles.y;
startX = x;
y = angles.x;
startY = y;
} //first time the button down
if(Input.GetMouseButton(0)) { //Mouse button is down
//Change the angles by the mouse movement
x += Input.GetAxis("Mouse X") * xSpeed * distance* 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
Reorient();
} //button held down
} //Camera moving
else { //Reset
resetTimer += Time.deltaTime; //add to the timer;
var amountReset = resetTimer / resetTime; //How far we are
Reorient();
if(resetTimer >= resetTime) resetting = false;
} //Reset
character.forward = transform.forward;//make camera and character face forward
} //There's a character
} //LateUpdate
function Reorient() { //orient yourself //Rotate the camera to those angles rotation = Quaternion.Euler(y, x, 0); transform.rotation = rotation;
//Move the camera to look at the character
transform.position = rotation * Vector3(0.0, 0.0, -distance)
+ character.position;
}
function Update(){
//zoom
distance += Input.GetAxis("Mouse ScrollWheel") * scrollSpeed; cameraDistance = Mathf.Clamp(distance, cameraDistanceMin, cameraDistanceMax);
// set camera position
//movment controls
//wasd
if (Input.GetKey ("w"))
character.Translate(Vector3.forward * speed * Time.deltaTime);
Translate(Vector3.forward speed Time.deltaTime); if (Input.GetKey ("s")) character.Translate(Vector3.back speed Time.deltaTime); Translate(Vector3.back speed Time.deltaTime); if (Input.GetKey ("q")) character.Translate(Vector3.left speed Time.deltaTime); Translate(Vector3.left speed Time.deltaTime); if (Input.GetKey ("e")) character.Translate(Vector3.right speed Time.deltaTime); Translate(Vector3.forward speed Time.deltaTime); if (Input.GetKey ("a")) character.Rotate(0,-1 *rotatespeed,0); character.Rotate(0,-1 *rotatespeed,0); if (Input.GetKey ("d")) character.Rotate(0,1*rotatespeed,0); character.Rotate(0,1 *rotatespeed,0);
if(Input.GetKeyDown("/"))
if(walkonoff == true) walkonoff = false; else if (walkonoff == false) walkonoff = true;
if(walkonoff == true){ speed=4; rotatespeed = 2; animation.play("Walk"); }
if(walkonoff == false){ speed=8;rotatespeed = 4; animation.play("Run"); }
if (Input.GetKey("/")){
}
}
[/code] But the problem is if i rotate the camera with the mouse the character does not move wiht it. How could i fix that.
Answer by UniteMage · Apr 29, 2012 at 10:49 PM
Hello
There is an excellent tutorial on the third person character controller at 3DBuzz (http://www.3dbuzz.com/).
In the following method of the tutorial, I added a single line as indicated below:
Vector3 CalculatePosition(float rotationX, float rotationY, float distance)
{
Vector3 direction = new Vector3(0, 0, -distance);
// Added line
rotationY = Character.transform.eulerAngles.y + rotationY;
Quaternion rotation = Quaternion.Euler(rotationX, rotationY, 0);
return TargetLookAt.position + rotation * direction;
}
Where ever the characters faces, the camera looks in the same direction....
I recommend studying the tutorial at 3DBuzz....
I hope this helps.
CSDG
So how would i implements just that into my script? Because i dont use rotation Y.
Your answer
Follow this Question
Related Questions
How to change gravity direction on Character Motor 0 Answers
Character Collision Issue 1 Answer
Bug with my enemyAI 2 Answers
Gyroscope problem (nexus S, Android: 4.0) 1 Answer
My character jumps occationally when against a wall? 2 Answers