Hi there. I'm having a hard time coming up with a script that will allow me to rotate an object by all three axis based on the direction I move my mouse. To clarify a little more for example: no matter what position on it's X, Y, and Z it is in, I'd like the object in to rotate downward and toward me upon moving the mouse downward, rotate leftward no matter it's X, Y, Z position if i move my mouse leftward, ETC.
Edit: I worked around this problem by using physics to my advantage: Two positions to apply force makes the object spin as if my mouse is controlling it. Here's the script. If there are no better answers in the next few days I'll close this as solved.
function Update () {
rigidbody.AddForceAtPosition(Vector3(transform.position.x+(10*Input.GetAxis("Mouse
X")),transform.position.y,transform.position.z+(10*Input.GetAxis("Mouse
Y"))),Vector3(transform.position.x,transform.position.y+4,transform.position.z));
print(transform.rotation); }
var speed : float = 1.0; //how fast the object should rotate
function Update(){
transform.Rotate(Vector3(Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0) * Time.deltaTime * speed);
}
Best wishes,
-Lincoln Green
Is there away to put a restraint on this so it will only rotate a certain amount??
public class ExampleClass : MonoBehaviour {
public float horizontalSpeed = 2.0F;
public float verticalSpeed = 2.0F;
void Update() {
float h = horizontalSpeed * Input.GetAxis("Mouse X");
float v = verticalSpeed * Input.GetAxis("Mouse Y");
transform.Rotate(v, h, 0);
}
}
Thanks for the response. Updated Unity again and problem resolved. Thanks for the response. If it happens on a future update I will double check to make sure the scene is open.
Screen.lockCursor = true;
if (Mathf.Abs(yawMouse) > 0.1f || Mathf.Abs(pitchMouse) > 0.1f)
{
targetFlyRotation = yawMouse * transform.right + pitchMouse * transform.up;
targetFlyRotation.Normalize();
targetFlyRotation *= Time.deltaTime * 3.0f;
//limit x rotation if looking too much up or down
//Log out the limitX value for this to make sense
float limitX = Quaternion.LookRotation(moveDirection + targetFlyRotation).eulerAngles.x;
//70 sets the rotation limit in the down direction
//290 sets limit for up direction
if (limitX < 90 && limitX > 70 || limitX > 270 && limitX < 290)
{
Debug.Log("restrict motion");
}
else
{
moveDirection += targetFlyRotation;
//does the actual rotation on the object if no limits are breached
transform.rotation = Quaternion.LookRotation(moveDirection);
}
}
may i ask u something to the suggested solution? i tried but didnt get it
heres my code so fare:
public GameObject Target;
public float horizontalSpeed = 2.0F;
public float verticalSpeed = 2.0F;
public float angle = 45.0f;
void Update()
{
if (Input.GetMouseButton(0))
{
MouseSpin();
}
if (Input.GetMouseButtonDown(1))
{
Target.transform.rotation = Quaternion.identity;
}
}
void MouseSpin()
{
float h = horizontalSpeed * Input.GetAxis("Mouse X");
float v = verticalSpeed * Input.GetAxis("Mouse Y");
v = Mathf.Clamp(v, 1, -1);
Target.transform.Rotate(v, h, 0);
}
}
the clamp doesnt work ^^ maybe im just to dumb, what did i wrong?
greets
You shouldn't post a question as an answer. If you can't find any similiar questions on Unity Answers you should make a new topic. Then you might get some more replies. > the clamp doesnt work ^^ Try to explain what doesn't work. Did you get an error? And try to debug with http://docs.unity3d.com/ScriptReference/Debug.Log.html I haven't tested your code yet, so I am not sure what's going wrong. Tim
Is there away to put a restraint on this so it will only rotate a certain amount??
– Jamie_h88I'd guess you would have to keep track of the current rotation, or using transform's quaternion to check, and then do or not do the rotation.
– MrVerdouxhave u tried an attribute? http://unity3d.com/learn/tutorials/modules/intermediate/scripting/attributes
– wardevourIt says Unexpected symbol 'Time' ??? what is the problem?
– idiot333ya, same here.
– Yanboys