Search Unity

  1. How can our website serve you better? Give us your feedback. Take our survey and let us know.
    Dismiss Notice

Just need simple help

Discussion in 'Scripting' started by jermaine, Dec 28, 2009.

  1. jermaine

    jermaine

    Joined:
    Feb 10, 2009
    Posts:
    28
    :oops:
    Ok I know this is probably a simple solution to this. And I tried to solved myself.. But I need some help before my head explodes, or I have a stroke. I more of a graphics guy....

    I working on very simple collisons.
    Basically I'm using the "First Person Controller" for the player
    I have added a simple cube(From Unity). I added Rigidbody to the cube.
    Basically I am trying to get it when so that when the player collides with the box. A message would display in the debug log.

    I have written this script (javascript) and added it to the cube.
    Code (csharp):
    1.  
    2. function OnCollisionEnter(collision:Collision){
    3. if (collision.gameObject.tag == "Player")    
    4. {
    5. print ("yay it works);
    6. }
    7. }
    8.  
    I get no errors, but I also get no display in the console/debug log. So basically nothing is happening. Is their something wrong with the code? Can I get some help please?
     
  2. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Just a quick guess, but it looks like you're missing a parenthesis:

    Code (csharp):
    1. function OnCollisionEnter(collision:Collision){
    2.   if (collision.gameObject.tag == "Player")  
    3.     {
    4.       print ("yay it works");  // missing parenthesis here
    5.     }
    6. }
     
  3. jermaine

    jermaine

    Joined:
    Feb 10, 2009
    Posts:
    28
    ty for the reply..
    Yeah the parenthesis is there, that just a mistype on my part sorry..

    any help I been searching, i just cant find a solution...
     
  4. Wox

    Wox

    Joined:
    Dec 14, 2009
    Posts:
    93
    Hi! maybe this can help.


    Character Controller:
    http://unity3d.com/support/documentation/Components/class-CharacterController.html
    "The Controller does not react to forces on its own and it does not automatically push Rigidbodies away."


    Some code i found somewhere some time ago!

    Code (csharp):
    1.  
    2.  
    3. //CharacterCollider.js
    4.  
    5. // this script pushes all rigidbodies that the character touches
    6.  
    7. var pushPower = 2.0;
    8. function OnControllerColliderHit (hit : ControllerColliderHit)
    9. {
    10.  
    11. var body : Rigidbody = hit.collider.attachedRigidbody;
    12. //print("START");
    13. var a_layer = hit.collider.gameObject.layer;
    14.  
    15. if (a_layer != 8 )
    16. return;
    17.  
    18. // no rigidbody
    19. if (body == null || body.isKinematic)
    20. return;
    21.  
    22. // We dont want to push objects below us
    23. if (hit.moveDirection.y < -0.3)
    24. return;
    25.  
    26. // Calculate push direction from move direction,
    27. // we only push objects to the sides never up and down
    28. var pushDir = Vector3 (hit.moveDirection.x, 0, hit.moveDirection.z);
    29.  
    30. // If you know how fast your character is trying to move,
    31. // then you can also multiply the push velocity by that.
    32.  
    33. var p =  (pushPower-body.mass);
    34. print(p);
    35. if (p < 0 ) pr=0;
    36.  
    37.  
    38. // Apply the push
    39. body.velocity = pushDir * p;
    40. }
    41.  
    42.  

    Credists to the maker whoever you are :D
     
  5. jermaine

    jermaine

    Joined:
    Feb 10, 2009
    Posts:
    28
    thanks to everyone I figured out the problem.
    I added a box collider to the Player, and now it works.

    For some reason I was assuming that I only needed character controller, but I guess I was wrong.

    I knew it was something simple lol.

     
unityunity