I am want to make a gameObject (a cube) rotate around its x,y,z axis while i press and drag the left arrow mouse down. Is this possible? Can you point out best practices or tutorials?
I've taken a different approach and I used Quaternions.
Here is my code which drag rotates an object with easing.
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);
}
}
My only problem is that if you move the mouse very fast it bounces the object over the axis you moved it. It can be solved with using ray casting but then it will constrict the drag over the object which i don't want...
It's not clear what you exactly want but the following script rotates cube around x axis when pressing left or right arrow (or use joystick or whatever is set up for left-right movement).
var rotationSpeed = 100.0;
function Update () {
var ydir = Input.GetAxis("Horizontal");
transform.Rotate(ydir * rotationSpeed * Time.deltaTime, 0, 0);
}
var cubeThing : Transform; //the object that you want rotated
function Update(){
if(Input.GetMouseButton(0)){
cubeThing.Rotate(Vector3.up * Input.GetAxis("Mouse Y"));
}
}
Also, make sure to tag your question as answered if you found a solution.
A friend of mine came up with this script, this will let the rotated object ease out even if you release the mouse…
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;
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;