How to make Rigidbody movement not slippery?
I already try changing the speed changing the ForceMode changing the rigidbody mass and drag, nothing seems to make the movement "responsive", all these does is making the character feels "heavier" or "lighter".
Here's the movement script
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour
{
public float speed = 10f;
private Rigidbody rb;
void Awake()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical).normalized;
rb.AddForce(movement * speed * Time.fixedDeltaTime, ForceMode.Impulse);
}
}
I'm not sure what you mean by "slippery," and how to relates to the other word you use, "responsive." Do you mean it feels like Asteroids, where it takes a while to change direction?
But since you wrote slippery in the title: the slipperiness of rigidbodies is controlled by Physics $$anonymous$$aterials.
Yes, "slippery" like Asteroid.
With my original code, if i set the Rigidbody mass to low number it will have fast acceleration but will take long time before it stopped. I want to make it have fast acceleration and fast deceleration.
Answer by DoTA_KAMIKADzE · Sep 12, 2015 at 09:48 AM
My guess is that you have no idea when you should use AddForce, am I right? If yes, then just replace your line#19 (rb.AddForce) with that one:
rb.velocity = movement * speed;
yes you're right, i don't even know what AddForce do actually, i just used it because some of the tutorials i see are using it. :D
Thanks for your suggestion your code works nicely, but when i let go the movement button the character still moving for a while before it stop (like there's a lag). Can something be done to prevent this?
If there is no input set the velocity to Vector3.zero
Depends on implementation of your input, but considering your comment, you should check out Input $$anonymous$$anager THERE (e.g. my one more guess would be that if you'll set gravity of your both axis there to something like 9000 then it will work as you expect, now in order to understand what it does follow the link that I've provided).
Alternatively you can do what Sarper Soher has suggested you to do. There are also plenty of other ways to do the same, as I said at the beginning that mostly depends on your implementation and what variety of behaviors you expect in the end.
thanks a lot. been a rough transition from in-game level editors to c#. i have no idea what i'm doing lol. now how do i make gravity affect velocity.....
Answer by Cherno · Sep 12, 2015 at 08:06 PM
In FixedUpdate, at first, set the RB's velocity to Vector3.zero. After that, use Transform.Translate to set the desired movement vector.
Note 1: Many users say that Translate shouldn't be used with RB movement, but I haver never had any problems with it, and it is also used in at least one comprehensive RB movement tutorial. Note 2: If the RB is being affected by other phyics (explosions, being pushed etc), this needs to be suspended temporarily, of course.
Answer by Moohasha · Sep 12, 2015 at 10:20 PM
Adding force to a Rigidbody means you are using the physics engine to move a GameObject. In real life (ie, using physics), when you move an object you apply a force to it and it accelerates to a point where the dynamic friction of the surface it is moving over prevents it from accelerating any further. Sometimes this acceleration appears very sudden, like when a sprinter shoots off the starting blocks, but it's still a gradual acceleration from no velocity to fast velocity.
I suggest either not using the Rigidbody to move your GameObject (just translate the Transform manually) or specify VelocityChange as the ForceMOde when you call AddForce(). VelocityChange means you are immediately changing the velocity to something else, ignoring mass and acceleration. You can use this to immediately begin moving at some velocity or to immediately stop.
Answer by PuzzleBuilder · Jan 02, 2017 at 08:25 AM
In the input manager, increase the gravity for the vertical and horizontal axis inputs. I did this while using the movePosition function, and it worked well. I think it would also help in your case.
Gravity in the input manager is really for beginners using keyboard input and a simple "speed=an-axis" code line. Then, ins$$anonymous$$d of having to code yourself "push key = gradual speed up; let go of key = slow down," you can use the Input manager sliders. It's mostly used to tweak the standard character controller script.
If you use Addforce on a rigidbody (like the poster is,) you're already coding the gradual speed increase/decrease. Also using input manager settings is driving like a car with two steering wheels. It's simpler to just code with keyDown and ignore axises (and for tablets, you have to do that anyway,)
Your answer
Follow this Question
Related Questions
Acceleration only increasing when I press a key 0 Answers
Rigidbody movement instead of transform? 0 Answers
Issues Getting my GameObject to both Move Forward and Jump properly. 0 Answers
Rigidbody movement: Changing the Y of transform.position makes my character stutter 0 Answers
Jump is higher then normal 0 Answers