Hello to all,
When i created the controller everything worked well. Needed to only have about 10-15 force to get the player moving well. But after adding more code to both the controller and triggers and colliders i need to add about 400f of force and it jusgt doesnt move as well as it did when i first made it…
controller code:
public class Controller : MonoBehaviour
{
Rigidbody rb;
float xInput, zInput;
public float speed;
public float jumpforce;
public float DashForce;
public LayerMask GroundLayer;
public SphereCollider Col;
float fallmult = 2.5f;
float lowjump = 2f;
bool jump = false;
private void Awake()
{
rb = GetComponent<Rigidbody>();
Col = GetComponent<SphereCollider>();
}
// Update is called once per frame
void Update()
{
xInput = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
//zInput = Input.GetAxis("Vertical") * speed * Time.deltaTime;
if (IsGrounded() && Input.GetButtonDown("Jump"))
{
jump = true;
}
if (rb.velocity.y < 0)
{
rb.velocity += Vector3.up * Physics.gravity.y * (fallmult - 1) * Time.deltaTime;
}
else if (rb.velocity.y > 0 && !Input.GetButton("Jump"))
{
rb.velocity += Vector3.up * Physics.gravity.y * (lowjump - 1) * Time.deltaTime;
}
}
private void FixedUpdate()
{
rb.velocity = new Vector3(xInput, rb.velocity.y, 0);
if (jump)
{
Jumping();
jump = false;
}
if (Input.GetKeyDown(KeyCode.LeftShift))
{
rb.AddForce(DashForce* Time.deltaTime, 0, 0, ForceMode.Impulse);
}
}
void Jumping()
{
rb.AddForce(Vector3.up * jumpforce * Time.deltaTime, ForceMode.Impulse);
}
private bool IsGrounded()
{
return Physics.CheckCapsule(Col.bounds.center, new Vector3(Col.bounds.center.x, Col.bounds.min.y, Col.bounds.center.z), Col.radius * 0.9f, GroundLayer);
}
}
ive checked various tutorials and the code i use is quitre similar to what others use, and i dont see where the issue could be coming from.
Is there anthing in my code that i missed that would make the ocntrols slow/sluggish?
Best Regards,
Przemyslaw
p.s the Input.GetKeyDown(KeyCode.LeftShift)
doesnt seem to work unless i add ridiculous amounts of force but when i do that the player kinda “teleports”.
But when i add to the dash as Input.GetKey(KeyCode.LeftShift)
it works pretty well. But the problem is that as long as they hold LShift they keep having force added
You’re directly changing the velocity of the Rigidbody in Update which is going to completely trample over any forces you may be adding in FixedUpdate. You’re also trying to capture a KeyDown event in FixedUpdate which is liable to cause missed inputs.
In summary:
- Pick one, adding forces or setting velocity. But not both, they will fight with each other
- Only do Rigidbody operations in FixedUpdate.
- Only capture input in Update().
To reconcile 2. and 3. the general approach is to capture the input in Update and consume it in FixedUpdate using instance variables like you’re already doing with jump
.
1 Like
Hmmmm, ok… So ive removed any add force and im going to stick just with adding velocities. I ve also removed all KeyDown events in Fixed-Update. Ive alse removed trying to dash until i understand what im doing wrong. My current conde looks like this, and i think ive fixed what you said was incorrect, yet the controls are still the same.
public class Controller : MonoBehaviour
{
Rigidbody rb;
float xInput, zInput;
public float speed;
public float jumpforce;
public LayerMask GroundLayer;
public SphereCollider Col;
float fallmult = 2.5f;
float lowjump = 2f;
bool jump = false;
private void Awake()
{
rb = GetComponent<Rigidbody>();
Col = GetComponent<SphereCollider>();
}
void Update()
{
xInput = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
if (IsGrounded() && Input.GetButtonDown("Jump"))
{
jump = true;
}
if (rb.velocity.y < 0)
{
rb.velocity += Vector3.up * Physics.gravity.y * (fallmult - 1) * Time.deltaTime;
}
else if (rb.velocity.y > 0 && !Input.GetButton("Jump"))
{
rb.velocity += Vector3.up * Physics.gravity.y * (lowjump - 1) * Time.deltaTime;
}
}
private void FixedUpdate()
{
rb.velocity = new Vector3(xInput, rb.velocity.y, 0);
if (jump)
{
Jumping();
jump = false;
}
}
void Jumping()
{
//rb.AddForce(Vector3.up * jumpforce * Time.deltaTime, ForceMode.Impulse);
rb.velocity = new Vector3(0, jumpforce * Time.deltaTime, 0);
}
private bool IsGrounded()
{
return Physics.CheckCapsule(Col.bounds.center, new Vector3(Col.bounds.center.x, Col.bounds.min.y, Col.bounds.center.z), Col.radius * 0.9f, GroundLayer);
}
}
I had also tried doing to the horizantal movement the same as i did with jump, and that was slightly better i only need to put the speed variable to 250 to get movement, which is down by 150.
public class Controller : MonoBehaviour
{
Rigidbody rb;
float xInput, zInput;
public float speed;
public float jumpforce;
public LayerMask GroundLayer;
public SphereCollider Col;
float fallmult = 2.5f;
float lowjump = 2f;
bool move = false;
bool jump = false;
private void Awake()
{
rb = GetComponent<Rigidbody>();
Col = GetComponent<SphereCollider>();
}
// Update is called once per frame
void Update()
{
//zInput = Input.GetAxis("Vertical") * speed * Time.deltaTime;
if (Input.GetButton("Horizontal"))
{
move = true;
}
else if (Input.GetButtonUp("Horizontal"))
{
move = false;
}
if (IsGrounded() && Input.GetButtonDown("Jump"))
{
jump = true;
}
if (rb.velocity.y < 0)
{
rb.velocity += Vector3.up * Physics.gravity.y * (fallmult - 1) * Time.deltaTime;
}
else if (rb.velocity.y > 0 && !Input.GetButton("Jump"))
{
rb.velocity += Vector3.up * Physics.gravity.y * (lowjump - 1) * Time.deltaTime;
}
}
private void FixedUpdate()
{
if (move)
{
Move();
}
else if (move==false)
{
NoMove();
}
if (jump)
{
Jumping();
jump = false;
}
}
void Move()
{
xInput = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
rb.velocity = new Vector3(xInput, rb.velocity.y, 0);
}
void NoMove()
{
rb.velocity = new Vector3(0, rb.velocity.y, 0);
}
void Jumping()
{
//rb.AddForce(Vector3.up * jumpforce * Time.deltaTime, ForceMode.Impulse);
rb.velocity = new Vector3(0, jumpforce * Time.deltaTime, 0);
}
private bool IsGrounded()
{
return Physics.CheckCapsule(Col.bounds.center, new Vector3(Col.bounds.center.x, Col.bounds.min.y, Col.bounds.center.z), Col.radius * 0.9f, GroundLayer);
}
}