- Home /
Player Rotation relitive to active camera
Hello, I'm trying to make a silent hill/RE style game without take controls. I have it to were the player movement axis adjust based on the activeCamera object's transform. But I'm having trouble getting the rotation of the player graphics to work
Player Movement script:
var moveSpeed : float = 10;
var activeCamera : GameObject;
function Update ()
{
var vert : float = Input.GetAxis("Vertical");
var horz : float = Input.GetAxis("Horizontal");
transform.eulerAngles.z = 0;
transform.eulerAngles.x = 0;
rigidbody.AddForce(activeCamera.transform.right*horz*moveSpeed);
rigidbody.AddForce(activeCamera.transform.forward*vert*moveSpeed);
}
This is what I have so far for the player graphics rotation. I just don't know where/how to implement the activeCamera's transform so that when I press down on the joypad the playergraphic rotates relative to it.
var speed : float = 1.0;
var playergraphic : Transform;
var activeCamera : GameObject;
function Update ()
{
transform.eulerAngles.z = 0;
transform.eulerAngles.x = 0;
transform.eulerAngles.y = 0;
MoveRotation();
}
function MoveRotation ()
{
var vert : float = Input.GetAxis("Vertical");
var horz : float = Input.GetAxis("Horizontal");
var moveDirection = new Vector3(horz, 0, vert);
if(moveDirection != Vector3.zero)
{
var newRotation = Quaternion.LookRotation(moveDirection);
playergraphic.transform.rotation = Quaternion.Slerp (playergraphic.transform.rotation, newRotation, Time.deltaTime * speed);
}
}
Answer by Tomas_D · May 10, 2014 at 05:14 AM
Found the answer in this thread here: unity Answers forum
Answer by Mr.Hal · May 10, 2014 at 05:44 AM
Tell me if that works, I'm pretty sure this is how the Silent Hill team would do it. You might need to flip "Quaternion.LookRotation(playergraphic.position - activeCamera.transform.position)" to "Quaternion.LookRotation(activeCamera.transform.position - playergraphic.position)", if the direction is backwards.
var speed : float = 1.0;
var playergraphic : Transform;
var activeCamera : GameObject;
function Update ()
{
//Slerps the rotation of the camera to point at the player via a calculated direction
activeCamera.transform.rotation = Quaternion.Slerp (activeCamera.transform.rotation, Quaternion.LookRotation(playergraphic.position - activeCamera.transform.position), Time.deltaTime * speed);
}
No man if I put it in exactly like you wrote it my player character sprouts three different axis and the graphic wigs out and flickers. When I flip it the player.graphic looks fine but the player control is revised. I managed to put this together and it works ok, but if activeCamera's y rotation is in the negative the player graphics start wigging out and sprouting three axis.
var speed : float = 1.0;
var playergraphic : Transform;
var activeCamera : GameObject;
function Update ()
{
transform.eulerAngles.z = 0;
transform.eulerAngles.x = 0;
$$anonymous$$oveRotation();
}
function $$anonymous$$oveRotation ()
{
var vert : float = Input.GetAxis("Vertical");
var horz : float = Input.GetAxis("Horizontal");
var moveDirection = new Vector3(horz, 0, vert);
var camDir : Vector3 = activeCamera.transform.rotation * moveDirection;
var objectDir : Vector3 = playergraphic.transform.position + camDir;
if(moveDirection != Vector3.zero)
{
var newRotation = Quaternion.LookRotation(objectDir - playergraphic.transform.position);
playergraphic.transform.rotation = Quaternion.Slerp (playergraphic.transform.rotation, newRotation, Time.deltaTime * speed);
}
}
lol nvm about the negative rotation I'm just stupid. I was putting in -90 to get a rotation when I could of just put 270. The script I posted above works just remember to use positive numbers.
Your answer
Follow this Question
Related Questions
How to force camera rotation? 1 Answer
The solution with camera rotation which is used in most strategic games 1 Answer
Rotate camera 1 Answer
Smooth Follow Camera Rotate on Z-Axis? 2 Answers
Rotating gameobject after mouselook 0 Answers