- Home /
I have added a force of 500 and an if statement if the press a certain key they will move a certain way but it does not move I know why it does not but there is another problem.
using System.Threading;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public Rigidbody rb;
public float forwardForce = 2000f;
// Update is called once per frame
void FixedUpdate()
{
rb.AddForce(0, 0, 0 * Time.deltaTime); // Time.deltaTime evens the speed depending on the framerate.
if ( Input.GetKey("d") )
{
rb.AddForce(500 * Time.deltaTime, 0, 0);
}
if ( Input.GetKey("a") )
{
rb.AddForce(-500 * Time.deltaTime, 0, 0);
}
}
}
(Read the header and then read this line) There is a problem where my cube does not move I know that my variable forward Force is conflicting with my if statements but when I fix that my 500 force is hardly pushing I have a physics material which makes the friction of the ground but I checked the dynamic force is 0 and the static friction is 0 but it still moves very slowly. I'm a beginner following a tutorial.
Answer by Eno-Khaon · Jan 02 at 03:41 AM
First things first:
You don't need to multiply velocity by Time.(fixed)deltaTime.
Adjusting for framerate is important for the resulting position, but you don't need to (and shouldn't) apply that to the velocity itself. Because you're calling Time.deltaTime in FixedUpdate(), it (by default) is multiplying the value by 0.02f (50 physics updates per second). In other words, the force you're attempting to apply is likely 10 units/second acceleration, divided by mass. Likewise, your "forwardForce" variable could be scaled down to a relative 40.
That matter doesn't necessarily explain why it's not moving, however, so let's try covering a few bases here:
Vector3 playerInput;
void Update()
{
// Input is read during Update(), so
// this is the most reliable place to check it
playerInput = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
}
void FixedUpdate()
{
// This ForceMode ignores mass, which would often be
// preferred for player-driven input
rb.AddForce(playerInput, ForceMode.Acceleration);
}
Additionally, you mention that you're moving a cube around; with the way PhysX handles friction between colliders, this should mean that you have two primary points of contact and potentially a lot more friction beyond that. If you *JUST* changed the friction, but not the "Friction Combine" of it in your PhysicMaterial (i.e. still using the average friction), then it will still put up a large amount of resistance toward that initial motion (static friction).
But still my friction combine by default was "Average" so should I change that?
You don't necessarily need to change the "Friction Combine" setting. The point to consider, however, is that a cube-type Rigidbody tends to have a large amount of friction against a flat surface when you're essentially just trying to slide it around.
If you intend to eli$$anonymous$$ate that friction as a factor in movement (or reduce it significantly, or similar), you might want to try a different friction setting to get a new behavior out of it.
Answer by spreadyourwings · Jan 02 at 05:33 AM
I think you need to add ForceMode.Impulse like this
if ( Input.GetKey("d") )
{
rb.AddForce(500 * Time.deltaTime, 0, 0, ForceMode.Impulse);
}
if ( Input.GetKey("a") )
{
rb.AddForce(-500 * Time.deltaTime, 0, 0, ForceMode.Impulse);
}
ForceMode.Impulse (and ForceMode.VelocityChange, by extension) is intended for instantaneous force application, where Input.GetKey is a continuous (every frame) operation.
ForceMode.Force (and ForceMode.Acceleration) is intended for gradual changes.
While that ForceMode choice *would* coincidentally behave in the same manner as simply removing the Time.(fixed)deltaTime multiplication, it's simpler to just remove the unnecessary multiplier than work around it, especially since behavior would change if the Physics time steps are modified.
I am confused about whether I need to add ForceMode.Impulse or ForceMode.Acceleration
ForceMode.Impulse is an instantaneous force (i.e. an impact or instructing the character to jump). It is a force type intended to be used once at a time.
By contrast, ForceMode.Acceleration applies force-per-second (i.e. gravity or persistent movement) while ignoring mass. It's a force type well suited to moving an object around in a (somewhat) fixed manner, regardless of its weight. For example, the speed that a person can run isn't related exclusively to their mass; it is also influenced by muscle, flexibility, skill, strategy, and practice in running, among other factors. As a result, it's simpler to just take the approach of setting a specific rate of acceleration to approximate the end result you're looking for.
rb.AddForce(Vector3.right * speed / rb.mass, ForceMode.Force);
rb.AddForce(Vector3.right * speed, ForceMode.Acceleration);
rb.AddForce(Vector3.right * speed * Time.fixedDeltaTime / rb.mass, ForceMode.Impulse);
rb.AddForce(Vector3.right * speed * Time.fixedDeltaTime, ForceMode.VelocityChange);
These all result in the same acceleration over time. The main point to consider in this case is that the instantaneous forces aren't a "proper" choice to apply continuous motion and you may or may not need to factor in the Rigidbody's mass when moving the cube around yourself.
Answer by huh1234 · Jan 02 at 07:20 AM
I am sorry I wasted your time because I had a constraint set on the x axis. I was testing constraints out and I accidently left the x axis on for position
Your answer
Follow this Question
Related Questions
Buggy Collision 1 Answer
Does anyone know what i am doing wrong? 0 Answers
How to apply friction independent of framerate? 1 Answer
Editor movement suddenly became very slow 2 Answers
Tesla Tower Trap 1 Answer