Hello, I was wondering if someone might point me in the right direction RE a problem I’m having. Basically I want to continuously rotate a game object by a ratio which is defined by the distance (Vector3) of the mouse from Screen.height/2 & Screen.width/2. I already have the script that calculates this value (var dist) that uses Vector3.Distance, however I’m not really sure how to implement it beyond this. The key point is that the rotation is continuous, if I simply use Input.GetAxis(“Mouse Y…X”) * dist etc, it returns a linear value which rotates the game object by a set value, not a continuous ratio. If anyone could help it would be much appreciated; incidentally this is my first post so I apologize if my noobness is painful to you lol
If you want to make the object look at the mouse pointer, attach this script to the object:
function Update () {
var mousePos: Vector3 = Input.mousePosition; // get the mouse coordinates
// calculate the 3D point under the mouse pointer midway
// between the camera and the object:
mousePos.z = Vector3.Distance(Camera.main.transform.position, transform.position)/2;
var target: Vector3 = Camera.main.ScreenToWorldPoint(mousePos);
// look at the target point:
transform.LookAt(target);
}
But if you simply want to rotate the object proportionally to the mouse position, attach this one:
var maxRot: float = 60; // angle at the screen borders
private var angles: Vector3;
function Start(){
angles = transform.eulerAngles; // save the initial rotation
}
function Update(){
var pos = Input.mousePosition; // read mouse coordinates
// convert horizontal and vertical position to range -1..1
var ah = (2 * pos.x / Screen.width) - 1;
var av = (2 * pos.y / Screen.height) - 1;
var newAngles: Vector3;
// rotate the object from the initial rotation
newAngles.y = (angles.y - ah * maxRot)% 360;
newAngles.x = (angles.x - av * maxRot)% 360;
newAngles.z = 0;
transform.eulerAngles = newAngles;
}
This code rotates the object proportionally to the mouse deviation from the center, reaching +/-maxRot degrees at the screen borders.