- Home /
How to disable my mouse to turn right
Hello everyone, I need help with coding and I've no idea how to do it. Basically, I don't want my "First Person Controller" (standart assets/character controller) to be able to turn right. Now I understand it works as X and Y but is there any way I can disable for it to turn right, and only turn left (X-wise. I want Y to stay as it is)
I've seen and tried changing "Minimum X, Maximum X" but it doesn't work. It always lets player turn right and left.
[AddComponent$$anonymous$$enu("Camera-Control/$$anonymous$$ouse Look")] public class $$anonymous$$ouseLook : $$anonymous$$onoBehaviour {
public enum RotationAxes { $$anonymous$$ouseXAndY = 0, $$anonymous$$ouseX = 1, $$anonymous$$ouseY = 2 }
public RotationAxes axes = RotationAxes.$$anonymous$$ouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float $$anonymous$$imumX = -360F;
public float maximumX = 360F;
public float $$anonymous$$imumY = -60F;
public float maximumY = 60F;
float rotationY = 0F;
void Update ()
{
if (axes == RotationAxes.$$anonymous$$ouseXAndY)
{
float rotationX = transform.localEulerAngles.y + Input.GetAxis("$$anonymous$$ouse X") * sensitivityX;
rotationY += Input.GetAxis("$$anonymous$$ouse Y") * sensitivityY;
rotationY = $$anonymous$$athf.Clamp (rotationY, $$anonymous$$imumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
else if (axes == RotationAxes.$$anonymous$$ouseX)
{
transform.Rotate(0, Input.GetAxis("$$anonymous$$ouse X") * sensitivityX, 0);
}
else
{
rotationY += Input.GetAxis("$$anonymous$$ouse Y") * sensitivityY;
rotationY = $$anonymous$$athf.Clamp (rotationY, $$anonymous$$imumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
}
void Start ()
{
// $$anonymous$$ake the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
}
Setting $$anonymous$$imumX
and maximumX
will have no effect because they're not used in your script anywhere. You need to add a clamp statement to the rotationX in the same way that you've clamped rotationY between $$anonymous$$imumY and maximumY
Thank you for your comment, I've edited my code and it's "half-working" right now but still not the result I want. $$anonymous$$ouse doesn't turn right when we don't turn left. But if mouse turns left, we can go right. Till the point in the beginning, where mouse can't turn right. :/
Answer by robertbu · Nov 27, 2013 at 10:37 PM
You will need to ignore either positive or negative input from the Input.GetAxis(). So change line 27 to either this:
float inputX = Input.GetAxis("Mouse X");
if (inputX > 0.0f)
transform.Rotate(0.0f, inputX * sensitivityX, 0.0f);
or
float inputX = Input.GetAxis("Mouse X");
if (inputX < 0.0f)
transform.Rotate(0.0f, inputX * sensitivityX, 0.0f);
Hello there! I tried your code but it didn't work. Gives an error with Remote. :(
I've modified my code and it's something like this now:
And the thing is, "it doesn't turn right in the beginning" (which is great) "BUT when we turn left we can turn right". We only can't turn right in the beginning point. So my problem is not solved, people can still turn right. Is there any way we can fix this by something like... No matter when & how much camera turns left, mouse can't turn right? I'm really not good at this and my game depends on this aspect.
using UnityEngine;
using System.Collections;
[AddComponent$$anonymous$$enu("Camera-Control/$$anonymous$$ouse Look")]
public class $$anonymous$$ouseLook : $$anonymous$$onoBehaviour {
public enum RotationAxes { $$anonymous$$ouseXAndY = 0, $$anonymous$$ouseX = 1, $$anonymous$$ouseY = 2 }
public RotationAxes axes = RotationAxes.$$anonymous$$ouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float $$anonymous$$imumX = -360F;
public float maximumX = 0F;
public float $$anonymous$$imumY = -60F;
public float maximumY = 60F;
float rotationY = 0F;
float rotationX = 0F;
void Update ()
{
if (axes == RotationAxes.$$anonymous$$ouseXAndY) {
rotationX += Input.GetAxis ("$$anonymous$$ouse X") * sensitivityX;
rotationX = $$anonymous$$athf.Clamp (rotationX, $$anonymous$$imumX, maximumX);
rotationY += Input.GetAxis ("$$anonymous$$ouse Y") * sensitivityY;
rotationY = $$anonymous$$athf.Clamp (rotationY, $$anonymous$$imumY, maximumY);
transform.localEulerAngles = new Vector3 (-rotationY, rotationX, -360);
} else {
}
}
void Start ()
{
// $$anonymous$$ake the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
}
I don't why the code I provided would have given you an error. As for your problem, is it an issue with orientation? That is, if you rotate something 180 degrees, left rotation becomes right rotation.
Hello! I'm using "$$anonymous$$ouseXandY" since it lets mouse to go on both X and Y planes. (looking up-down and right-left) I've deleted the other $$anonymous$$ouseY and $$anonymous$$ouseX cause when they're ticked in "Inspector" part they only used their plane. (only X or only Y)
About Rotate, error is CS0103: The name 'Rotate' does not exist in the current context.
If you can help me, please do. I will surely give credits too :>
It should have been 'transform.Rotate'. I fixed my code. But I unsure that it will be the solution. As I said, part of left/right turning depends on the frame of reference.
And it is working now... I can't thank you enough. I CAN'T THAN$$anonymous$$ YOU ENOUGH!
Your answer
Follow this Question
Related Questions
Look around when holding down mousebutton 1 Answer
Mouse Look Script Help 2 Answers
weapon bob when looking 2 Answers
How to control a weapon through a single mouse movement? 2 Answers
Unity Android Movement Problem 0 Answers