Rotate OBJECT to face mouse cursor for x/z based top down shooter
Normally I'd avoid reposting a repost but I've tried every single solution proffered in the other threads and they do not work. One reason, among several, is that they all use the camera to calculate the rotation angle in relation to the mouse and then rotate the camera to it. This is not desirable.
Instead, I want the primary game object to rotate to face the mouse cursor while leaving the camera in its original orientation.
Few caveats: I'm not a coder. I'm using uScript for most of my development. I'll also mention that this has kept me scratching my head for almost a week, so any prescriptive solutions that can get me over this hump are really appreciated.
Answer by Mox.du · Nov 12, 2011 at 05:01 AM
Hi,
I am not sure what is the problem exactly but here are two codes for rotating object toward mouse cursor around its Y axis (I believe that this is what you wanted).
Code 1: Here you have speed while rotating towards mouse. You can get rid of speed if you uncomment last line of code and comment previous.
// LookAtMouse will cause an object to rotate toward the cursor, along the y axis. // // To use, drop on an object that should always look toward the mouse cursor. // Change the speed value to alter how quickly the object rotates toward the mouse.
// speed is the rate at which the object will rotate var speed = 5;
function Update () { // Generate a plane that intersects the transform's position with an upwards normal. var playerPlane = new Plane(Vector3.up, transform.position);
// Generate a ray from the cursor position
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
// Determine the point where the cursor ray intersects the plane.
// This will be the point that the object must look towards to be looking at the mouse.
// Raycasting to a Plane object only gives us a distance, so we'll have to take the distance,
// then find the point along that ray that meets that distance. This will be the point
// to look at.
var hitdist = 0.0;
// If the ray is parallel to the plane, Raycast will return false.
if (playerPlane.Raycast (ray, hitdist)) {
// Get the point along the ray that hits the calculated distance.
var targetPoint = ray.GetPoint(hitdist);
// Determine the target rotation. This is the rotation if the transform looks at the target point.
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
// Smoothly rotate towards the target point.
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime); // WITH SPEED
//transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 1); // WITHOUT SPEED!!!
}
}
Code2: - make sure that your Camera "X" is aligned (paralel) with object "X". positive or negative.
var mouse_pos : Vector3; var target : Transform; //Assign to the object you want to rotate var object_pos : Vector3; var angle : float;
function Update () { mouse_pos = Input.mousePosition; print(mouse_pos); // mouse_pos.z = 5.23; //The distance between the camera and object object_pos = Camera.main.WorldToScreenPoint(target.position); mouse_pos.x = mouse_pos.x - object_pos.x; mouse_pos.y = mouse_pos.y - object_pos.y; angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg; // if(angle < 90 && angle > -90){ transform.rotation = Quaternion.Euler(Vector3(0, -angle, 0)); //print(angle); //}
}
Regards.
The first code works, but there are some things you need to do to make it so. First, tag the camera attached to your object as $$anonymous$$ainCamera. Second, make sure that the script is attached to the gameObject, not the camera, to avoid turning the camera too. Thanks. Hoping these finer details will help folks.
Both codes works. Something you should always do when taking code snippets like this is to make a test with it. Open new scene, add object attach code and test, because none of these codes was written for your specific scene in sense of object na$$anonymous$$g etc. It is also good to read comments in scripts which will give you some clues.
I tried the first code and it works fine except that as my object rotates to face the mouse, the mouse is also being moved, so it continuously spirals around.
Answer by Mox.du · Nov 13, 2011 at 12:10 AM
Hmm ...
Scripts are working with transforms only, so if your model CVs/Vertices are being altered I don't think that script is the cause by itself. Perhaps you have some children problems or bone problems or something like that. If you don't solve this you can upload small package with just the object and script, showing problem and I will take a look.
Regards.
Answer by Robo885 · Sep 14, 2012 at 12:10 AM
I have tried the first block of code, applying it two either my player, or the spotlight I have attached to it. My goal is to have the light face the direction of the mouse, while the camera stays put and the player can move with WASD.
However, I keep getting NullReferenceExpection. Being new, I have tried to solve this problem, but have run in to no good luck. Help would be much appreciated!
NullReferenceException UnityEngine.Camera.ScreenPointToRay (Vector3 position) Mouse_Tracking.Update () (at Assets/Mouse_Tracking.js:14)
The above implies that line 14 is the issue;
`var ray = Camera.main.ScreenPointToRay (Input.mousePosition);`
I have changed Camera to Light, but this did not help the situation. I also set it to GameObject and moved the script to the capsule itself - no fixes.
Hopefully this is enough information for a kindly stranger to help?