So I Followed Bracky’s Tutorial For The Camera Rotation. However, When I Turn My Camera Left And Right, The Body Doesn’t Follow Ever Since I Removed The Player Controller To Replace It With A RigidBody. I Am Currently Using RigidBody Movement And Bracky’s Camera Look Script.
If you’re using a Rigidbody, you should rotate it only in FixedUpdate and use rb.MoveRotation(), instead of modifying the transform.
Just be careful because you will want to keep your input processing in Update(). Input processing in FixedUpdate can cause other subtle bugs.
So:
- Capture input values in Update. Store them in member variables on your script.
- Read the captured input in FixedUpdate and do the rotation there using rb.MoveRotation
Everything that Praetor said, and also use code tags to post code, not screenshots.
To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
- is this code even running? which parts are running? how often does it run?
- what are the values of the variables involved? Are they initialized?
Knowing this information will help you reason about the behavior you are seeing.
I’ve been trying to implement a capture of the input but I can’t manage to do it and now I got another error.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseLook : MonoBehaviour
{
public float mouseSensitivity = 100f;
public Rigidbody playerBodyRb;
float xRotation;
private float mouseX = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90, 90);
}
private void FixedUpdate()
{
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBodyRb.MoveRotation(Vector3.up * mouseX);
}
}
the error is in this private void fixed update about how I can’t have vector3 and quaternion at the same time I don’t get it
Another error!? Wow! Neat! Hopefully someone here can read your mind.
How to report your problem productively in the Unity3D forums:
No, by that I mean I added a private float at the top and got it’s value in update. But I was rlly just writing whatever code came to mind and I wondered what he meant by capturing input values, I cannot find a solution on how to capture them. Also rb.moveRotation is a thing I never used before and I do not know how to implement it, that’s why I just removed my transform.rotation and replaced it with rb.moverotation
Oh ok I got the exact location of the error, idk can you help me fix my spaghetti code tho like playerBodyRb.MoveRotation(Vector3.up * mouseX);
Where I wrote Vector3.up is supposed to be a “Quaternion” and I am trying to rotate my player body along the y axis to make him look left and right.
private void FixedUpdate()
{
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBodyRb.MoveRotation(Vector3.up * mouseX);
}
should be
private void FixedUpdate()
{
Quaternion newRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBodyRb.MoveRotation(newRotation);
}
Remember we want to use MoveRotation instead of directly modifying the transform.