- Home /
Unable to turn character upside down due to problem with mouse look script
I'm making a 3D game in which the character is able to flip gravity and walk on the ceiling. I've done this before in 2D and wanted to try in 3D. The issue I'm having isn't the ability to flip gravity but that my character doesn't turn upside-down when gravity is flipped.
I followed a tutorial online to create 2 scripts for basic 3D character movement and mouse look. The issue I'm having stems from the last line in the mouse look script which sets the player's rotation based on the x component of the mouse around an upwards vector, as seen here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CamMouseLook : MonoBehaviour
{
public Vector2 mouseLook;
public Vector2 smoothV;
public float sensitivity = 2.0f;
public float smoothing = 2.0f;
GameObject player;
void Start ()
{
player = this.transform.parent.gameObject;
}
void Update ()
{
var md = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
md = Vector2.Scale(md, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing);
smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing);
mouseLook += smoothV;
mouseLook.y = Mathf.Clamp(mouseLook.y, -90.0f, 90.0f);
transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
player.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, player.transform.up);
}
}
I know it's that last line that's causing the issue as commenting it out fixes everything but leaves my player unable to look/turn left and right. I've tried numerous ways to solve this issue to no avail. I'm not exceedingly experienced so there's a good chance that I'm missing something obvious and any help that gives me a push in the right direction is appreciated. If anyone wants to watch the tutorial in question to see if it is of any help, the link is here with the specific script being discussed in the last third of the video.
Answer by hectorux · Nov 15, 2018 at 01:14 AM
Maybe you could try player.transform.Rotate(new Vector3(0,mouseLook.x,0)); this wont make your rotation equal to a new Quaternion, instead, it will add a rotation in your local y axis
Hello, sorry for my delayed reply - It was late for me when you responded so I decided to look into it in the morning. I've tried using your code ins$$anonymous$$d and whilst it does work when both right way up and upside down, there seems to be an issue that the player will continuously rotate without stopping if the mouse is nudged ever so slightly left or right. I assume that this rotation is constantly being added to the player's current rotation which is causing the issue. If you have any other ideas, they would be greatly appreciated.
Okay, so I tweaked your suggestion somewhat and came up with this: if (mouseLook.x != 0.0f) { player.transform.Rotate(new Vector3(0, mouseLook.x, 0)); mouseLook.x = 0.0f; } I used it to replace the last line of my code and it seems to work well so far. Thank you for the suggestion. :D
the problem with always rotating is because you used getAxisraw, this, have a difference with getaxis. The principal diference(if it isnt the only) is that you dont get a smooth resoult