- Home /
Easing character controller movement
Hi lovely unity guys again,
I need some help with easing character controller movement. I'm developing an iPad app with a character moving with a finger swipe. Basically, it moves when i wipe my finger across the screen. How do I achieve an easing movement before the character eventually comes to a stop. I've stripped down some of the codes but the character movement is something like this.
private var character : CharacterController;
var movement : Vector3 = Vector3.zero;
movement = thisTransform.position - targetLocation;
movement *= Time.deltaTime;
character.Move( movement );
I reckon there should be some of a smoothing script? please correct me if i'm wrong.
Thanks guys. Appreciate it.
Rgds, jser
updated code:
var startspot : Transform;
var endspot : Transform;
function Start () {
SmoothMove(startspot.position, endspot.position, 5.0);
}
function SmoothMove (startpos : Vector3, endpos : Vector3, seconds : float) {
var t = 0.0;
while (t <= 1.0) {
t += Time.deltaTime/seconds;
transform.position = Vector3.Lerp(startpos,
endpos, Mathf.SmoothStep(0.0, 1.0, t));
yield;
}
}
That's weid, i was trying to edit my question but i couldn't find a save button after i finish typing the changes. And the textbox is full of html tags. Please help. Thanks.
I already updated my answer. You have to use Update()
ins$$anonymous$$d of Start()
. See my answer.
After editing your question, you need to press the POST ANSWER button again. It's at the bottom of your example view
mmm...this is what i see on my screen when I click on the edit button under my question - http://www.jamesser.com/wip/screenshot.png
I couldn't see any Post Answer button :/
ok that's very odd. You should also see an example window underneath your question, so you can see what you asked.
Answer by Marnix · Jun 20, 2011 at 04:50 PM
You are looking for ease in ease out
, which is very easy to accomplish. You don't only use Time.deltaTime, but you have to multiply it with a speed. This speed is varying while moving. I found a reference for you, but you could easily look for ease in ease out
http://forum.unity3d.com/threads/28312-A-smooth-%28ease-in-amp-out%29-version-of-Lerp
Free in the asset store:
iTween
. A framework full of this kind of functions.
Update
The biggest problem you're having, is that you call your function only once at Start()
. Instead, you should call it everyframe, because else your character only moves a little piece over deltaTime (which is about 0.002 seconds). Try using this in the Update()
instead. Here you can read about all overridable methods in a MonoBehaviour.
Hi $$anonymous$$arnix, thanks for getting back. I'm trying to use the following code but how do I implement it to my character.$$anonymous$$ove( movement )?
movement is a Vector3 and Smooth$$anonymous$$ove(startspot.position, endspot.position, 5.0) is a co-routine. Thanks again.
[code in post]
ps: And one side question. How do i add a grey background to the codes in my future questions, like how ChrisD help me with.
@James Ser: Indeed, this is barely readable. If you have any updates on your question post them in your question. Every line of code should be preceded with four spaces. Do this easy by selecting your code and pressing the 10010101 button in the editor. If you revise this code in your question, I will have a look at it.
Hi $$anonymous$$arnix, it seems like i can't edit my initial question. I have edited my question as an answer. Pardon me for being dumb and appreciate your assistance. Thanks!
@James Set: This was on the forum that I posted in my question: http://forum.unity3d.com/attachment.php?attachmentid=6564&d=1250101098
Hey $$anonymous$$arnix, thanks for sending that across. How do i use this in conjunction with my character.$$anonymous$$ove( movement ) to give easing effect before my character comes to a stop?
Your answer
Follow this Question
Related Questions
CubeWorld's controller 0 Answers
Getting a smooth jump with the Character Controller 3 Answers
Smoothly move with Character Controller 3 Answers
animated color: how to smooth? 1 Answer