I need help! I have a script that rotates the player and it rotates on the z axis when i only want it to rotate on the x/y axes.
So I'm writing a player movement/look script in javascript and i can't figure out why unity rotates around the y axis correctly but not the x axis. I have the script when i move the mouse left or right it rotates around the y axis and up/down it rotates around the x axis. But when i move the mouse right/left it works fine but if i move my mouse up/down it only rotates on the x axis in the forward direction. I'm not sure whats wrong. Thanks for your help & time. (I know it's poorly written, I'm a beginner) My code:
CODE
#pragma strict
var y : float;
var x : float;
var s = 3;
var r : Quaternion;
function Start () {
}
function Update ()
{
//DOWN
var p = GetComponent(Transform).position;
if(Input.GetButton("Verticalf"))
{
GetComponent(Transform).position = p + transform.forward / 10;
}
p = GetComponent(Transform).position;
if(Input.GetButton("Horizontalr"))
{
GetComponent(Transform).position = p + transform.right / 10;
}
p = GetComponent(Transform).position;
if(Input.GetButton("Horizontall"))
{
GetComponent(Transform).position = p + -transform.right / 10;
}
p = GetComponent(Transform).position;
if(Input.GetButton("Verticalb"))
{
GetComponent(Transform).position = p + -transform.forward / 10;
}
//WORKING CODE
x = Input.GetAxisRaw("Mouse X");
r = GetComponent(Transform).rotation;
if (Input.GetAxisRaw("Mouse X") != 0)
{
GetComponent(Transform).rotation = Quaternion.Euler (0,x * s,0) * r;
}
//BROKEN CODE
y = Input.GetAxisRaw("Mouse Y");
r = GetComponent(Transform).rotation;
if (Input.GetAxisRaw("Mouse Y") != 0)
{
GetComponent(Transform).rotation = Quaternion.Euler (y*s,0,0) * r;
}
}
Thanks so much!
Your answer
![](https://koobas.hobune.stream/wayback/20220612123814im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
OnCollisonEnter2D Not Firing after checking collider 1 Answer
Can not get my raycast to detect an object 0 Answers
New Hierarchy Son Script 0 Answers
How to add a Type to a Class in Javascript? 1 Answer
Audio won't play using Javascript 0 Answers