- Home /
My Player goes through the colliders even I used Rigidbody.MovePosition , Please help me !!
Here is the script, I dont know why it goes through the obstacles. when I reduce the moving speed of player it actually works, but then player is too slow, I think player's capsule collider is big enough to collide,
public class Controller : MonoBehaviour {
public float moveSpeed = 10f; //moving speed
public float FacingSpeed = 20f; //player roation speed
Rigidbody rigidbodyThis;
public Transform lookingPoint;
public Camera cam; //get the relative direction to the camera
private float horizontal;
private float vertical;
void Start () {
rigidbodyThis = GetComponent<Rigidbody> ();
}
void FixedUpdate() {
MovePlayer(); //move the player
//look towards mouse position
Vector3 relativePos = lookingPoint.position - rigidbodyThis.position;
Quaternion lookdirection = Quaternion.LookRotation(relativePos);
Quaternion lookdirection_y = Quaternion.Euler(rigidbodyThis.rotation.eulerAngles.x, lookdirection.eulerAngles.y, rigidbodyThis.rotation.eulerAngles.z);
rigidbodyThis.rotation = Quaternion.Lerp(rigidbodyThis.rotation, lookdirection_y, FacingSpeed * Time.fixedDeltaTime);
}
void MovePlayer()
{
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
Vector3 forword = cam.transform.forward;
Vector3 right = cam.transform.right;
forword.y = 0f;
right.y = 0f;
forword.Normalize();
right.Normalize();
Vector3 TargetPoint = (right * horizontal + forword * vertical) * moveSpeed;
rigidbodyThis.MovePosition (rigidbodyThis.position + TargetPoint * Time.fixedDeltaTime);
}
}
Answer by DenisIsDenis · Jun 05, 2021 at 08:25 AM
Your controller is going through walls when it is diagonally going because you are not normalizing the movement vector. Replace these lines:
Vector3 TargetPoint = (right * horizontal + forword * vertical) * moveSpeed;
rigidbodyThis.MovePosition (rigidbodyThis.position + TargetPoint * Time.fixedDeltaTime);
with this:
Vector3 TargetPoint = right * horizontal + forword * vertical;
rigidbodyThis.MovePosition(rigidbodyThis.position + TargetPoint.normalized * Time.fixedDeltaTime * moveSpeed);
Answer by Yrandika · Jun 05, 2021 at 12:32 PM
Thanks for the reply, I could not reply earlier due to an internal issue or some what problem. but I used your code and then player started move way more slowly, even I increased the speed of player. I dont understand the issue. but I came across another way to solve, I used character controller instead of just using rigidbody. Thank you
Your answer
![](https://koobas.hobune.stream/wayback/20220613034624im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
What is the best way to move the player with physics without using MovePosition? 0 Answers
Moving and SweepTesting a kinematic Rigidbody multiple times within a single FixedUpdate() call 0 Answers
Rigidbody.MovePosition/MoveRotation Hits far away colliders 2 Answers
Calling Rigidbody.MovePosition, no movement at all 4 Answers