Search Unity

boundary area for the player when the camera moves

Discussion in 'Physics' started by Fressno, Sep 11, 2019.

  1. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    Hello friends.
    So, im trying to make a boundary area(what ever will work for my game).
    I would prefer a "physical" area(like a collider or something i can attach as a gameobject to the camera.
    The thing is that this is going to be a 3d shoot em up, just like Sine Mora.
    But i need help finding a way to keep the player inside an confined area as the camera and the player is moving around the level. i dont want the level to move towards a static playing area.
    I could take the easy way and just box in the player with colliders, but i guess im going to see alot of twitching when the player is "pushed" forward, or stopped against a collider wall.
    And i guess that it might be a leak thru a collider when it doesnt do its job and let the player thru bcus it cant update fast enough.
    So, what is your suggestions?
    I need to use all directions, but mostly X,Y, but if im going to make cinemachine angles i might need the Z axis.
     
  2. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    i figured it out.
    i just put the player as a child of the camera. using the cameras local 0.0.0 and clamp the players movement there with this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using Rewired;
    4.  
    5. public class PlayerMoveRewired: MonoBehaviour
    6. {
    7.     public float xMin;
    8.     public float xMax;
    9.     public float yMin;
    10.     public float yMax;
    11.  
    12.     public int playerId = 0;
    13.  
    14.  
    15.     public float moveSpeed = 3.0f;
    16.  
    17.  
    18.     private Player player;
    19.     private Vector3 moveVector;
    20.  
    21.     void Awake()
    22.     {
    23.         player = ReInput.players.GetPlayer(playerId);
    24.     }
    25.  
    26.     void Update()
    27.     {
    28.         ProcessInput();
    29.         transform.position = new Vector3(Mathf.Clamp(transform.position.x, xMin,xMax),
    30.             Mathf.Clamp(transform.position.y, yMin, yMax),transform.position.z);
    31.     }
    32.  
    33.  
    34.     private void ProcessInput()
    35.     {
    36.         transform.Translate(moveSpeed*player.GetAxis("Move Horizontal") * Time.deltaTime, moveSpeed*player.GetAxis("Move Vertical") * Time.deltaTime,0f);
    37.     }
    38. }
    worked like a charm.

    thanx Alexander Zotov for your short and precise explanation.
     
  3. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    realised i had to make it a localposition.
    code update:
    Code (CSharp):
    1.         transform.localPosition = new Vector3(Mathf.Clamp(transform.localPosition.x, xMin, xMax),
    2.         Mathf.Clamp(transform.localPosition.y, yMin, yMax),Mathf.Clamp(transform.localPosition.z,1,1));
    3.     }
     
    Last edited: Sep 12, 2019