1
Support / Re: Rotating on the Z Axis to Face Objects
« on: November 23, 2013, 11:35:03 pm »
Excellent! Thank you very much!
Just in case anyone else is having this issue, here is my code:
This basically rotates the attached GameObject to your current mouse position. Just make sure to attach your main camera to the script in the inspector, and also make sure to have a Rigidbody component attached to the GameObject.
Thanks again!
Just in case anyone else is having this issue, here is my code:
Code: [Select]
using UnityEngine;
using System.Collections;
public class LookAtMouse : MonoBehaviour {
public Camera camera;
private Vector3 mousePosition;
void Update() {
//Gets the current mouse position on the screen
mousePosition = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z - camera.transform.position.z));
//Rotates the object towards the mouse cursor
rigidbody.transform.eulerAngles = new Vector3(0, 0, Mathf.Atan2((mousePosition.y - transform.position.y), (mousePosition.x - transform.position.x)) *Mathf.Rad2Deg - 90);
}
}
This basically rotates the attached GameObject to your current mouse position. Just make sure to attach your main camera to the script in the inspector, and also make sure to have a Rigidbody component attached to the GameObject.
Thanks again!