- Home /
Rotate Object with with mouse
Hi
I found the script below here in the forum. It works great but it turns the object around itself, means, once it's back is front, moving the mouse up becomes down and the other way around. I would need to modify it to use Space.World but can not figure it out.
var speed : int;
var friction : float;
var lerpSpeed : float;
private var xDeg : float;
private var yDeg : float;
private var fromRotation : Quaternion;
private var toRotation : Quaternion;
function Update () { if(Input.GetMouseButton(0)) {
xDeg -= Input.GetAxis("Mouse X") * speed * friction;
yDeg += Input.GetAxis("Mouse Y") * speed * friction;
fromRotation = transform.rotation;
toRotation = Quaternion.Euler(yDeg,xDeg,0);
transform.rotation = Quaternion.Lerp(fromRotation,toRotation,Time.deltaTime * lerpSpeed);
}
}
Is this for a first-person view, or a top-down look-at-mouse-position, or just move-mouse-rotate-object thing?
just a move-mouse-rotate-object thing, a fixed object with a fixed camera
it's a mouse rotate object thing, te object is fixed and the camera is fixed, i just want to turn the object
@sascham: Don't use the answer function unless you want to answer your own question. Use the "add new comment" button below the post you want to comment. If you want to clarify / add things to your question you can edit any of your posts.
I've converted your answers to comments
Answer by asafsitner · Dec 12, 2011 at 09:20 AM
Try using transform.Rotate
instead. Something like this should work (c#, but the principle is the same):
using UnityEngine;
public class Rotate : MonoBehaviour
{
public float RotationSpeed = 5;
// Update is called once per frame
void Update ()
{
transform.Rotate((Input.GetAxis("Mouse X") * RotationSpeed * Time.deltaTime), (Input.GetAxis("Mouse Y") * RotationSpeed * Time.deltaTime), 0, Space.World);
}
}
I'd suggest switching the X and Y axes to make the rotation more intuitive (i.e. dragging the mouse left-right to rotate the object left-right instead of up-down) but that depends on your intention of course.
Your answer
![](https://koobas.hobune.stream/wayback/20220613061945im_/https://answers.unity.com/themes/thub/images/avi.jpg)