Problem with collision
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: http://i.imgur.com/QQjBCMF.jpg http://i.imgur.com/GHTsWFi.jpg
Did you change your collision layer setup? None of the colliders are triggers right?
Answer by Dibbie · Jun 26, 2016 at 08:00 AM
It looks like your player's box collider is set to "IsTrigger" which ignores physical collision and uses a "invisible collision" essentially, which doesnt stop objects... Try setting "IsTrigger" to false on your player - and another thing to note, it looks like your map itself is imported, so check the import settings to see if you have "Generate Colliders" on, cause that may help as well.
Setting "IsTrigger" to false worked! Thanks for quick help :)
Glad I could help - if you found this answer the solution to your problem, please mark it as Best Answer to remove it from the Unanswered category.
Answer by nadhimali · Jun 26, 2016 at 08:09 AM
or maybe you select different layers to each object and didn't set them to collide in physics settings. try to check Edit>Project Setting>Physics and look at the ticks under Layer Collision Matrix