I've been at this for a few hours now, thinking it would be nice and simple, but guess not!
All that I want to do is rotate an object along it's Z-axis towards the mouse cursor.
I found some code (see the Rotation part of the full code below) that rotates the object as long as it stays close to the center. This object moves around though, and apparently the farther away the object moves from the center of the scene (0,0 x/y coords), the slower the rotation effect! I want the rotation to always stay constant (IE; instantaneous at this point), but can't for the life of me figure out how to translate the position of the mouse on the screen into the facing rotation Z-axis of the object!
using UnityEngine;
using System.Collections;
public class Object_Controller : MonoBehaviour {
//Public Vars
public Camera camera;
public float speed;
//Private Vars
private Vector3 mousePosition;
private Vector3 direction;
private float distanceFromObject;
void FixedUpdate() {
if (Input.GetButton("Fire2")){
//Grab the current mouse position on the screen
mousePosition = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y, Input.mousePosition.z));
//Rotates toward the mouse
rigidbody.rotation = Quaternion.Euler(0, 0, Mathf.Atan2(mousePosition.y, mousePosition.x) * Mathf.Rad2Deg - 90);
//Judge the distance from the object and the mouse
distanceFromObject = (Input.mousePosition - camera.WorldToScreenPoint(transform.position)).magnitude;
//Move towards the mouse
direction = (mousePosition - transform.position).normalized;
rigidbody.AddForce(direction * speed * distanceFromObject * Time.deltaTime);
}//End Move Towards If Case
}//End Fire3 If case
}
}