Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

void OnCollisionEnter

Discussion in 'Scripting' started by d3st1ny33, Apr 28, 2014.

  1. d3st1ny33

    d3st1ny33

    Joined:
    Apr 28, 2014
    Posts:
    5
    i can't get this collision detection to work, heres my script:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class movement : MonoBehaviour {
    5.     public int accSpeed;
    6.     public Vector3 force;
    7.     private Vector3 spawn;
    8.     public GameObject die;
    9.     void Start (){
    10.         spawn = transform.position;
    11.         }
    12.     void Update () {
    13.         force = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
    14.         rigidbody.AddForce (force * accSpeed);
    15.     }
    16.     void OnCollisionStay(Collision other){
    17.         if (other.transform.tag == "enemy") {
    18.                         Instantiate (die, transform.position, Quaternion.identity);
    19.                         transform.position = spawn;
    20.                 }
    21.     }
    22. }
    23.  
    /edit i just noticed im using OnCollisionStay but it dosnt work wit either
    /edit2 more info

    I have read that the collider sometimes needs a rigid body which i have, and has the isKinnematic trigger on. I dont want IsTrigger because then it wont collide.
     
    Last edited: Apr 28, 2014
  2. Suddan

    Suddan

    Joined:
    Jan 12, 2013
    Posts:
    21
    Script seems to be working, make sure you set the correct tag and check for typos (case sensitive)..
     
  3. d3st1ny33

    d3st1ny33

    Joined:
    Apr 28, 2014
    Posts:
    5
    i know, enemy is spelt lowercase forgot to add to op