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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Looking for an example "locking" and "unlocking" the first person view

Discussion in 'Scripting' started by live_xi, Nov 16, 2015.

  1. live_xi

    live_xi

    Joined:
    Oct 21, 2015
    Posts:
    12
    Hi Folks
    am newbie using unity3D please help me. the following scenario under perspective of first person
    1. there is a 3D workbench object in scene
    2. the first person clicks on the workbench
    3. the camera flies to a certain perspective and enter "workbench" mode (meaning player cannot change perspective, the camera has been locked)
    4. player can finish some assembly by using mouse under fixed perspective
    5. after player has finished his work on "workbench" he can exit the "workbench" mode and the camera is "unlocked" so that he can move freely again.

    Like the game "Dead Space" ...:)

    any clue? i do have a first person in scene by using standard asset but no idea how to lock and unlock the view etc...thanks in advance.
     
  2. IroncladEarl

    IroncladEarl

    Joined:
    Jul 3, 2013
    Posts:
    87
    There are a million ways to skin a cat (I think thats how that saying goes). One way that I would use, is to include it in my CameraController script with some variables exposed for configuration. I've creating a working example below

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5.  
    6. public class CameraController : MonoBehaviour {
    7.  
    8.     //
    9.     // General
    10.     //
    11.     public float cameraMoveSmoothness = 3f;
    12.  
    13.     //
    14.     // Player Camera Specific
    15.     //
    16.     private Transform playerTransform; // The player transform (This is set in the Start() method)
    17.     public Vector3 cameraLookOffset = Vector3.zero; // Where is the camera looking (relative to player origin)
    18.     public Vector3 cameraRelativePosition = new Vector3 (0,1,-5); // Where is the camera positioned (relative to player origin)
    19.  
    20.     //
    21.     // Workbench Camera Mode Specific
    22.     //
    23.     public bool workbenchModeActive = false; // Is workbench mode active? This is set using the method SetWorkbenchMode
    24.     public Transform workbenchTransform; // Workbench gameObject transform (This is set from the code depending on which workbench you are at
    25.     public Vector3 workbenchCameraRelativePosition; // Workbench camera position (relative to workbench origin)
    26.     public Vector3 workbenchCameraLookOffset; // Where is the workbench camera looking (relative to workbench origin)
    27.  
    28.  
    29.     void Start () {
    30.         playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
    31.     }
    32.  
    33.     void Update () {
    34.  
    35.         // If the Workbench Mode is not active, continue player tracking with camera
    36.         if (!workbenchModeActive) {
    37.  
    38.             // Lerp the camera towards the desired target position (from current pos to player pos + offset)
    39.             Vector3 moveTowardsPosition = Vector3.Lerp(
    40.                 transform.position,
    41.                 playerTransform.position+(playerTransform.rotation*cameraRelativePosition),
    42.                 cameraMoveSmoothness*Time.deltaTime);
    43.  
    44.             transform.position = moveTowardsPosition; // Apply movement to camera
    45.             transform.LookAt( playerTransform.position + cameraLookOffset); // Make the camera look at the player position + look offset
    46.  
    47.         }
    48.         // If Workbench mode is active, set camera to workbench position
    49.         else if(workbenchModeActive) {
    50.  
    51.             // If there is a workbench transform set
    52.             if(workbenchTransform != null) {
    53.  
    54.                 // Lerp the camera towards the desired target position (from current pos to workbench pos + offset)
    55.                 Vector3 moveTowardsPosition = Vector3.Lerp(
    56.                     transform.position,
    57.                     workbenchTransform.position+(workbenchTransform.rotation*workbenchCameraRelativePosition),
    58.                     cameraMoveSmoothness*Time.deltaTime);
    59.  
    60.                 transform.position = moveTowardsPosition; // Apply movement to camera
    61.                 transform.LookAt(workbenchTransform.position + workbenchCameraLookOffset); // Make the camera look at the workbench position + look offset
    62.  
    63.             }
    64.         }
    65.  
    66.     }
    67.  
    68.     public void SetWorkbenchMode(bool isActive, Transform workbench) {
    69.         switch (isActive) {
    70.             case true:
    71.                 workbenchModeActive = true;
    72.                 workbenchTransform = workbench;
    73.                 break;
    74.             case false:
    75.                 workbenchModeActive = false;
    76.                 workbenchTransform = null;
    77.                 break;
    78.         }
    79.     }
    80. }
    81.  

    Using this you now have an adjustable camera script that will allow you to add more functionality. This is the result of the above script: http://imgur.com/l2NQHG0
     
    MD_Reptile and live_xi like this.
  3. live_xi

    live_xi

    Joined:
    Oct 21, 2015
    Posts:
    12
    great