- Home /
Look at the mouse?
Hello. In this game, the space ship rotates to look at the mouse cursor. How can I do this, but only along one axis? Thanks
Answer by Meater6 · Apr 25, 2011 at 12:38 AM
Try this:
// 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 = 4.0;
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);
}
}
I got it from Unity Wiki, and I want to make sure the person who made it gets the credit for this script. I tried to use a link, but it didn't seem to work. Hope this helps.
EDIT:
I made this script here that makes the object it's on look at where the mouse intersects. This will do for the rotation of the spaceship, as long as there is a invisible plane in the background (you can also do it through scripting, without the invisible plane) . But the movement part is much more complicated. I think it might work like this, the ship moves toward the x,y value of the mouse intersect point, while keeping its z position the same. I tested this method and it seems like the movement in the game.
SpaceshipScript:
var posZ : float = 0; var smooth : float = 0.1;
function Update () { var ray : Ray = camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit;
if(Physics.Raycast(ray,hit)){
transform.LookAt(hit.point);
transform.position = Vector3.Lerp(transform.position,hit.point,smooth);
}
transform.rotation.z = 0;
transform.position.z = posZ;
}
I hope this answers your question.
Actually, I already tried that script, and I couldn't get it to work. I tried to replace the "Vector3.up" to "Vector3.right" and nothing happens and when I try to change it to "Vector3.forward" and the script goes haywire (I cant really explain it, the object starts rotating along multiple axes).
It depends on what you want. I would like it if you added more information to your question, for example what axis. For the game you displayed, this script would not work. I tried changing the Vector3, and it worked fine.
I want to make something exactly like the game that I linked to in my question. I want the ship to be facing forward (along the world z axis) and I want the ship to look towards the cursor, rotating along the x and y axes. The reason I asked how to rotate along one axis only is to prevent the object rolling around the z axis (when I use a normal "lookAt" code, the object will rotate around the z axis to face the object). $$anonymous$$y plan is to have an empty object and rotate it along the x axis, put the ship in the game object, and then rotate the ship around the y axis (so there will be no z rotation)
If this script would not work, do you have any ideas on how I would accomplish what I want?
Ok, your right this script wouldn't work for that. I would try making a plane in the background, use the same type of raycast as in the script above, and use a smooth look at to the hit point, while locking the z axis. If the raycast hits a asteroid, look at that ins$$anonymous$$d, Though, the game that you present does not lock the z axis. It seems to smooth look at the point, then once it has reached the correct position (You define what that position is) it then resets it self to 0 rotation on the z axis. I will try to find a better explanation to this.
Answer by Aydan · Apr 25, 2011 at 12:57 AM
add this script to your camera then set the fpc to the object you want to rotate.
private var worldPos : Vector3; private var mouseX : int; private var mouseY : int; private var cameraDif : int; var fpc : GameObject;
function Start () {
//determines how far down the ScreenToWorldPoint is from the camera position
//it's calculated [height of camera] - [height of pivot point of character]
//this is to ensure the character only rotates (via LookAt) along rotation.x and doesn't look up or down
cameraDif = camera.transform.position.y - fpc.transform.position.y;
}
function Update () {
mouseX = Input.mousePosition.x;
mouseY = Input.mousePosition.y;
//this takes your current camera and defines the world position where your mouse cursor is at the height of your character -->translates your onscreen position of mouse into world coordinates
worldPos = camera.ScreenToWorldPoint(Vector3(mouseX, mouseY, cameraDif));
fpc.transform.LookAt(worldPos);
}
this is for a top down game so it may come in helpful
Is there any way to use this script and have the object positioned at some other place that 1 on the Y axis?
this is for a top down game so maybe if you changed the axis you could get the what your looking for.
Your answer
Follow this Question
Related Questions
Movement along X and Z axis... 2 Answers
error CS8025: Parsing error 1 Answer
How to make something happen when mouse on side of screen? 1 Answer
Unity 3D Screen.lockCursor Problems 2 Answers
How to convert the MouseLook(Script) to a java script. 1 Answer