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. Dismiss Notice

Move object in relation to player position

Discussion in 'Editor & General Support' started by KrisM, Jun 15, 2019.

  1. KrisM

    KrisM

    Joined:
    Oct 18, 2014
    Posts:
    28
    This is a relatively simple concept that's a little complicated to explain.

    The game world is essentially (outer) space. I'm trying to set up bounds for the map by implementing a barrier that players cannot pass through. When the player gets close to the edge of the spherical map, a visual barrier will appear to represent a collision with the 'wall'.

    I've set up a particle effect for this barrier. But I need to position it so it's always on the other side of the player; a specific distance from the center of the map.

    Unfortunately, I'm not familiar enough with moving objects (in relation to player position) to know where to start with this. Any help would be appreciated.
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,869
    The answer changes a tiny bit depending on 2D or 3D but the basics are pretty much the same.

    The player is represented by a game object. Every game object has a transform. The transform has a world-space position and rotation for that game object.

    You can define the center of the map as world coordinates (0, 0, 0), or maybe you have some other object such as an empty GameObject marking the center of the map. That GameObject also has a world-space position and rotation in its transform.

    If you want to calculate the direction from the center of the map toward the player, that's
    Vector3 direction = player.transform.position - center_of_map_marker.transform.position
    .

    If you want to calculate this same direction, but with a vector exactly one unit long regardless of the distance they are apart, then follow the previous with
    direction = direction.normalized;
    .

    If you want to calculate a point that is exactly 2 units farther from the center of the map than the player is, then
    Vector3 boundary = player.position + 2f * direction;


    If you want to put a wall at that spot, facing away from the center of the map, then make a cube, squish the Z blue scale down to make it a wall, and refer to it as
    public Transform wall;
    in your script. Now your script can do
    wall.position = boundary; wall.rotation = Quaternion.LookAt(direction, -Physics.gravity);
    This points the blue local arrow of the wall object away from the player and center. If you use your particle effect as a wall, the rotation may need to be arranged differently.
     
    KrisM likes this.
  3. KrisM

    KrisM

    Joined:
    Oct 18, 2014
    Posts:
    28
    I think I'm part way there, and thank you for the help.
    The sphere radius is 500 (for the 'world space'), so the barrier needs to be present at that location. It needs to swivel/move according to the player position so that at any point the player would hit the edge of the map, that's where the wall is.

    I've used:
    Code (CSharp):
    1. direction = PlayerObject.transform.position - transform.position;
    2. direction = direction.normalized;
    3.  
    4. boundaryPos = transform.position + 500f * direction;
    5. barrierEffect.SetVector3 (Barrier_Pos, boundaryPos);
    While I can see the barrier position is changing in relation to player's location, it's not working in the right way. I think the problem is with the "500f * direction", which seems illogical in relation to how I understand 'direction'. However, it's highly likely that my understanding is flawed; which is contributing to my struggles in getting this working.
     
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,869
    Sounds like the wall should be 500 meters from the center in the direction of (and past) the player. The player only defines the direction, then. So
    center.transform.position + 500f * direction
    instead.
     
  5. KrisM

    KrisM

    Joined:
    Oct 18, 2014
    Posts:
    28
    That's right... the player defines the direction, and the distance is derived from the world space 0,0,0 (or the center of the object).
    Unfortunately,
    center.transform.position + 500f * direction
    didn't work. "error CS0103: The name 'center' does not exist in the current context".
    I'm assuming that 'center' in your example refers to a gameObject, which is what the script is attached to.
     
  6. KrisM

    KrisM

    Joined:
    Oct 18, 2014
    Posts:
    28
    Quick update; I've found the solution. The particle effect (VFX Graph) instantiated the particles in local space; distorting the Vector3 coordinates. Switching this to 'world' space resolved the issue.

    The original solution offered by Halley works; thanks again.
     
    halley likes this.