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);
}
}