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
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.
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