Search Unity

Character Rotation on camera angle

Discussion in 'Scripting' started by Maker26, Nov 15, 2017.

  1. Maker26

    Maker26

    Joined:
    Nov 10, 2017
    Posts:
    23
    (Note: I am very new here and I don't know all the codings)
    I want to know how can I make my character's body rotate when my first person camera touches and angle. For example, if the camera reaches 15 degres, then the character should start rotating. It's like minecraft when the player is in third person, he can see that the character's body rotates along with the camera when it reaches a limit.
    If I'm not mistaken, it might be some sort of a coding with IEnumerator, but I don't know what to add next in order to work like I want to.

    I successfuly made the body rotate with the FP camera, but it rotates too perfect.

    Here is what I use:

    using UnityEngine;
    using System.Collections;

    public class BodyRotator : MonoBehaviour
    {

    public float speedH = 2.0f;
    public float speedV = 2.0f;

    private float yaw = 0.0f;
    private float pitch = 0.0f;

    void Update()
    {
    yaw += speedH * Input.GetAxis("Mouse X");

    transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
    }
    }

    I would appreciate some help. I'm using Unity 5.6.4f to let you know.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Something like:
    Code (csharp):
    1.  
    2. //  only check on the X-Z plane:
    3. Vector3 cameraDirection = new Vector3(camera.forward.x, 0f, camera.forward.z);
    4. Vector3 playerDirection = new Vector3(player.forward.x, 0f, player.forward.z);
    5.  
    6. if(Vector3.Angle(cameraDirection, playerDirection) > 15f)
    7. {
    8.     body.rotation = Quaternion.RotateTowards(body.rotation, camera.rotation, however fast you want);
    9. }
    10.  
     
    LiterallyJeff likes this.
  3. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    You may not want to match the body rotation to the camera rotation though, I imagine it probably should be a Y-axis only rotation.
     
    GroZZleR likes this.
  4. Maker26

    Maker26

    Joined:
    Nov 10, 2017
    Posts:
    23
    And where exactly should I put this? it gives me an error at the body.rotation. And even if I add the object body, it tells me that " 'object' does not contain a definition for 'rotation' and no extension method 'rotation' accepting a first argument of type 'rotation' could be found (are you missing a using directive or an assembly reference?)"

    Like I said, I am new in unity, and I don't know so many things about it.
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    pretty sure body would just be the transform in your code snippet :)
     
  6. Maker26

    Maker26

    Joined:
    Nov 10, 2017
    Posts:
    23
    The code does not work. Even if I change body to transform, it still gives me an error that unity can't convert from "this" to "this"
    What code am I supposed to use and WHERE is supposed to be?
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Post your updated code?
     
  8. Maker26

    Maker26

    Joined:
    Nov 10, 2017
    Posts:
    23
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class BodyRotator : MonoBehaviour
    6. {
    7.  
    8.     public float speedH = 2.0f;
    9.     public float speedV = 2.0f;
    10.  
    11.     private float yaw = 0.0f;
    12.     private float pitch = 0.0f;
    13.     public float maxDegreesDelta = 15f;
    14.  
    15.     Transform cameraT;
    16.     CharacterController controller;
    17.  
    18.     private void Start()
    19.     {
    20.         cameraT = GetComponent<Transform>();
    21.         controller = GetComponent<CharacterController>();
    22.  
    23.     }
    24.  
    25.     void Update()
    26.     {
    27.         Vector3 cameraDirection = new Vector3(cameraT.forward.x, 0f, cameraT.forward.z);
    28.         Vector3 playerDirection = new Vector3(controller.velocity.x, 0f, controller.velocity.z);
    29.  
    30.         if (Vector3.Angle(cameraDirection, playerDirection) > 15f)
    31.         {
    32.             transform.rotation = Quaternion.RotateTowards(transform.rotation, transform.rotation, 15f);
    33.         }
    34.  
    35.     }
    36. }
    37. }
    Not working as it should. Where exactly is supposed to be in? or do I need to add a float?
     
    Last edited: Nov 16, 2017
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
  10. Maker26

    Maker26

    Joined:
    Nov 10, 2017
    Posts:
    23
    Changed everything, but still nothing, the character is still breaking his neck.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BodyRotator : MonoBehaviour
    5. {
    6.  
    7.     public Transform target;
    8.     public float speed;
    9.  
    10.     Transform cameraT;
    11.     CharacterController controller;
    12.     private float maxDegreesDelta = 15f;
    13.  
    14.     private void Start()
    15.     {
    16.         cameraT = GetComponent<Transform>();
    17.         controller = GetComponent<CharacterController>();
    18.  
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         Vector3 cameraDirection = new Vector3(cameraT.forward.x, 0f, cameraT.forward.z);
    24.         Vector3 playerDirection = new Vector3(controller.velocity.x, 0f, controller.velocity.z);
    25.  
    26.         if (Vector3.Angle(cameraDirection, playerDirection) > maxDegreesDelta)
    27.         {
    28.             float maxDegreesDelta = speed * Time.deltaTime;
    29.             transform.rotation = Quaternion.RotateTowards(transform.rotation, target.rotation, maxDegreesDelta);
    30.         }
    31.     }
    32. }
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    What exactly does your comment mean? Is the character rotating on an axis you don't want or something else?
     
  12. Maker26

    Maker26

    Joined:
    Nov 10, 2017
    Posts:
    23
    I just said in the first post of this topic. I want my character's body to rotate when the neck that has a camera inside touches a certain angle. For example, if the neck reaches 15 degrees, then the body should start rotating. It's how you rotate the character from minecraft.
     
  13. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Oh wait, I didn't notice how you modified the 15f in the angle check to maxDegreesDelta. If nothing else, that should be back to 15f in that portion. See if that fixes it. :)
     
  14. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    You're definitely getting close. This should work, assuming something else is controlling the Camera rotation:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class BodyRotator : MonoBehaviour
    4. {
    5.     public float rotationSpeed = 30f;
    6.     public float deadZoneDegrees = 15f;
    7.  
    8.     private Transform cameraT;
    9.     private Vector3 cameraDirection;
    10.     private Vector3 playerDirection;
    11.     private Quaternion targetRotation;
    12.  
    13.     private void Awake()
    14.     {
    15.         cameraT = Camera.main.transform;
    16.     }
    17.  
    18.     private void Update()
    19.     {
    20.         cameraDirection = new Vector3(cameraT.forward.x, 0f, cameraT.forward.z);
    21.         playerDirection = new Vector3(transform.forward.x, 0f, transform.forward.z);
    22.  
    23.         if(Vector3.Angle(cameraDirection, playerDirection) > deadZoneDegrees)
    24.         {
    25.             targetRotation = Quaternion.LookRotation(cameraDirection, transform.up);
    26.             transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
    27.         }
    28.     }
    29. }
    30.  
     
    rhysw likes this.
  15. Maker26

    Maker26

    Joined:
    Nov 10, 2017
    Posts:
    23
    Your code is working how I was expecting to work. One question though. Was I supposed to drag the script in the bones? not in the whole character?
     
    NataliaMit likes this.
  16. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    I don't know how you set your character up. That script controls whatever gameobject it's on, so it should be on the body gameobject.
     
  17. Maker26

    Maker26

    Joined:
    Nov 10, 2017
    Posts:
    23
    Confirmed, the script was still in the whole character and one in the bones, I tried removing from the bones and let it remain in the character and nothing bad happened. The script is doing his job perfectly. Thank you so much ;D
    I can now move on to add some other features.
     
    LiterallyJeff likes this.
  18. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Glad to see you got it working :)
     
  19. Maker26

    Maker26

    Joined:
    Nov 10, 2017
    Posts:
    23
    Hmmmm, one thing I noticed. Yes, the character does rotate with the camera, but the camera kinda shakes when the body is rotating. If I set the speed super low, it won't shake anymore but the rotation is too slow, but if I set the speed a bit faster like any other first person game, it shakes the camera very badly. What is causing this? I think the code is missing something that might fix this problem. But what?
     
  20. Maker26

    Maker26

    Joined:
    Nov 10, 2017
    Posts:
    23
    What I was trying to say it was that when I stop rotation the camera and wait for the body finish it's rotation, then the camera will shake until I'm not rotating the character anymore.
    How can I prevent this?
     
  21. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Is the camera a child of the body or something, and also being controlled by another script? It sounds like either a physics issue, or you have two things affecting the position/rotation of the camera.
     
  22. Maker26

    Maker26

    Joined:
    Nov 10, 2017
    Posts:
    23
    My internet went off for 5 hours right at that time where I had to reply. The first person camera is in the head, and the third person camera is behind the character. Is anything wrong about it?
     
  23. enderman1212

    enderman1212

    Joined:
    May 28, 2019
    Posts:
    1
    Ok this is EPIC
     
  24. Audacity_Child

    Audacity_Child

    Joined:
    Feb 18, 2020
    Posts:
    4
    It's 2020 and this STILL works perfectly! For any other readers I'm creating a mobile VR game where when you look down to walk the animation plays and the character rotates along with the camera, my rotation speed is set all the way to 300 because of GVR Emulator but does the job PERFECTLY I now have a mini mobile VR GTA :) THANK YOU SO MUCH almost every time i need to Google a unity problem your forum fixes usually do the trick perfectly, again thank you so much!!
     
  25. fernandocass

    fernandocass

    Joined:
    Oct 4, 2018
    Posts:
    3
    For me, almost everything worked perfectly, my problem is that of the print. NOTE (It is not the positioning of my camera).
     

    Attached Files:

    • 01.jpg
      01.jpg
      File size:
      471 KB
      Views:
      313
  26. GarrettF

    GarrettF

    Joined:
    Jul 4, 2019
    Posts:
    49
    Just to add to the excitement. It's 2021 and this still works perfectly. I mean it's code so it'll probably work indefinitely.

    Also this works great with IK systems and third person games. Using FinalIK in 2020LTS and it works wonderfully to have the AIMik move the weapons back and forth before the character follows.
     
  27. mcreel304

    mcreel304

    Joined:
    Feb 27, 2022
    Posts:
    3
    i am sorry to bother you assuming you still are using unity but do you happen to have the script that was rotating your player like minecrafts 3rd person? i believe it will solve my issues with my own controller.
     
  28. mcreel304

    mcreel304

    Joined:
    Feb 27, 2022
    Posts:
    3
    could you possibly supply me with your third person stuff i cant seem to get mine to work with his code.