- Home /
how to add a drag/resistance effect to my character's movement
I want to move my character left and right if Input.getKey(left/right arrow)
but instead of the usual way transform.position+=Vector3.Right*Speed*Time.deltaTIme
where the character would move with a constant speed ex. from 0 to 3 when Key pressed and from 3 to 0 suddenly when keyUp . instead I want to gradually reach a max speed within a frame of Time when Key pressed and gradually back to zero when Key is Up.
Answer by yasser_kaddoura · Sep 17, 2016 at 04:33 PM
I guess this is what you are looking for:
horizontal = Input.GetAxis("Horizontal");
myRb.velocity = new Vector2(horizontal * speed, myRb.velocity.y);
hey @yasser_kaddoura, I think this may work, but I'm afraid it's too steep for my purpose, that is the horizontalAxis goes from 0-1very fast so the dragging effect is $$anonymous$$imized, can I adjust the sensitivity of the axis?
also it seems to me as though the value of horizontalAxis abruptly change to zero when $$anonymous$$ey is up ele$$anonymous$$ating the slide movement at the end, am I correct?
Check the input manager (Edit -> Project Settings -> Input) and change the behaviour the way you like. The "sensitivity" value controls how fast the value goes from 0 to 1 when the button is pressed. The "gravity" value controls how fast it returns to 0. You also might want to unckeck "snap" which does exactly what you don't want: When you press the opposite direction it snaps to 0 ans start "growing" in the opposite direction.
thanks alot @Bunny83 :) it's exactly what I wanted
Answer by SorenaCoder · Sep 17, 2016 at 07:46 PM
Hi, First :
var horizontal = Input.GetAxis("Horizontal") * speed;
var vertical = Input.GetAxis("Vertical") * speed;
rigidbodyComponent.velocity += new Vector3(horizontal,vertical);
and in second step:
rigidbodyComponent.velocity = Vector3.ClampMagnitude(rigidbodyComponent.velocity,maximumSpeed);
Have fun
Your answer

Follow this Question
Related Questions
Networked position interpolation implementation (not Dead Reckoning)? 1 Answer
Standard FPS Controller - Smoothing Movement 2 Answers
Character Motor: standard vs. custom ? 0 Answers
How to Tilt a Spaceship Based on Inertia/Acceleration Force Separate from Facing Direction? 2 Answers
How to move an object smoothly while in a Coroutine / Ienumerator? 3 Answers