I try to make a player who can collide with a wall. It sounds very simple but I encounter a problem.
My player is a box - it has got a rigidbody, box collider and a simple script for moving:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float forwardVel = 12;
public float rotateVel = 120;
public Quaternion targetRotation;
Rigidbody rBody;
float forwardInput, turnInput;
public Quaternion TargetRotation {
get {
return targetRotation;
}
}
void Start() {
targetRotation = transform.rotation;
rBody = (GetComponent<Rigidbody>());
forwardInput = turnInput = 0;
}
void GetInput() {
forwardInput = Input.GetAxis("Vertical");
turnInput = Input.GetAxis("Horizontal");
}
void Update() {
GetInput();
Turn();
}
void FixedUpdate() {
Run();
}
void Run() {
rBody.velocity = transform.forward * forwardInput * forwardVel;
}
void Turn() {
targetRotation *= Quaternion.AngleAxis(rotateVel * turnInput * Time.deltaTime, Vector3.up);
transform.rotation = targetRotation;
}
}
My walls have also got a rigidoby and a mesh collider but my player can’t collide with them and I don’t know why. I’d be very grateful, if anyone could help me.
Here are two screenshots where you can see my player’s and walls’ components: