- Home /
Limit rotation of weapons with mouse help
I have a robot who shoots paintballs out of two paintball guns mounted as arms. These rotate to follow the mouse and shoot in that direction when fired. At the moment the guns face backwards sometimes when the mouse goes behind the player. I need them to constantly face forward- do I need to limit rotation? Here is my code so far..
using UnityEngine;
using System.Collections;
public class MyScript : MonoBehaviour {
private Vector3 inputRotation;
private Vector3 mousePlacement;
private Vector3 screenCentre;
void Update () {
FindStuff();
transform.rotation = Quaternion.LookRotation(inputRotation);
}
void FindStuff () {
screenCentre = new Vector3(Screen.width * 0.5f,0,Screen.height * 0.5f);
mousePlacement = Input.mousePosition;
mousePlacement.z = mousePlacement.y;
mousePlacement.y = 0;
inputRotation = mousePlacement - screenCentre;
}
}
Any help or pointers would be great!
Thanks
Answer by robertbu · Jan 31, 2013 at 01:42 AM
Quick, untested idea. Limit your mousePlacement.z to positive values >= 1. Just something like:
if (mousePlacement.z < 1) mousePlacement.z = 1;
This should accomplish two goals. First the point you look at will always be in front of the robot, so his guns will never face backward. Second you will never produce an inputRotation of (0,0,0). According to the docs, LookRotation() called with (0,0,0) will log an error.
It doesn't seem to make any difference- perhaps I am placing it incorrectly. I usually use JavaScript.
Here is my code
using UnityEngine;
using System.Collections;
public class $$anonymous$$yScript : $$anonymous$$onoBehaviour {
private Vector3 inputRotation;
private Vector3 mousePlacement;
private Vector3 screenCentre;
void Update () {
FindCrap();
transform.rotation = Quaternion.LookRotation(inputRotation);
}
void FindCrap () {
screenCentre = new Vector3(Screen.width * 0.5f,0,Screen.height * 0.5f);
mousePlacement = Input.mousePosition;
mousePlacement.z = mousePlacement.y;
mousePlacement.y = 0;
if (mousePlacement.z < 1) mousePlacement.z = 1;
inputRotation = mousePlacement - screenCentre;
}
}
I played a bit. Here is a mod to your code that I think gives you what you want.
public class $$anonymous$$yScript : $$anonymous$$onoBehaviour {
private Vector3 inputRotation;
private Vector3 mousePlacement;
private Vector3 screenCentre;
void Update () {
FindStuff();
transform.rotation = Quaternion.LookRotation(inputRotation);
}
void FindStuff () {
screenCentre = new Vector3(Screen.width * 0.5f,0,Screen.height * 0.5f);
mousePlacement = Input.mousePosition - screenCentre;
//mousePlacement.z = mousePlacement.y;
mousePlacement.z = 100; // <--- Set to get the sensitivity
mousePlacement.y = 0;
inputRotation = mousePlacement;
}
}
Note that placing mouse 'Y' in 'Z' is going to give inconsistent behavior in that the amount of rotation will change depending on the 'Y' location of the mouse. Pick a value that gives the mouse sensitivity you want and just assign it to z.
Using screen units to do calculations can cause problems as resolutions change. Here is a different way of doing your rotation:
public class $$anonymous$$yScript : $$anonymous$$onoBehaviour {
void Update () {
Vector3 v3T = Input.mousePosition;
v3T.z = 20;
v3T = Camera.main.ScreenToWorldPoint (v3T);
v3T.y = transform.position.y;
transform.rotation = Quaternion.LookRotation(v3T);
}
}
Note that the Z value used in ScreenToWorldPoint() is the distance in front of the camera. For this code to work, the value has to be greater than the distance between camera and the object rotating.
Your answer
Follow this Question
Related Questions
Limit Object Angle 2 Answers
Clamp rotation of a GameObject 1 Answer
how can i get Y rotation of Transform in degree ?? 1 Answer
Rotation of Object on single axis in direction of the mouse position 0 Answers
Slerp/Rotational Problem Query 1 Answer