Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Walking Through Walls?

Discussion in 'Scripting' started by DPGUnit, Apr 20, 2011.

  1. DPGUnit

    DPGUnit

    Joined:
    Mar 30, 2011
    Posts:
    26
    My character can walk through walls for some reason; the character has a script attached to it so when it collides with anything with a rigidbody is goes back to spawn is their a way round this. Maybe add a different type of physic property to the walls because i can not use rigidbody for that. Any help will be useful. Thanks in advance.

    - Jay
     
  2. Sargon_of_Akkad

    Sargon_of_Akkad

    Joined:
    Jan 16, 2011
    Posts:
    147
    You sure you don't want to do it that when it collides with something it checks a script on that object that holds a variable like 'DoesRespawn = 1' (or 0) and if the variable is 1 then run the respawn script? That way you don't have to worry about it colliding exclusively with rigidbodies, just any old collider would do.
     
  3. RingOfStorms

    RingOfStorms

    Joined:
    Oct 23, 2010
    Posts:
    584
    Maybe your wall has no collider? And make sure it is not checked as a trgger, otherwise objects will go through it.
     
  4. DPGUnit

    DPGUnit

    Joined:
    Mar 30, 2011
    Posts:
    26
    Sargon_of_Akkad - Can you elaborate on that please. Or give me some links thanks. Below is the code i`m using.
    Code (csharp):
    1. private var dead = false;
    2.  
    3. function OnTriggerEnter(other : Collider)
    4. {
    5.     if(gameObject.tag == "fallout")
    6.     {
    7.         dead = true;
    8.     }
    9. }
    10.  
    11. function LateUpdate ()
    12. {
    13.         if (dead)
    14.         {
    15.             transform.position = new Vector3(9.75,20.9,123.2);
    16.             dead = false;
    17.         }
    18. }
    19.  
    RingOfStorms - i`ve tried that doesn`t work i think it is because of my script
     
  5. CgRobot

    CgRobot

    Joined:
    Jan 4, 2011
    Posts:
    175
    That scripts appears to be unnecessarily long. How about something like this:

    Code (csharp):
    1.  
    2.  
    3. function OnTriggerEnter(other : Collider)
    4. {
    5.           if(other.tag == "fallout")
    6.           {
    7.                     transform.position =  new Vector3(9.75, 20.9, 123.2);
    8.           }
    9. }
    Note: this script has not been tested.

    I'm not sure, but I don't think you have to use "new" in "new Vector3." I think your problem was that you typed gameObject.tag instead of other.tag. Other was the variable that you assigned in the collision. It contains the object that you collided with. So if you type "other.tag" you get the tag of whatever object you collided with. When you typed "gameObject.tag" you accessed the tag on the game object the script is attached to. I hope this helps!

    --CgRobot
     
  6. DPGUnit

    DPGUnit

    Joined:
    Mar 30, 2011
    Posts:
    26
    Thanks for shorting my script but i am still able to walk through walls. I have 2 options
    - Remove walls and add gravity which i cann`t seem to do. I add a rigidbody and then i just fall through the floor. Does my floor need a rigidbody?
    -Use walls but fix walk through problem.
    I only spent 5 mins with the script provided because i was running out of time i will try again later thanks anyway
     
  7. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Your character has a script attached to it, that tells it that if it hits something that has a rigidbody... a wall should not have a rigidbody. ;)