- Home /
"Hacking" the lookAt function
So recently I've been delving into topdown shooter controls for a new project and was reading up on a shooting script when I came across something user "aldonaletto" mentioned that peeked my interest. He said:
Since you said ship, I suspect that you're clicking the empty space - and this won't work, because Raycast only returns true if some collider (at 100m or less, in this case) is hit. If this is the case, a possible hack is to create a big plane (scale 10000,1,10000) in front of the ship at a big distance (5000, for instance) and delete its mesh renderer. Child the plane to the player, and remeber also to change the raycast to Physics.Raycast(ray, hit) to remove the distance limit.
My question is, is this a legit way of getting this done? Now obviously, if something works, it works, even if it is the tiniest bit gimmicky, but I guess what I'm asking is, is this a "good" way to get the LookAt functions to work in this context? Is there a better way to make things work or give the mouse something to "hit"? Personally, I've been using a rotation script based on player and mouse positions, but LookAt just seems like the better way to go, especially in a top down shooter.
Cheers!
I remember reading from someone on here that games are made of smoke and mirrors. To me that means if it works, it's easy to maintain, and it doesn't effect performance then its a decent "hack" imo.
One way I got round the empty space problem was to cast a ray from the mouse to screen and use that direction as a look at position, not sure if that would work in your case.
Hey, I appreciate the reply! That's exactly my thought too, smoke and mirrors.
And that's essentially what I was going to do but had an issue with the Z axis messing things up, which what I think the plane would fix.
Answer by robertbu · Dec 26, 2013 at 11:29 PM
Seems fine to me, though I think there are a couple of better solutions. At a minimum, you can use Collider.Raycast() instead of Physics.Raycast(). Collider.Raycast() allows you to cast just against the plane instead of all the objects in the scene. A better yet solutions is to use Unity's mathematical Plane. Then you can use Plane.Raycast(). This solution involves no meshes, does not have issues with the the size of the plane, and is more efficient. If the plane is parallel to the camera plane (i.e. the camera is not at an angle with respect to the plane), then you can use Camera.ScreenToWorldPoint() as an even more efficient solution.
@aldonaletto knows all of this, so whatever solution he was outlining was specific to the question or perhaps the simplest solution for the situation.
Thanks robert! You guys sure seem to know your stuff. Thanks for all the info! I'll definitely check out everything you mentioned.
Answer by Owen-Reynolds · Dec 27, 2013 at 03:41 PM
It seems to me that the end result you're wanting is their "click arrow." Like, if they were click-firing a laser, this is where it would start and aim. You could then raycast, launch a rocket...whatever, using that click arrow.
As T27M hinted in a comment, the "click arrow" is built in the first step of a ray cast: Camera.main.ScreenPointToRay(Input.mousePosition);
That uses the imaginary front-plane of the camera as a start point (no need for you to make it.)
Your answer
Follow this Question
Related Questions
Transform.LookAt Seems To Cause Issues 1 Answer
Camera rotate to look at GameObject from Raycast 3 Answers
Aim flashlight at cursor in third person 1 Answer
smooth raycast LookAt ? 1 Answer
LookAt To Only Rotate on Y Axis 2 Answers