x


The angle in forward axis between two points.

Hi, guys. I need your help. I have one game object and a point on space that I got from a Ray casted from the main camera. I tried very hard to make this object aim to that point just in the forward axis, like a Sidescroller shooter, where the mouse pointer is the aim of the gun (up and down only). I tryed everything but something is wrong.. please advise.

void Update () {

    if(Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition),out hit, 100))
    {
       targetLook = hit.point;
       //now i need to make my object aim to the targetLook, but only on the forward axis.
       //I tryed using Atan2, that I usually do with Flash based games, but in 2D Space..
    }
}

Appreciate any help!

more ▼

asked Mar 08 '12 at 04:30 AM

ronaldosilva gravatar image

ronaldosilva
34 3 7 10

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

What's wrong with Transform.LookAt? if the object's center and the point are on the correct plane (defined by the object's up vector and it's center, if I understand correctly), or said differently, if both object and point have y == 0, it should be ok.

more ▼

answered Mar 08 '12 at 05:31 AM

Berenger gravatar image

Berenger
11k 12 19 53

The problem is that the target point is obtained by the X and Y position of the mouse, and the Z position can vary on the depth that of the surface the Raycast hit returns to me..

I need to ignore the Z and only get the X and Z position. Thats why the LookAt does not work =/

Mar 08 '12 at 01:57 PM ronaldosilva

Ok. Then, you can use an empty gameObject with a box collider that covers all the floor (something like (50,1,50)) with a specific layer then you cast the ray only on that layer, that way the Y won't change.

You can use Plane.Raycast as well, which return a distance and the hit point is calculated by ray.origine + ray.direction.normalized * distance.

Finally, you can use Vector3.Scale( Camera.main.ScreenToWorldPoint(mousepos), new Vector3(1,0,1) ). If the camera is orthographic.

Mar 08 '12 at 04:42 PM Berenger

Wow! Thank you! =)

Mar 08 '12 at 10:27 PM ronaldosilva
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2173
x289
x103

asked: Mar 08 '12 at 04:30 AM

Seen: 674 times

Last Updated: Mar 08 '12 at 10:27 PM