Player/platform collision not detected
Hey guys,
Hoping some genius might be able to spot what I'm missing here :)
I have a rotating platform. I want to parent the player to it on collision, and I've been using transform.parent trying to get it to work. I've tried both OnTrigger and OnCollision events, none of which are detected during runtime.
The player has a non-kinematic Rigidbody (XYZ rotations frozen, Z position frozen), a sphere collider and is tagged "Player".
I've checked the Physics Settings, and there are no layers ignoring each other's collisions.
Here's my code:
public bool PowerOff = true;
public float RotationSpeed;
Animator PowerSwitch;
public GameObject[] Wheels;
public GameObject Player;
public GameObject CharactersGroup;
public GameObject Wheel;
void Start () {
PowerSwitch = this.gameObject.GetComponent<Animator> ();
}
void Update () {
if (PowerOff == false) {
foreach (GameObject obj in Wheels) {
obj.gameObject.transform.Rotate (0, 0, Time.deltaTime * -RotationSpeed);
}
} else if (PowerOff == true)
{
foreach (GameObject obj in Wheels) {
obj.gameObject.transform.Rotate (0, 0, 0);
}
}
}
void OnCollisionEnter(Collision other)
{
Debug.Log ("Colliding with wheel platform."); //Doesn't detect anything.
if (other.gameObject.tag == "Player" && PowerOff == true)
{
PowerSwitch.SetBool ("Open", true);
PowerOff = false;
} else if(other.gameObject.tag == "Player" && PowerOff == false)
{
PowerSwitch.SetBool ("Open", false);
PowerOff = true;
}
}
void OnTriggerStay(Collider other)
{
if (other.tag == "Player") {
other.transform.parent = Wheel.transform;
Debug.Log ("Colliding with wheel platform."); //Doesn't detect anything.
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Player")
{
other.gameObject.transform.parent = CharactersGroup.transform;
}
}
}
(I've also tried referencing the Player as a public GameObject and parent it that way, also didn't do the trick).
Huge thanks to anyone who bothers to read this! :) :) :)
Answer by Bill-Sansky · Aug 07, 2017 at 12:48 PM
If your code is on the rotating platform, you will want it to have a Rigidbody in order for the event to be called, a collider is not enough.
Your answer
Follow this Question
Related Questions
Collisions in Editor Test Runner 0 Answers
Check for collision ONCE 0 Answers
Problem with DontGoThroughThings script 0 Answers
Collider seems bigger than it is 0 Answers
Help With NavMesh 0 Answers