Sorry if my English is bad
I working on a platform-shooter game.
I have the player that walking with the right and left keys and I have the gun.
I that the gun will rotate toward the mouse according to the mouse position(X,Y).
I dont know how to do that but I have 2 Scripts:
1)rotate the gun on is X,Y,Z axis.
2)rotate the gun on is Y axis.
I dont know how to change them so they rotate only on the X axis,and I dont know
wich of the codes to choose. Maybe one of you can help me…Thanks
the codes:
// 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);
}
}
and
private var mousePos : Vector3;
private var mouseX : int;
private var mouseY : int;
private var cameraDif : int;
var player : GameObject;
enum cameraDirection {x, y, z};
var camDirection : cameraDirection;
function Start () {
if (camDirection == cameraDirection.x) {
cameraDif = camera.transform.position.x - player.transform.position.x;
}
else if (camDirection == cameraDirection.y) {
cameraDif = camera.transform.position.y - player.transform.position.y;
}
else if (camDirection == cameraDirection.z) {
cameraDif = camera.transform.position.z - player.transform.position.z;
}
}
function Update () {
mouseX = Input.mousePosition.x;
mouseY = Input.mousePosition.y;
mousePos = camera.ScreenToWorldPoint(Vector3(mouseX, mouseY, cameraDif));
if (camDirection == cameraDirection.y) {
mousePos.y = player.transform.position.y;
}
else if (camDirection == cameraDirection.x) {
mousePos.x = player.transform.position.x;
}
else if (camDirection == cameraDirection.z) {
mousePos.z = player.transform.position.z;
}
player.transform.LookAt(mousePos);
}
// 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.Normalize(Camera.main.transform.position - transform.position), 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);
}
}
var speed = 4.0;
function Update () {
// Generate a plane that intersects the transform's position with an upwards normal.
var offset = Vector3.Normalize(Camera.main.transform.position - transform.position);
var back = Vector3.Cross(Vector3.up, offset);
var playerPlane = new Plane(Vector3.Cross(back, 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);
}
}
the player is the sphere and he use the “move 2” script to moving.
the game object is what i want to rotate because later i will change him to my gun.
the scipts are: watchmouse - is my first code and the main camera use him but i disable him. watchmouse2 - is the original second code . and
watch mouse 3 - is the last code that hencz gave me.
I opened your project and I couldn’t find my code so I replaced NewBehaviourScript with it and it actually WORKED.
The gameobject rotates around its X axis you only need to place the object in front of the camera and not under the camera!
Here is my objects positions:
Sphere-(0,1.8,0)
Camera-(0,1.8,-10)
GameObject-(0,2,0) (the Y was 0 and I changed this to 2)
But its rotate him on all of the axis (X,Y,Z)!
I use your code on the gameobject:
var speed = 4.0;
function Update () {
// Generate a plane that intersects the transform's position with an upwards normal.
var offset = Vector3.Normalize(Camera.main.transform.position - transform.position);
var back = Vector3.Cross(Vector3.up, offset);
var playerPlane = new Plane(Vector3.Cross(back, 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);
}
}
Because when I in the game and not in the scene editor,I click on the empty gameobject
in the “hierarchy” menu and I see his rotation in the “Inspector” menu.
when you see numbers like 0.234324e -24 that means 0. it’s because of the inaccuracy of floats.
Attach a visible object to the gameobject and you’ll see that it rotates only on it’s X axis.
sometimes I see numbers like 0.234324e but normally I see regular numbers
here I take a picture:
I attached a box to the gameobject and its rotate on the XYZ axis.
the box is where i draw red line
var speed = 4.0;
function Update() {
// Generate a plane that intersects the transform's position with an upwards normal.
var playerPlane = new Plane(Vector3.back, 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);
}
}
var speed = 4.0;
function Update() {
// Generate a plane that intersects the transform's position with an upwards normal.
var playerPlane = new Plane(Vector3.back, 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);
var side = Vector3.Cross(Vector3.up, (targetPoint - transform.position).normalized);
var offset = Vector3.Cross(side, Vector3.up);
//var up = Vector3.Cross(offset, (Camera.main.transform.position - transform.position).normalized);
var up = Vector3.back;
// Determine the target rotation. This is the rotation if the transform looks at the target point.
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position, up) * Quaternion.FromToRotation(Vector3.right, Vector3.up);
// Smoothly rotate towards the target point.
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
}
}
Its work better now but its still rotate on all XYZ axis.
Its look good but not enough yet.the rotation angle is not perfect.
Here is the new one so please download it and help me…
Thanks alot for your help !!!