Search Unity

GameObject Placement and Movement Design

Discussion in 'Game Design' started by tansvanio, Feb 9, 2016.

  1. tansvanio

    tansvanio

    Joined:
    Dec 16, 2012
    Posts:
    12
    Greetings.

    I have been working on a system where a player presses a key and a prefab spawns in front of him and can be moved with controls.
    I wonder if you guys have any ideas on the best way to implement this.

    So far I have considered several possibilities.

    As far as spawning the object goes, I thought of simply instantiating the prefab a couple coordenates away from the player but this is not very great becouse it does not take into account the player camera, so I thought of a better way.
    This way consists of casting a raycast that hits the terrain and returns a position. The object is then spawned at this position.

    How would you guys proceed from here to implement the movement of the prefab until it can be locked to its position?
    I tried to translate the prefab to each new position of the hit.position from the raycast, but this resulted in a very buggy aftermath (the prefab would come flying towards the player controller for some reason).

    So these are my thoughts, what are yours?

    PS: My vision of this system is that of a general survival game where the player can craft objects and place them.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Is this a first-person or third-person view? Does the player move around with the keyboard alone, or also use the mouse?
     
  3. tansvanio

    tansvanio

    Joined:
    Dec 16, 2012
    Posts:
    12
    First-Person. Player uses keyboard and mouse to move arround.
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    OK, then I would suggest you simply spawn the object in front of the player. Cast a ray to see if it hits anything, but only within a few meters; if not, then put the object at the end of that ray (i.e. a fixed distance away). As the player moves and turns with the "held" object, continue to update the object via the same method. When they press a key or whatever to let go, then drop it at that point.
     
  5. tansvanio

    tansvanio

    Joined:
    Dec 16, 2012
    Posts:
    12
    Yes! I ended up implementing it somewhat like that. Updating the objects position to the raycast hit point. However it was very important for that raycast to ignore the object itself, otherwise it would move itself towards me.
     
    Last edited: Feb 11, 2016
    JoeStrout likes this.