Player going through walls
I know there is already a lot of similar questions out there, but none of them have any helpful answers. I have a player with a Rigidbody and a Capsule Collider. I have some simple 3D Cubes for ground and walls. When I move the player against a wall, it just glitches through. :( Here's my script:
public KeyCode forwardKey;
public KeyCode backwardKey;
public KeyCode leftKey;
public KeyCode rightKey;
public KeyCode jumpKey;
public float speed;
Rigidbody body;
// Start is called before the first frame update
void Start()
{
body = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
// Movement
Vector3 new_pos = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
body.MovePosition(transform.position + new_pos * Time.fixedDeltaTime * speed);
// Jumping
RaycastHit hit;
Vector3 down = transform.TransformDirection(Vector3.down);
if (Physics.Raycast(transform.position, down, out hit, 1f))
{
if (Input.GetKeyDown(jumpKey))
{
body.AddForce(Vector3.up * 500);
}
}
}
Also, if anybody knows why my jumping isn't working, that would be useful too.
Thanks in advance! :D
Comment
Your answer
Follow this Question
Related Questions
Gravity is not working 0 Answers
How i make my player object passes through walls? 2 Answers
How to move an object able to collide through something outside of physics? 0 Answers
Dont know how to change X and Z values without changing the Y. 0 Answers
Rigidbody. Acceleration of free falling object works bad 0 Answers