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 make push and pull the Box by the player (3D) in 4 directions ?

Discussion in 'Getting Started' started by Avenged90x, Dec 1, 2017.

  1. Avenged90x

    Avenged90x

    Joined:
    Sep 6, 2017
    Posts:
    35
    Hi!

    I'm working on simply game in unity 3D with FPP movement of player (but Y axis of camera movement is locked, you can look around using mouse only in X axis). The player movement and footsteps sounds components - everything from Unity Standard Assets : First Person Controller , so there is many options like walking (setting speed), running, jumping, etc. I want to make push and pull the box functionality, but don't know how.. I tried just adding rigidbody to the box and reducing the mass of the box and yes... it moves while I'm walking into the box but the movement of the box after collision by the player is very uncontrolled and unpredictible.. Box moves most often when hitted in the corner edge, not in the flat surface on one of the sides.. And when I came to the box to the flat surface, turn left and move little forward - the box moves with impetus forward although I did'nt pushed it.. So I think this is wrong way to do that :D

    I want to make simply push and pull :D.. For example, the box has 4 flat surfaces / sides. When the player came to the box at 1 side and holding down shift, or ctrl and moves straight forward - the box should move pinned to the player in the same direction. When player stops - the box stops too. When holding down another key 'alt' or something like that, and going back - the box should follow the player in the same direction as player moving like "pulling.

    I was thinking about using "parent" to another object, but I made something wrong and it doesn't work ...

    Any solutions, ideas :) ??

    Thanks!
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Parenting the box transform to the player (while the "grab" key is held) is a reasonable idea. That will work, and should even do stuff like prevent your character from pushing the box through walls.

    (I can think of other ways to do it, but they would require you do all that collision-handling stuff yourself.)
     
  3. Avenged90x

    Avenged90x

    Joined:
    Sep 6, 2017
    Posts:
    35
    Okey, but how to make the parenting the box correctly? It should be in script of player or box?
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Either one. Or in some other script entirely. It doesn't matter — code it in whatever way seems simplest and clearest to you.
     
  5. Avenged90x

    Avenged90x

    Joined:
    Sep 6, 2017
    Posts:
    35
    Ok, I tried parenting the box. It works, but when I go into the wall - I can put the box in the wall.. because while the player is the 'parent' there is only player's collider detection.. like ignoring the box collider.. How to solve that ? I tried very, very different way :D like:
    -added a first person controller movement to the box xD and dectivate it at the start. Than, when the player hits/collide with the box - activate box movement and deactivate player movement + set the player as a child of the box :D So its weird, but works pretty fine.. But as you can imagine, there was no possibility to rotate.. I could only move forward/backwrd , left and right but without following the camera direction. So.. its not very good idea - but in this case, the collisions with walls and other objects were working perfectly...

    I tried add another collider to my player and was thinking about activate it and deactivate from code, but how to make these collider to work in this first pperson ??
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I don't have much experience with Unity's first-person controller. I would have expected it to make use of any colliders that are parented under the GameObject the script is attached to, but who knows?

    You can always ditch that controller and write your own; then at least you will understand what it does and how it works.
     
    Opricanm likes this.