- Home /
How to use Slerp / Lerp / RotateTowards to rotate an object (from-to) two rotations but around their Y axis only?
I've created a script for character movement from scratch .. and it has just worked as perfect as i wanted, but I had a real issue..
in my script I've relied for movement on Slerp , Lerp and RotateTowards functions for Quaternions .. I mean I've tried the three of them and i got the same result of rotating and Looking towards camera just as I wanted .. The Problem is : my character is rotating around all the axis of my Orbit Camera [the target rotation] .. I just need it to be rotated only around a Y Axis of Orbit Camera so that when I rotate my camera up and down it doesn't affect my character to rotate down with it as well .. Take a look at my script :
var Speed : float = 6;
var Gravity : float = 20;
var rotationSpeed : float = 5;
var Angle : float;
var MainCamera : Transform;
function Update() {
controller = GetComponent(CharacterController);
if (controller.isGrounded)
{
moveDirection = Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
moveDirection = MainCamera.TransformDirection(moveDirection);
moveDirection *= Speed;
moveDirection.y = -10;
if (controller.velocity.sqrMagnitude > 0.1)
transform.rotation = Quaternion.Slerp(transform.rotation,MainCamera.rotation*Quaternion.Euler(0,Angle,0),Time.deltaTime*rotationSpeed);
//
if (Input.GetAxis("Vertical") >= 0.01)
{
Angle = 0;
if (Input.GetAxis("Horizontal") >= 0.01)
Angle = 45;
if (-Input.GetAxis("Horizontal") >= 0.01)
Angle = -45;
}
//
if (Input.GetAxis("Horizontal") >= 0.01)
{
Angle = 90;
if (Input.GetAxis("Vertical") >= 0.01)
Angle = 45;
if (-Input.GetAxis("Vertical") >= 0.01)
Angle = 135;
}
//
if (-Input.GetAxis("Vertical") >= 0.01)
{
Angle = 180;
if (Input.GetAxis("Horizontal") >= 0.01)
Angle = 135;
if (-Input.GetAxis("Horizontal") >= 0.01)
Angle = -135;
}
//
if (-Input.GetAxis("Horizontal") >= 0.01)
{
Angle = -90;
if (Input.GetAxis("Vertical") >= 0.01)
Angle = -45;
if (-Input.GetAxis("Vertical") >= 0.01)
Angle = -135;
}
}
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
function ApplyGravity () {
moveDirection.y -= Gravity * Time.deltaTime;
}
Please , help me in my problem within these three functions Slerp or Lerp or RotateTowards, Thank you.
here's a sample of the game on the web so that you could see the problem by your selves : http://www.4shared.com/rar/jdycCqeS/The_Sample_Game.html?
How about you lerp only the Y value?
Use `Quaternion.LookRotation` to calculate the necessary rotation that looks at the camera, convert it to euler angles with `Quaternion.eulerAngles`, then lerp the character's `transform.eulerAngles.y` to the rotation's y value.
Your answer
Follow this Question
Related Questions
How to use Lerp? 3 Answers
Movement Smoothing Script not Working 2 Answers
Character too floaty after jumping and grappling.,Grapple too floaty 0 Answers
slerp vs lerp for rotation 2 Answers
Character Controller Movement 0 Answers