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

Clamp 3rd person camera rotation on the y axis

Discussion in 'Scripting' started by Vexer, Jul 24, 2018.

  1. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    Hey Unity!

    I have been trying to clamp my 3rd person camera now for over 5 hours I tried to use bits of other scripts from other people and they gave the exact same problem as my script gives for some reason I keeps saying:
    Cannot implicitly convert type 'UnityEngine.Quaternion' to 'UnityEngine.Vector3'

    Before you comment "It literally says what ur doing wrong!!" I get that but I don't know how to fix it!!!

    So I would appreciate any help thank you guys!!

    Code:
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerFollow : MonoBehaviour {
    7.  
    8.     public Transform PlayerTransform;
    9.  
    10.     private Vector3 _cameraOffset;
    11.  
    12.     public Rigidbody rb;
    13.  
    14.     public bool LookAtPlayer = false;
    15.  
    16.     public bool RotateAroundPlayer = true;
    17.  
    18.     public float RotationsSpeed = 5.0f;
    19.  
    20.     [Range(0.01f, 1.0f)]
    21.     public float SmoothFactor = 0.5f;
    22.  
    23.     // Use this for initialization
    24.     void Start () {
    25.         _cameraOffset = transform.position - PlayerTransform.position;
    26.     }
    27.    
    28.     // FixedUpdate is called after Update methods
    29.     void FixedUpdate () {
    30.  
    31.         if (RotateAroundPlayer)
    32.         {
    33.                 Quaternion camTurnAngleRightandLeft = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * RotationsSpeed, Vector3.up);
    34.                 Quaternion camTurnAngleUpandDown = Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * RotationsSpeed, Vector3.right);
    35.  
    36.             _cameraOffset = camTurnAngleUpandDown * camTurnAngleRightandLeft * _cameraOffset;
    37.         }
    38.  
    39.         Vector3 newPos = PlayerTransform.position + _cameraOffset;
    40.  
    41.         transform.position = Vector3.Slerp(transform.position, newPos, SmoothFactor);
    42.  
    43.         if (LookAtPlayer || RotateAroundPlayer)
    44.         {
    45.             transform.LookAt(PlayerTransform);
    46.         }
    47.     }
    48. }
    49.  
    -Vexer
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    One good way to setup a 3rd person camera is with a camera rig, which consists of 3 parts.
    1 empty game object, a child of that (another empty) , and a child of that (the camera itself).
    You can manipulate the rotation of X/Y - 1 on each of the empties.

    As for how to clamp.. simply store the angle as a float. Adjust it based on input, clamp it, and then apply to the localRotation with Quaternion.Euler.

    Setting up the camera, if you use this approach. The root empty is always at the same position as the character. The first child can be raised up on the local y position, and the camera itself can be set on the local z position axis. This also means the camera is always looking at the character, without 'LookAt'.
     
  3. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    How do i store the angles as a float?
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    In a float variable..

    Say you had a cube and this code:
    Code (csharp):
    1. float xRot = 0;
    2.  
    3. void Update() {
    4.    xRot += Input.GetAxis("Mouse Y"); // could add some more adjustment here.
    5.    xRot = Mathf.Clamp(xRot, -30, 30);
    6.  
    7.    transform.rotation = Quaternion.Euler(xRot, 0, 0);
    8.  }
    You could try that, as an example.