I have a move script for my player and it works fine except when I’m going too fast (which really isn’t that fast) I got through objects. I’m using a rigidbody and I’ve tried all of the different collision detection modes and nothing has worked. Does anyone know if I should I use a different physics component/reference the physics component in my move script?
The script:
{
Animator animator;
Vector2 input;
Rigidbody rb;
public float speed = 10f;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate()
{
//movement
//sprint
if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey("w"))
{
transform.position += transform.TransformDirection(Vector3.forward) * Time.deltaTime * speed * 2.5f;
}
//walk forward
else if (Input.GetKey("w") && !Input.GetKey(KeyCode.LeftShift))
{
transform.position += transform.TransformDirection(Vector3.forward) * Time.deltaTime * speed;
}
//walk backward
if (Input.GetKey("s"))
{
transform.position -= transform.TransformDirection(Vector3.forward * Time.deltaTime) * speed;
}
if (Input.GetKey("a"))
{
transform.position += transform.TransformDirection(Vector3.left * Time.deltaTime) * speed;
}
if (Input.GetKey("d"))
{
transform.position -= transform.TransformDirection(Vector3.left * Time.deltaTime) * speed;
}
//animation
input.x = Input.GetAxis("Horizontal");
input.y = Input.GetAxis("Vertical");
animator.SetFloat("InputX", input.x);
animator.SetFloat("InputY", input.y);
}
}