Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Simple rotation of the camera, with the mouse, around the player

Discussion in 'Getting Started' started by Mister_Pyxel, May 10, 2017.

  1. Mister_Pyxel

    Mister_Pyxel

    Joined:
    May 4, 2017
    Posts:
    47
    Hi everybody,

    So, since yesterday, I try to make my main camera to turn around the player when I move my mouse.
    What I want to achieve is a camera system like the one on this video (at 05 mins 25 secs):


    So, what I want is:
    When the character don't move:
    - The camera can turn around the character following the mouse movement
    - The character do not rotate while the camera rotate around him​

    When the character move:
    - The camera is still able to rotate with the mouse movement
    - But the character is rotating at the same time so we can change direction just by moving the mouse​

    I hope my objectives are well explained.

    So to do all this, the first step is to make the camera rotate around the player by moving the mouse (so we don't speak about movement or anything).
    Right now I have a camera that follow the character while moving.

    I have look on internet and I have found many people asking this kind of question, I have tryed different scripts, but I didn't find one allowing me to turn around in 360° in axes X and Y around the player (I have found one allowing me to turn around the player but I wasn't able to look upside or downside).

    So I come here to ask you if somebody have any idea of how to do this.

    Right now, this is my script for the camera:
    Code (csharp):
    1.  
    2. //-----CameraFollow script-----\\
    3. //---Author:MrPyxel---\\
    4. //---Version:1.2---\\
    5. //---Update:10/05/2017 14:00---\\
    6.  
    7.  
    8. //-----Libraries-----\\
    9. using System.Collections;
    10. using UnityEngine;
    11.  
    12. //-----Script body-----\\
    13. public class CameraFollow : MonoBehaviour
    14. {
    15.     //-----Private variables -----\\
    16.     private Vector3 offset;
    17.  
    18.     //-----Public variables-----\\
    19.     [Header("Variables")]
    20.     public Transform player;
    21.    
    22.     [Space]
    23.     [Header("Position")]
    24.     public float camPosX;
    25.     public float camPosY;
    26.     public float camPosZ;
    27.  
    28.     [Space]
    29.     [Header("Rotation")]
    30.     public float camRotationX;
    31.     public float camRotationY;
    32.     public float camRotationZ;
    33.  
    34.     [Space]
    35.     [Range(0f, 10f)]
    36.     public float SensibilitéeX;
    37.     [Range(0f, 10f)]
    38.     public float SensibilitéeY;
    39.     public float turnSpeed = 3f;
    40.  
    41.     //-----Private functions-----\\
    42.     private void Start()
    43.     {
    44.         offset = new Vector3(player.position.x + camPosX, player.position.y + camPosY, player.position.z + camPosZ);
    45.     }
    46.  
    47.  
    48.     private void LateUpdate()
    49.     {
    50.         transform.position = player.transform.position + offset;
    51.     }
    52. }
    53.  
    So, as you can see I have some variables that I have keep from my test (the rotation part of the public variables), I have keep them because I think that might be usefull to rotate the camera (so I will not bother me to re-write them).

    So with this,do somebody know how to make the camera rotate(in axes X and Y) around the player when I move my mouse?

    Edit:
    I have succed! So thanks to this thread:
    https://forum.unity3d.com/threads/s...ound-player-using-non-parented-camera.331729/

    I have succed to make the camera turning around the player in X and Y axes, so for those who want to do this, here is my script:
    Code (csharp):
    1.  
    2. //-----CameraFollow script-----\\
    3. //---Author:MrPyxel---\\
    4. //---Version:1.5---\\
    5. //---Update:10/05/2017 22:20---\\
    6.  
    7.  
    8. //-----Libraries-----\\
    9. using System.Collections;
    10. using UnityEngine;
    11.  
    12. //-----Script body-----\\
    13. public class CameraFollow : MonoBehaviour
    14. {
    15.     //-----Privates variables-----\\
    16.     private Vector3 offset;
    17.  
    18.     //-----Publics variables-----\\
    19.     [Header("Variables")]
    20.     public Transform player;
    21.  
    22.     [Space]
    23.     [Header("Position")]
    24.     public float camPosX;
    25.     public float camPosY;
    26.     public float camPosZ;
    27.  
    28.     [Space]
    29.     [Header("Rotation")]
    30.     public float camRotationX;
    31.     public float camRotationY;
    32.     public float camRotationZ;
    33.  
    34.     [Space]
    35.     [Range(0f, 10f)]
    36.     public float turnSpeed;
    37.  
    38.     //-----Privates functions-----\\
    39.     private void Start()
    40.     {
    41.         offset = new Vector3(player.position.x + camPosX, player.position.y + camPosY, player.position.z + camPosZ);
    42.         transform.rotation = Quaternion.Euler(camRotationX, camRotationY, camRotationZ);
    43.     }
    44.  
    45.  
    46.     private void LateUpdate()
    47.     {
    48.         offset = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * turnSpeed, Vector3.right) * offset;
    49.         transform.position = player.position + offset;
    50.         transform.LookAt(player.position);
    51.     }
    52. }
    53.  
    Note that this script will allow the camera to go underground, so the next step is to creat a cap to stop the camera before the ground, good luck to people trying to do this, like me! ;)
     
    Last edited: May 10, 2017
    DianaCode3, Ryiah and AlanMattano like this.
  2. Mishkawy

    Mishkawy

    Joined:
    Feb 17, 2016
    Posts:
    7
    I wonder if you have any updates on this?
    So far, I am able to rotate the camera while the character is moving, and that makes the character follow the camera direction.
    And when the character is Idle, the camera does not move, I just want to keep everything as is, but I want when the character is idle, to be able to rotate the camera freely around him.
    And once the character starts moving again, the camera starts to control the character rotations again.
     
  3. Deleted User

    Deleted User

    Guest

    This seems like a really good idea. I have been looking for something like this for a long time. Here is the fixed script:

    Code (CSharp):
    1.  
    2. //-----CameraFollow script-----\\
    3. //---Author:MrPyxel---\\
    4. //---Version:1.5---\\
    5. //---Update:10/05/2017 22:20---\\
    6. //-----Libraries-----\\
    7. using System.Collections;
    8. using UnityEngine;
    9. //-----Script body-----\\
    10. public class CameraFollow : MonoBehaviour
    11. {
    12.     //-----Privates variables-----\\
    13.     private Vector3 offset;
    14.     //-----Publics variables-----\\
    15.     [Header("Variables")]
    16.     public Transform player;
    17.     [Space]
    18.     [Header("Position")]
    19.     public float camPosX;
    20.     public float camPosY;
    21.     public float camPosZ;
    22.     [Space]
    23.     [Header("Rotation")]
    24.     public float camRotationX;
    25.     public float camRotationY;
    26.     public float camRotationZ;
    27.     [Space]
    28.     [Range(0f, 10f)]
    29.     public float turnSpeed;
    30.     //-----Privates functions-----\\
    31.     private void Start()
    32.     {
    33.         offset = new Vector3(player.position.x + camPosX, player.position.y + camPosY, player.position.z + camPosZ);
    34.         transform.rotation = Quaternion.Euler(camRotationX, camRotationY, camRotationZ);
    35.     }
    36.     private void Update()
    37.     {
    38.         offset = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * turnSpeed, Vector3.right) * offset;
    39.         transform.position = player.position + offset;
    40.         transform.LookAt(player.position);
    41.     }
    42. }
    The Update function was named wrong. Correct me if I’m wrong.
     
  4. Uporka

    Uporka

    Joined:
    Nov 2, 2022
    Posts:
    1
    My math knowledge calls me to do suicide