so Ive spent all night looking for a solution, and its probably really easy but I’m a goof. Right now my camera is nested under the rigid body of this character. so when my camera moves left, and right, it moves the mesh as well.
My question is, how do I get full mouse movement of up and down, left and right, but only move the model side to side? I like how the camera stays behind the mesh so its easier to rotate.
Clearly theres got to be a different way. Im using a rigid body so is transform.Rotate the way to go? Should I be adding torque or using rigidbody.Rotate in some way? Im completely lost in euler angles, quaternions, ect. I just want to understand better.
public class MyCamera : MonoBehaviour
{
public float mouseY;
public float mouseX;
void Update()
{
Cursor.lockState = CursorLockMode.Locked;
mouseY = Input.GetAxis("Mouse Y");
mouseX = Input.GetAxis("Mouse X");
transform.Rotate(Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0);
}
}
My goal is to have a third person camera similar to game such as risk of rain, where the camera is locked behind you and you can aim and shoot just fine without the characters model going up and down with the mouse.
I believe this is exactly what you want. Even tho, you may have to watch one or two previous episodes to fully understand what’s going on if you are new:
Although it was a fun tutorial, this doesn’t solve my issue.
I made this whole new camera system from the video which rotates the camera around the player, which doesn’t quit work.
I don’t want to turn the rigidbody left and right, I want to strafe (which I have working fine movement wise)
the problem is the rigidbody doesn’t rotate with the camera to face the dirction th camera looks at.
I made a slight workaround by making an empty game object that the rigidbody “looks at” and that gam object is nested under the camera.
This works somewhat, but when im looking over the character from above the translation from strafing left and right, forward, ect doesn’t line up with the camera facing.
I dont want to move the character at awkward directions. I want the rigid body to forcefully always turn to look the same way the camera is facing (and preferably not on the pitch)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyCamera : MonoBehaviour
{
public bool lockCursor;
public float mouseSensitivity = 10;
public Transform target;
public Transform target2;
public float dstFromTarget = 2;
public Vector2 pitchMinMax = new Vector2(-40, 85);
public float rotationSmoothTime = .12f;
Vector3 rotationSmoothVelocity;
Vector3 currentRotation;
public Rigidbody rb;
float yaw;
float pitch;
void Start()
{
if (lockCursor)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
void LateUpdate()
{
yaw += Input.GetAxis("Mouse X") * mouseSensitivity;
pitch -= Input.GetAxis("Mouse Y") * mouseSensitivity;
pitch = Mathf.Clamp(pitch, pitchMinMax.x, pitchMinMax.y);
//smoothes the pitch and yaw inputs
currentRotation = Vector3.SmoothDamp(currentRotation, new Vector3(pitch, yaw), ref rotationSmoothVelocity, rotationSmoothTime);
//not quit sur whats going on here
transform.eulerAngles = currentRotation;
//rotates th camera away from the target
transform.position = target.position - transform.forward * dstFromTarget;
//rotate rigidbody to the forward point in front of camera
Vector3 target2Position = new Vector3(target2.transform.position.x, 0, target2.transform.position.z);
rb.transform.LookAt(target2Position);
}
}
As someone who did the above tutorial in the past, i quickly loaded the character i created. Then i jumped into Risk of Rain 2. Both follow the camera looking direction while walking with w. Both allow walking left or right while looking forward. They generally behave pretty similar with minor differences.
The only differences i found is the walking direction while strafing, as well as rotating the character with the camera while standing. While it’s been some time that i watched the tutorial, Sebastian Lague generally does a great job at explaining which part of the code is responsible for what. So if some part of the code does something that’s not to your liking, you should be able to just leave it out.
If you, instead of looking left and right while walking left and right, want to look forward while walking left and right, then you can probably achieve that by changing around some numbers. I believe in the tutorial he calculated some angle to turn to, then walked forwards. Instead you’d want to calculate the angle and move in that direction, instead of turning and moving forwards.
You could also just create an imaginary vector by stretching out the forward direction of the camera, and then looking at that position in word space. Doing so after the other calculations, would force your character to look in the direction of the camera.
By far the best way these days to get a camera rolling in Unity is to use Cinemachine. It’s both powerful, easy to use, and free (you just have to activate it in the package manager).
If you just want your own system to work, the direction you want your character to look in is the camera’s direction, but with y=0. So:
Thanks for this code. its all I needed to get the whole thing to work right. You rock!! Don’t know why I didn’t think of this!
A new problem has arose. Is there a way to smooth out the rotation of the character. Right now it looks all jittery when rotating left and right with the mouse. Maybe I should not use rigidbody to rotate and just use a normal transform? I just wanted physics to apply if something collide with the player while they are turning.
So normal rigid bodies with the same physic materials as my player model work fine on slopes, they slide if not enough friction.
Well my character slides off any slopes even the smallest slope imaginable. Its like the character controller is adding a little bit of movement to it even when idle. Is it perhaps the vibrations in the house moving the mouse slighty?
Either way, my game idea wont work because using this method my character slowly slides off any surface that isn’t 100% flat
EDIT: Confirmed that if I don’t rotate i don’t slip off thee surface. Ugh. Frustrating
The unity manual says “If you want to continuously rotate a rigidbody use MoveRotation instead, which takes interpolation into account.”
I guess ill try to fiddle around with MoveRotation.
Make sure to set your player’s Rigidbody to interpolate. That makes all of it’s movements much smoother (at a slight perf cost, which is why it’s no on by default).
Not sure about the sliding - but remember that making a character controller takes quite a bit of work. You’ll probably end up doing a lot of things manually rather than relying on the physics simulation, simply to make it feel good.
I appreciate your help man. Im going to put this project on hold until I get better. Ive moved onto a new project. Honestly I’ve tried everything and cannot get the “vibrating/wobbling” to go away no matter how I rotate them. So I must be a noob or something. GG.