Question by
IsoHitEmup · Dec 13, 2020 at 04:40 AM ·
physicsrigidbodyplayerplayer movement
Player moves indefinitely after collision
My player moves just fine, but I noticed that after colliding with any wall, my plate starts to move on its own opposite of the direction it hit the wall. I believe this may be a physics problem but idk how to fix it.
Here is my script for walking:
public class Walk : MonoBehaviour
{
public Rigidbody rb;
public CapsuleCollider col;
public LayerMask groundLayers;
public float jumpForce;
void Start()
{
rb = GetComponent<Rigidbody>();
col = GetComponent<CapsuleCollider>();
}
void Update()
{
float movement = Input.GetAxisRaw("Vertical");
movement *= Time.deltaTime;
float sideStep = Input.GetAxisRaw("Horizontal");
sideStep *= Time.deltaTime;
this.transform.Translate(Vector3.forward * movement);
this.transform.Translate(Vector3.right * sideStep * 2);
if (Input.GetKey(KeyCode.LeftShift))
{
this.transform.Translate(Vector3.forward * movement * 2.75f);
}
else
{
this.transform.Translate(Vector3.forward * movement * 1.5f);
}
//move using physics, better for vehicles
//this.GetComponent<Rigidbody>().AddForce(this.transform.forward * movement * 1000f);
if (IsGrounded() && Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(Vector3.up * jumpForce, 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 * .3f, groundLayers);
}
}
Comment
Your answer
Follow this Question
Related Questions
Rigidbody can push but can't be pushed 2 Answers
Player Movement Goes Crazy When Hit On Side Of Moving Platform 0 Answers
Rigidbody falling not according to physics, only when script is applied. 0 Answers
[Help] Player stops moving when hitting wall diagonally 0 Answers
Ball Speed is not increasing as per code 0 Answers