- Home /
Problem with Lerp/Slerp
I know there are similar questions to this and I have probably read them. The issue is that nothing I've tried seems to work. I currently am just trying to do a simple movement of a cube around a open plane using touch features. I have the touch controls working and the cube does move. However, it instantaneously moves and rotates to its new position. I want it to accelerate towards the target point and then slow as it approaches it. I have been told that Lerp could be used but adding it in to the program didn't seem to accomplish anything. (Sorry for the unorganized question, I have a horrible time communicating problems). My code is below. I know its unorganized and probably inefficient but I'm not trying to do anything groundbreaking as I am still learning all the ins and outs of Unity. Not looking for someone to do the code for me, just looking for a push in the right direction would be great thanks.
var touch : Touch;
if (Input.touchCount > 0) {
if (Input.GetTouch(0).phase == TouchPhase.Moved){
var primarytouch : Touch = Input.GetTouch(0);
var speed = primarytouch.deltaPosition/primarytouch.deltaTime;
}
if (Input.GetTouch(0).phase == TouchPhase.Ended){
var finishedtouch : Touch = Input.GetTouch(0);
var ray : Ray = Camera.main.ScreenPointToRay(finishedtouch.position);
var controller : CharacterController = GetComponent(CharacterController);
var hit : RaycastHit;
var moveDirection : Vector3 = Vector3.zero;
if (Physics.Raycast(ray,hit,100))
{
var startPos = transform.position;
var targetPoint = hit.point;
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.time *2.0);
transform.position = Vector3.Lerp(startPos, hit.point, Time.time * 10);
}
}
}
}
Answer by whydoidoit · Jun 13, 2012 at 10:30 PM
You are falling into the classic trap of using Slerp with Time.time - that is the amount of time since the game started and if it is more than 1 second you will find you snap directly to the ending position:
The last parameter of Slerp needs to be a float between 0 and 1.
Either :
Use Time.deltaTime - this will give you a damping effect toward the target
Create a variable of type float. Initialize it to 0 when you start the move, add Time.deltaTime on to it.
Your answer
Follow this Question
Related Questions
Slerp / lerp not creating a smooth transition 2 Answers
How can i smoothly rotate a gameobject 1 Answer
slerp vs lerp for rotation 2 Answers
Rotate object towards mouse position 1 Answer
How do I transition smoothly between two lerps/slerps? 1 Answer