- Home /
Third Person Camera Control
Hi,
I am making a game which plays from the third person perspective and am having a bit of trouble with the camera control. I am trying to get similar camera play as that of Assassin's Creed and Warframe, where the character model stays at one rotation while the camera orbits around him and when the forward movement key is pressed, the character will rotate smoothly to that rotation, but ONLY on the "y" axis.
What I've tried using (but to no avail) is this:
function Update ()
{
if(Input.GetKey("w"))
{
transform.rotation =
Quaternion.Slerp(transform.rotation, CameraController.rotation,
rotSpeed * Time.deltaTime);
}
}
Any help or suggestions are greatly appreciated, thanks.
Answer by JinJin · May 05, 2015 at 10:32 AM
use Mathf.Lerp to interpolate transform.rotation.eulerAngles.y with CameraController.rotation.eulerAngles.y
Vector3 eulerAngles = transform.rotation.eulerAngles;
Mathf.Lerp( eulerAngles.y, CameraController.rotation.eulerAngles.y, rotSpeed * Time.deltaTime );
transform.rotation.eulerAngles = eulerAngles;
I've tried this, but it isn't doing anything at all.... Have I missed something?
Full script:
#pragma strict
private var anim : Animator;
var CameraController : Transform;
var rotSpeed : float;
function Start ()
{
anim = GetComponent("Animator");
}
function Update ()
{
var pEulerAngles : Vector3 = transform.rotation.eulerAngles;
if(Input.Get$$anonymous$$ey("w"))
{
$$anonymous$$athf.Lerp(pEulerAngles.y, CameraController.rotation.eulerAngles.y,
rotSpeed * Time.deltaTime);
transform.rotation.eulerAngles = pEulerAngles;
}
//Animation
var h : float = Input.GetAxis("Horizontal");
var v : float = Input.GetAxis("Vertical");
anim.SetFloat("Speed", v);
anim.SetFloat("Direction", h);
}
try without $$anonymous$$athf.Lerp()
Vector3 eulerAngles = transform.eulerAngles;
eulerAngles.y = CameraController.eulerAngles.y;
transform.eulerAngles = eulerAngles;
It works without $$anonymous$$athf.Lerp, but not how I need it too.. Without the $$anonymous$$athf statement, it simply snaps to the direction the camera is facing. I need it to smoothly rotate to the camera's Y rotation.
you can find your bug if you write to your unity console:
if(Input.Get$$anonymous$$ey("w"))
{
Debug.Log(pEulerAngles.y);
Debug.Log(CameraController.rotation.eulerAngles.y);
Debug.Log(rotSpeed);
$$anonymous$$athf.Lerp(pEulerAngles.y, CameraController.rotation.eulerAngles.y,
rotSpeed * Time.deltaTime);
Debug.Log(pEulerAngles.y);
transform.rotation.eulerAngles = pEulerAngles;
}
use this and you will see what goes wrong in the editor console
just compare the first and second Debug.Log(pEulerAngles.y); to see if it changed
Thanks, but I came to a different conclusion:
var angle : float = $$anonymous$$athf.LerpAngle(transform.rotation.eulerAngles.y,
CameraController.rotation.eulerAngles.y,
rotSpeed * Time.deltaTime);
if(Input.Get$$anonymous$$ey("w"))
{
transform.rotation.eulerAngles = Vector3(0,angle,0);
}
Thank you for your time regardless!
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Camera Control Help 0 Answers
character switching on collision 1 Answer
Mouse orbit + Smooth follow script question 0 Answers
3rd person camera reset bug 0 Answers