Search Unity

Move position of a Gameobject by specific number during OnTriggerEnter2D

Discussion in 'Scripting' started by Plott, Jan 21, 2017.

  1. Plott

    Plott

    Joined:
    May 12, 2014
    Posts:
    94
    So here's the scenario, when the player enters this trigger, the game object (the camera) needs to just move its position back by 5.

    This script isn't attached to the camera, only a game object with a box collider on it.

    Im not sure if vector3.lerp makes sense to use, and I'm having issues writing its correctly. Here is my script so far.

    Code (CSharp):
    1. public class MoveCamera : MonoBehaviour {
    2.  
    3.         public GameObject _camera;
    4.  
    5.         // Use this for initialization
    6.         void Start () {
    7.        
    8.         }
    9.        
    10.         // Update is called once per frame
    11.         void Update () {
    12.        
    13.         }
    14.  
    15.         void OnTriggerEnter2D (Collider2D other) {
    16.             if (other.gameObject.tag == "Player") {
    17.  
    18.                 Debug.Log ("move camera to new position");
    19.  
    20.                 //right here i need to move the camera from its original position back 5 (original position is -10, so it would be -15)
    21.  
    22.             }
    23.         }
    24.  
    25.         void OnTriggerExit2D (Collider2D other) {
    26.             if (other.gameObject.tag == "Player") {
    27.  
    28.                 Debug.Log ("return to normal camera position");
    29.  
    30.                 // right here it would return to normal, but i want to be able to move the camera individually from this script.
    31.             }
    32.         }
    33.  
    the position of the camera needs to still be following the player, and still needs to return to its regional position after exiting. Any know what I should look into for this?
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @Plott

    Hi there,

    you already have nearly standard Unity trigger enter/exit boiler plate code...

    and you also have pseudo code for your camera movement.

    If you are clueless about how to move camera at this point, or any other object, I think you better check Unity Learning section / videos.

    In any case, you can manipulate any object by it's transform, so transform.rotation and transform.position of camera in this case.