Character Rotation on camera angle

(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.

Something like:

//  only check on the X-Z plane:
Vector3 cameraDirection = new Vector3(camera.forward.x, 0f, camera.forward.z);
Vector3 playerDirection = new Vector3(player.forward.x, 0f, player.forward.z);

if(Vector3.Angle(cameraDirection, playerDirection) > 15f)
{
    body.rotation = Quaternion.RotateTowards(body.rotation, camera.rotation, however fast you want);
}
1 Like

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.

1 Like

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.

pretty sure body would just be the transform in your code snippet :slight_smile:

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?

Post your updated code?

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;
    public float maxDegreesDelta = 15f;

    Transform cameraT;
    CharacterController controller;

    private void Start()
    {
        cameraT = GetComponent<Transform>();
        controller = GetComponent<CharacterController>();

    }

    void Update()
    {
        Vector3 cameraDirection = new Vector3(cameraT.forward.x, 0f, cameraT.forward.z);
        Vector3 playerDirection = new Vector3(controller.velocity.x, 0f, controller.velocity.z);

        if (Vector3.Angle(cameraDirection, playerDirection) > 15f)
        {
            transform.rotation = Quaternion.RotateTowards(transform.rotation, transform.rotation, 15f);
        }

    }
}
}

Not working as it should. Where exactly is supposed to be in? or do I need to add a float?

You are missing the 3rd parameter of RotateTowards (max speed delta): Unity - Scripting API: Quaternion.RotateTowards

Changed everything, but still nothing, the character is still breaking his neck.

using UnityEngine;
using System.Collections;

public class BodyRotator : MonoBehaviour
{

    public Transform target;
    public float speed;

    Transform cameraT;
    CharacterController controller;
    private float maxDegreesDelta = 15f;

    private void Start()
    {
        cameraT = GetComponent<Transform>();
        controller = GetComponent<CharacterController>();

    }

    void Update()
    {
        Vector3 cameraDirection = new Vector3(cameraT.forward.x, 0f, cameraT.forward.z);
        Vector3 playerDirection = new Vector3(controller.velocity.x, 0f, controller.velocity.z);

        if (Vector3.Angle(cameraDirection, playerDirection) > maxDegreesDelta)
        {
            float maxDegreesDelta = speed * Time.deltaTime;
            transform.rotation = Quaternion.RotateTowards(transform.rotation, target.rotation, maxDegreesDelta);
        }
    }
}

What exactly does your comment mean? Is the character rotating on an axis you don’t want or something else?

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.

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. :slight_smile:

You’re definitely getting close. This should work, assuming something else is controlling the Camera rotation:

using UnityEngine;

public class BodyRotator : MonoBehaviour
{
    public float rotationSpeed = 30f;
    public float deadZoneDegrees = 15f;

    private Transform cameraT;
    private Vector3 cameraDirection;
    private Vector3 playerDirection;
    private Quaternion targetRotation;

    private void Awake()
    {
        cameraT = Camera.main.transform;
    }

    private void Update()
    {
        cameraDirection = new Vector3(cameraT.forward.x, 0f, cameraT.forward.z);
        playerDirection = new Vector3(transform.forward.x, 0f, transform.forward.z);

        if(Vector3.Angle(cameraDirection, playerDirection) > deadZoneDegrees)
        {
            targetRotation = Quaternion.LookRotation(cameraDirection, transform.up);
            transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
        }
    }
}
1 Like

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?

1 Like

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.

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.

1 Like

Glad to see you got it working :slight_smile:

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?

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?