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

Snapping Object While Following Player?

Discussion in 'Scripting' started by ImKodii, Oct 5, 2018.

  1. ImKodii

    ImKodii

    Joined:
    May 18, 2017
    Posts:
    5
    Hey i was wondering if there is any way i can make something follow the player but it like snaps using unitys grid. Heres an example on what i need it for:

    https://gyazo.com/76653feb2d5dcd32af1de2f26472ef50

    This is me moving and if i place a block on my grid it places it where a cube is but above it so its like placing it on the ground. But if i move it moves the ground also so it doesnt make it snap good together as show here
    https://gyazo.com/d6ee87fbd921a9e7eed78802665d46aa

    And i need the grid to continue following the player. If anyone could help that would be amazing!
     
  2. FernandoHC

    FernandoHC

    Joined:
    Feb 6, 2018
    Posts:
    338
    One way to do it is making the movable object being a child of the grid, just to anchor its movement.

    In order to snap to grid position, you have to write your code in a way that it knows the size of each grid cell and when changing the object's transform.position, round the value to be multiple of the grid cell size.

    Now, the second issue you have is that you have 2 snaps possibilities, after you place objects in the world, so in this case I would snap the grid itself to be on the same cell size position of the nearest object placed the mouse/touch is. Snapping the grid to it, and having the grid child objects follow the cell size position should in theory make both work together.

    Please keep in mind I'm not considering height in this description, and I'm not predicting other possible issues there might be in, so let us know how you progress as I'm curious myself if this would work.
     
    ImKodii likes this.
  3. Kobaltic1

    Kobaltic1

    Joined:
    Jan 22, 2015
    Posts:
    183
    Make an object. Make the grid a child of that object. Align the grid up manually to the object. Now have the object follow your player but when you set the transform.position of your following object round it to the nearest int. Use round to int. This will remove the float from the movement and snap it to position.

    https://docs.unity3d.com/ScriptReference/Mathf.RoundToInt.html
     
    ImKodii likes this.
  4. ImKodii

    ImKodii

    Joined:
    May 18, 2017
    Posts:
    5
    @Kobaltic1 Do you know how i could go about haneling this with my current script it uses for following the player?

    "
    public class PlayerFollow : MonoBehaviour {

    public Transform player;
    public float smoothSpeed = 0.125f;
    public Vector3 offset;

    void FixedUpdate () {
    Vector3 desiredPos = player.position + offset;
    Vector3 smoothedPos = Vector3.Lerp(transform.position, desiredPos, smoothSpeed);
    transform.position = smoothedPos;
    }
    }"
     
  5. Kobaltic1

    Kobaltic1

    Joined:
    Jan 22, 2015
    Posts:
    183
    This is untested and I did it off the top of my head but should be correct. Change your last line to:

    Code (CSharp):
    1.  
    2. transform.position.x = Mathf.RoundToInt(smoothedPos.x);
    3. transform.position.y = Mathf.RoundToInt(smoothedPos.y);
    4. transform.position.z = Mathf.RoundToInt(smoothedPos.z);
    5.  
    6.