Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to stop player from clipping through objects?

Discussion in 'Physics' started by serbusfish, Mar 18, 2017.

  1. serbusfish

    serbusfish

    Joined:
    Dec 27, 2016
    Posts:
    247
    This is undoubtedly common knowledge but i'm new to Unity so haven't covered it yet. How do you make an object not pass through another object? The player is flying a spaceship in my game but I dont want them to pass though certain objects such as buildings, I just want them to actually collide with it and stop moving. Ive messed with colliders but no matter what I do the ship always passes through the other objects.
     
    SSpecks likes this.
  2. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    823
    How are you moving the spaceship? Are you using a rigidbody for it? (https://docs.unity3d.com/ScriptReference/Rigidbody.html)

    If the spaceship is fast also set the collision detection (in the rigidbody) to continuous dynamic. Move the player not with transform.position but with rigidbody.moveposition or even better rigidbody.addforce.
    Make sure the spaceship has a collider and the building has a collider, Then everything should work out.
     
  3. serbusfish

    serbusfish

    Joined:
    Dec 27, 2016
    Posts:
    247
    Yes the ship is moved via the rigidbody, although mine uses rigidbody.velocity. This is the code:

    Code (csharp):
    1.  
    2. float moveHorizontal = Input.GetAxis("Horizontal");
    3. float moveVertical = Input.GetAxis("Vertical");
    4.  
    5. Rigidbody rb = GetComponent<Rigidbody>();
    6. Vector3 movement = new Vector3( 0.0f, moveVertical, moveHorizontal);
    7. GetComponent<Rigidbody>().velocity = movement * speed;
    8.  
     
  4. serbusfish

    serbusfish

    Joined:
    Dec 27, 2016
    Posts:
    247
  5. Magnas94

    Magnas94

    Joined:
    Jun 8, 2016
    Posts:
    17
    @serbusfish Your rigidbody code is perfect , so it seems the problem is with colliders, check following things :

    1. Does your player(spaceship) has any collider attached to it ? if yes , do make sure the collider is not trigger.
    2. Check the same point for buildings , also try adding Rigidbody to buildings and set them to kinemetic.
     
  6. serbusfish

    serbusfish

    Joined:
    Dec 27, 2016
    Posts:
    247
    Thank you, the collider on my player object was set as a trigger, now the ship successfully bumps into building without clipping.

    However, another issue has now presented itself. The building is part of a scrolling background, and if it bumps into the player in a certain way it can flip the player ship so its no longer facing the right direction. I tried locking the rotation of the players rigid body but it didnt help.
     
  7. serbusfish

    serbusfish

    Joined:
    Dec 27, 2016
    Posts:
    247
  8. Magnas94

    Magnas94

    Joined:
    Jun 8, 2016
    Posts:
    17
    @serbusfish
    Glad to know my solution worked , So , your spaceship rotates after bumping into buildings ? Here are the few things you can try:

    1. Make sure all the rotations are constrained under rigidbody properties of spaceship.
    2. You can call OnCollisionExit() on spaceship to ensure that spaceship's rotations are reset after its collision with buildings

    E.g,.:

    void OnCollisionExit(Collision col)
    {
    if(col.collider.tag == "Building")
    {
    transform.localEulerAngles = new Vector3(0,0,0);
    }
    }
     
  9. serbusfish

    serbusfish

    Joined:
    Dec 27, 2016
    Posts:
    247
    Thanks, I tried this but it doesn't work, the ship still gets flipped and stuck in the wrong position.
     
  10. Magnas94

    Magnas94

    Joined:
    Jun 8, 2016
    Posts:
    17
    @serbusfish Are you using box colliders with buildings and the ship ??
     
  11. serbusfish

    serbusfish

    Joined:
    Dec 27, 2016
    Posts:
    247
    I have a box collider building and a capsule collider on the ship.