How can i stop cube from rolling
Hi guys, i got a runner game and cube is my player, the problem is i can't stop cube from rolling. The ground is slippery(friction = 0) but it is still rolling. When i freeze rotation of y axis it seems like lagging so it doesn't work either. Please help me. There is my movement code: public Rigidbody rb; public float forwardForce = 2000f; public float sidewaysForce = 500f; public float acceleration; public PlayerMovement movement;
void FixedUpdate()
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
forwardForce += Time.deltaTime * acceleration;
if (Input.GetKey("d"))
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("a"))
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
To note, Rigidbody.AddForce() already applies a continuous force by default (using Force$$anonymous$$ode.Force).
With this in $$anonymous$$d, you can reduce all of your forces by 50x (using default physics update rate of 50-per-second) and remove any redundant use of Time.deltaTime.
Furthermore, if you switch to using a mass-independent Force$$anonymous$$ode (since this is for a player character's movement), you won't have to make any accommodations in case you change your player's mass at any point.
public float forwardForce = 40f; // (2000 / 50)
public float sidewaysForce = 10f; // (500 / 50)
// ...
rb.AddForce(0, 0, forwardForce, Force$$anonymous$$ode.Acceleration);
// ...
rb.AddForce(sidewaysForce, 0, 0, Force$$anonymous$$ode.VelocityChange);
Check the physics material on the cube and also the FrictionCombine setting on the floor. It sounds there is a lot of friction, so when you prevent rolling, the friction grabs it.
Also, it seems like freezing x and z prevent rolling. y is just a spin -- like if one corner rams a doorframe, the cube can spin a little to try to fit through.
Answer by IrshadKhan_ · Aug 23, 2019 at 05:26 AM
hey @etikaram try this Code
public Rigidbody rb;
public float forwardForce = 2000;
public float acceleration = 250;
public float sidewaysForce = 500;
void FixedUpdate()
{
if (Input.GetKey("d"))
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
forwardForce += Time.deltaTime * acceleration;
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("a"))
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
forwardForce += Time.deltaTime * acceleration;
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
}
Or
public Rigidbody rb;
public float forwardForce = 2000;
public float acceleration = 250;
public float sidewaysForce = 500;
void FixedUpdate()
{
if (Input.GetKeyDown("d"))
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
forwardForce += Time.deltaTime * acceleration;
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKeyDown("a"))
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
forwardForce += Time.deltaTime * acceleration;
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
}
Answer by GhostlyFedoras · Aug 20, 2019 at 07:38 PM
Hey, Assuming you do not want it to rotate what so ever You can always use,
transform.rotation = Quaternion.identity;
this just means the cube will have no rotation Just add this into your Update/FixedUpdate and your character wont roll in any direction, Hope this helps. you can see more on Quaternions etc here if this doesn't work or isn't what you need, https://docs.unity3d.com/ScriptReference/Quaternion.html
Your answer
