How to set the time value in Lerp, Slerp, RotateTowards?
I am trying to make a simple 2.5d character controller script. Or so I deluded my self into thinking. After spending most of the day trying to implement the turning of my character, I think my only option is to ask in here.
This is my code
public GameObject theFox;
public float speed = 6.0F;
public float jumpSpeed = 2.F;
public float gravity = 60.0F;
private Vector3 moveDirection = Vector3.zero;
private Vector3 TargetRot;
private Vector3 ThePlaceFromWhenceTheFoxRotated;
float rotationSmooth = 100f;
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed;
}
}
theFox.transform.eulerAngles = Vector3.Lerp(ThePlaceFromWhenceTheFoxRotated, TargetRot, rotationSmooth * Time.deltaTime);
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.D))
{
theAnimator.SetBool("WalkingRight", true);
TargetRot = new Vector3(0,90,0);
ThePlaceFromWhenceTheFoxRotated = theFox.transform.eulerAngles;
}
if (Input.GetKeyUp(KeyCode.D))
{
theAnimator.SetBool("WalkingRight", false);
}
if (Input.GetKeyDown(KeyCode.A))
{
theAnimator.SetBool("WalkingRight", true);
TargetRot = new Vector3(0, 270, 0);
ThePlaceFromWhenceTheFoxRotated = theFox.transform.eulerAngles;
}
if (Input.GetKeyUp(KeyCode.A))
{
theAnimator.SetBool("WalkingRight", false);
}
}
I have a gameObject with a character controller in it, and a model with animations as a child. I can move and jump with the character. Now I want it to turn and face the way it is walking - thus my idea was that i could simply turn the model.
This code results in my character snapping in the correct direction. But there is no learping. If i set the rotationSmooth value lower, it just sets the rotation at a wrong place.
I think the problem is with my use of time.deltatime - but all answers for this question I have found via google, use lerp like this , even the example in the documentation uses a lerp in the update function with time.deltatime * some value. I have tried using Quaternions, Slerp, Time.time, rotating the localTransform, several layers of if statements but nothing works. It just snaps in the direction.
The answer is most likely something very basic , or something stupid in my script, but I would really appreciate some help.
Check my answer here :
http://answers.unity3d.com/questions/998544/having-difficulty-with-lerp.html#answer-998744
Thank you! That explains it - it wasent the time value that I had to fix, but the rotation of the model. There should be a link to your post in the documentation :)
Your answer
Follow this Question
Related Questions
keep player centred in grid based movement 0 Answers
Keep "ball" character's face rotating to be up? 0 Answers
How to smoothly rotate gameobject 20 degrees on key press? 0 Answers
fromtorotation inacurate Help fixing? 0 Answers
Rotate on key press to an end 0 Answers