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

Question Camera Controller with Mouse and Camera Follow Object

Discussion in 'Scripting' started by emirkurt2001, Dec 7, 2020.

  1. emirkurt2001

    emirkurt2001

    Joined:
    Dec 3, 2020
    Posts:
    2
    Hi! I have 2 scripts about Camera Controller with Mouse and Camera Follow Object. When I use this 2 scripts my Camera Controller with Mouse's Mouse Y input cant rotate Main Camera's x .

    This is CameraController :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraController : MonoBehaviour
    6. {
    7.     public float cameraSmoothingFactor = 1;
    8.     public float lookUpMax = 60f;
    9.     public float lookUpMin = -60f;
    10.     private Quaternion camRotation;
    11.  
    12.  
    13.    
    14.  
    15.     void Start()
    16.     {
    17.         camRotation = transform.localRotation;
    18.     }
    19.     void Update()
    20.     {
    21.         camRotation.x += Input.GetAxis("Mouse Y") * cameraSmoothingFactor * (-1);
    22.         camRotation.y += Input.GetAxis("Mouse X") * cameraSmoothingFactor ;
    23.  
    24.         camRotation.x = Mathf.Clamp(camRotation.x, lookUpMin, lookUpMax);
    25.  
    26.         transform.localRotation = Quaternion.Euler(camRotation.x, camRotation.y, camRotation.z);
    27.  
    28.         Debug.Log(camRotation.x);
    29.  
    30.     }
    31. }
    This is CameraFollow:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraFollow : MonoBehaviour
    6. {
    7.     public Transform PlayerTransform;
    8.     private Vector3 _cameraOffset;
    9.  
    10.     [Range(0.01f,1f)]
    11.     public float SmoothFactor = 0.1f;
    12.  
    13.     public bool LookAtPlayer = false;
    14.  
    15.     public bool RotateAroundPlayer = true;
    16.     public float RotationSpeed = 5.0f;
    17.     private void Start()
    18.     {
    19.         _cameraOffset = transform.position - PlayerTransform.position;
    20.         PlayerTransform.transform.position = PlayerTransform.transform.position;
    21.         PlayerTransform.transform.parent = PlayerTransform.transform;
    22.     }
    23.     private void LateUpdate()
    24.     {
    25.         if (RotateAroundPlayer)
    26.         {
    27.             Quaternion camTurnAngle = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * RotationSpeed, Vector3.up);
    28.  
    29.             _cameraOffset = camTurnAngle * _cameraOffset;
    30.         }
    31.         Vector3 newPos = PlayerTransform.position + _cameraOffset;
    32.         transform.position = Vector3.Slerp(transform.position, newPos, SmoothFactor);
    33.  
    34.         if (LookAtPlayer || RotateAroundPlayer)
    35.         {
    36.             transform.LookAt(PlayerTransform);
    37.         }
    38.     }
    39. }
    40.  
     
  2. emirkurt2001

    emirkurt2001

    Joined:
    Dec 3, 2020
    Posts:
    2
    help please
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    You probably need to use two separate transforms parented together to get what you want.

    The reason:

    - your CameraFollow script does a transform.LookAt() which drives rotation fully

    - your CameraController script also attempts to drive the local rotation

    Only one of these can win if those are the same transforms. Parent two transforms together to blend them.