How does OnCollisionEnter() work? I know what it’s meant to do, but what do you put in the brackets? How do you define what collisions to react to? None of the Unity tutorials explain this very well, and I need some help from the experts.:(
using UnityEngine;
using System.Collections;
public class jumpLift : MonoBehaviour {
public float jumpHeight;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Jumplift")
{
transform.Translate(0, 0, jumpHeight);
Debug.Log("Collider has hit");
}
}
}
But when my character moves over the jump lift, he doesn’t jump, nor does the debug log recognise that it’s been hit.
I wonder if any f you could help me with this. I have the script that’s meant to control a turret when a player moves behind the controls. Unfortunately, when I move over the collider, it doesn’t do anything. (It doesn’t even output the debug log, meaning that it hasn’t recognised the character), But it all seems to be ok. Here’s the script;
using UnityEngine;
using System.Collections;
public class CannonScript : MonoBehaviour {
private bool controlling;
// Use this for initialization
void Start ()
{
controlling = false;
}
// Update is called once per frame
void OnTriggerEnter (Collider other)
{
if(other == GameObject.FindWithTag("Player"))
{
Debug.Log("Comtrolling is true");
controlling = true;
}
}
void Update ()
{
if(Input.GetKey ("z"))
if (controlling == true)
{
Debug.Log("turning left is true");
transform.Rotate(0, -Time.deltaTime, 0);
}
if(Input.GetKey ("x"))
if (controlling == true)
{
Debug.Log("turning right is true");
transform.Rotate (0, Time.deltaTime, 0);
}
}
}
You’re trying to say if the collider is a gameobject which will always return null as other is defined as a collider.
If(other.tag == "Player")
Or
If(other == playerObj.collider)
Or
If(other.gameobject == playerObj)
Etc....
This is all very basic stuff you can find in the scripting references with no need for a post.
Do you have to use triggers anyway? I prefer to calculate the distance between objects.
How do you calculate the distance between objects? That would be really useful. Oh, and I do need it to be a collider, as if I calculated the distance, he would be able to control if from any side of the cannon, and I want him to only control the cannon in the specific area.
Ps, I prefer troubleshooting by talking to fellow humans, and not looking it up on the reference… Although I do use it when everyone gets sick of me