Search Unity

Collision Issue

Discussion in 'Scripting' started by Muku, May 5, 2014.

  1. Muku

    Muku

    Joined:
    Jan 13, 2014
    Posts:
    13
    I'm this is an easy fix but I can't seem to find any info on it.
    I got my character to autorun but now he ignores the collision boxes of my static objects. I have an enemy that moves that I'm still able to hit and die from.

    Code (csharp):
    1. #pragma strict
    2.  
    3.  
    4. function Update () {
    5.     transform.Translate(Time.deltaTime * 5, 0, 0);
    6. }
    7.  
    Simple code as you can see. It's just attached to the character.
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    In order to collide without your static object you will need to add the OnCollisionEnter function here too. Also, don't forget to attach a rigidbody.
     
  3. Muku

    Muku

    Joined:
    Jan 13, 2014
    Posts:
    13
    I'm just using the character FPS controller script in unity so it has the rigidbody attached to it. What would the OnCollisionEnter look like if i were to add it to this script?
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Is it the Rigidbody FPS Walker script from the wiki or one script which comes with unity standard assets?

    Basically, you'd put the following code

    Code (csharp):
    1.  
    2. void OnCollisionEnter(Collision collision)
    3. {
    4.      // your code, i.e. check for a tag and then do something
    5. }
    6.  
     
  5. Muku

    Muku

    Joined:
    Jan 13, 2014
    Posts:
    13
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. function Update(){
    5.     transform.Translate(Time.deltaTime * 5, 0, 0);
    6. }
    7. void OnCollisionEnter(Collision){
    8. }
    9.  
    So this is how I entered it becuse if I added it up top as a void the autorun would stop working. Also it gives me an error saying "void" is not a valid macro.
    The FPS controller I'm using is the one included in the Unity assets.
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    My bad, you're using Unityscript/Javascript and i posted C#.

    Try:

    Code (csharp):
    1.  
    2. function OnCollisionEnter(collision : Collision)
    3. {
    4. // insert your code here
    5. }
    6.  
     
  7. Muku

    Muku

    Joined:
    Jan 13, 2014
    Posts:
    13
    No errors this time but for some reason it cancels the autorun feature. Reverts back to using WASD.

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. function OnCollisionEnter(collison : Collision){
    5.     transform.Translate(Time.deltaTime * 5, 0, 0);
    6. }
    7.  
     
  8. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Leave that code in Update(), put your code in OnCollisionEnter that you want to execute when a collision occurs.