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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Make Game Object not move when hit

Discussion in 'Editor & General Support' started by Bloodember, Sep 21, 2014.

  1. Bloodember

    Bloodember

    Joined:
    Mar 30, 2014
    Posts:
    41
    OK, I've been trying to figure this out for a few days now and I haven't found any answers. I'm making a space shooter, so all gameobjects have gravity unchecked. I'm trying to make an obstacle, that when the player hits it you just stop, the obstacle doesn't move. I have a rigidbody, I have the freeze position Z, and all the Freeze Rotation checked, but when you bump into it, it still moves. I also have mass at 1, and drag, and angular drag at Infinity. I do have a box collider.

    Now what am I doing wrong? Do I need a script that makes the obstacle not move. I have made one that just states in the Start function to freeze all constraints, which doesn't work, when you hit it you just go right through it. Which is another problem, when the player hits it, it moves and you go right through it also. I want the player to stop when they hit it, like a wall.

    Thanks for the help.
     
  2. kha_erp

    kha_erp

    Joined:
    Sep 21, 2014
    Posts:
    13
    Think you have to delete the rigidbody ..
     
  3. Bloodember

    Bloodember

    Joined:
    Mar 30, 2014
    Posts:
    41
    Tried it, didn't work, you just go through it.
     
  4. kha_erp

    kha_erp

    Joined:
    Sep 21, 2014
    Posts:
    13
    Try this in JavaScript and assign the script to the game object :
    -----------------------------------
    var currentPos :Vector3 ;

    function Start () {
    currentPos = gameObject.transform.localPosition ;
    }

    function Update () {
    gameObject.transform.position = currentPos;
    }
    -----------------------------------
     
  5. Bloodember

    Bloodember

    Joined:
    Mar 30, 2014
    Posts:
    41
    OK, I tried it and it makes my object disappear., I also converted it to C# and it does the same thing.
     
  6. kha_erp

    kha_erp

    Joined:
    Sep 21, 2014
    Posts:
    13
    it hasn't anything to do with object activation, make sure that your camera see this object and hit play .. i tried this script with rigidbody and gravity off, and it's working with me
     
  7. Bloodember

    Bloodember

    Joined:
    Mar 30, 2014
    Posts:
    41
    OK, it is working it's just moving to a different spot when I hit play. Not sure why it's doing that.

    Edit: Ok got it, but the player just moves through it, there is no collision.
     
  8. kha_erp

    kha_erp

    Joined:
    Sep 21, 2014
    Posts:
    13
    if it's not, add a collider to that object
     
  9. Bloodember

    Bloodember

    Joined:
    Mar 30, 2014
    Posts:
    41
    It has a collider.
     
  10. kha_erp

    kha_erp

    Joined:
    Sep 21, 2014
    Posts:
    13
    make sure that "Is Trigger" is not checked for the collider
     
  11. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,507
  12. Bloodember

    Bloodember

    Joined:
    Mar 30, 2014
    Posts:
    41
    That didn't work either, the player still passes right through it.
     
  13. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You said you've already tried to set all contraints and it didn't work and isKinematic doesn't work either? Could you provide a simple example or make a screenshot of your setup, since these both methods should actually work unless i completely misunderstood everything in this thread. I tested them both and everything worked just fine.
     
  14. Bloodember

    Bloodember

    Joined:
    Mar 30, 2014
    Posts:
    41
    Ok, I figured it out. It's my Player Movement script. I'll just have to rewrite it.
     
  15. he110_friend

    he110_friend

    Joined:
    Feb 21, 2020
    Posts:
    1
    Can u please share how you solved it... I'm facing the same problem with my unity AR project,
     
  16. askaroe

    askaroe

    Joined:
    Mar 11, 2023
    Posts:
    3
    Hello! Make sure that you've added rigidbody component both to player and obstacle