- Home /
Having an object point towards mouse position in 3d world
Hello. I've been trying to get this to work for.. Let's just say many hours now, and despite finding a bunch of suggestions and help threads i've been unable to solve it. So in a last attempt i'm writing here, hoping someone will be willing to help me find the solution.
I'm trying to have a pistol point at the mouse. It's fairly simple in theory.
I've tried using raycast to solve this by creating a gameobject i wanted to follow the mouse, but the problem with this is, that when i use transform.position, unity tells me that i'm unable to use vector3 as transform position.
While i've been able to get the information from the damn mouse, i'm unable to figure out a way to actually tell the object to move to the location.
So the big issue that i'm having is what to put inside the if statement. I realize that i've stored raycast in my variable, but i have no idea how to translate the contents of the variable to a float or a int or anything that i can use in combination with transform.position/translate or some other function that allows me to move my object to the mouses position.
A different thing i tried was the "lookAt", but since the mouse is on a 2d axis, it didn't really work.
Thanks in advance for the time.
Answer by syclamoth · Oct 21, 2011 at 01:38 PM
What you need is a combination between two things-
A flat plane, or ground collider
A raycast from the screen.
Now, as I understand it, you know how to get the mouse position. Now, use
Camera.main.ScreenpointToRay(mousePosition);
to get a ray, then use Physics.Raycast against the ground collider to get the point.
One thing to remember is that using this method will cause the character to look slightly at the ground, more so if the mouse is close to them. To get around this, modify the value of the returned point from the raycast by the player's position on your 'up' axis, something like this-
point = new Vector3(point.x, player.transform.position.y, point.z);
Then when you use LookAt, the player will always look around in a circle, not looking down at the ground.
Yes, that sounds right. The problem is, I am unable to translate it from theory to actual code. Not that I don't want to. What i currently have is this:
var pointPlace : GameObject;
function Update () { var ray = Camera.main.ScreenPointToRay (Input.mousePosition); if(Physics.Raycast()){
point = new Vector3(pointPlace.transform.position.x, point.y, point.z);
pointPlace being the gameobject the script is also attached to. I've tried to get this work for about 10 hours now, so it's probably right in front of me. The whole problem that I have is that I can't translate the Input.mousePosition to an actual location i tell the object to go to. Ins$$anonymous$$d i say that heres the mouse position, if raycast hits then.. Do this. The problem being that "do this", does somehow not translate to "move my object". Thanks again for the time. I'm pretty new to scripting but I really want to learn it to a point where the logics of coding - and everything i code, make full sense to me :P
Oh, you're not using Raycast properly. it should be more like this-
var hit : RaycastHit;
if(Physics.Raycast(ray, hit))
{
var turnTowardsThis = new Vector3(hit.point.x, transform.position.y, hit.point.z);
transform.LookAt(turnTowardsThis);
}
The problem is twofold- firstly, you can't use raycast without any parameters (physics doesn't know where to put the ray), secondly you need to have a RaycastHit to get information back out of the raycast! After that, you're all good.
Of course, you still need a 'ground' object, so that your raycast has something to hit. You might even want to put a layermask on the raycast, so that it only hits objects marked as 'ground'! See the documentation for more information.
Thanks a lot for the assist. I really appreciate it. If I could vote the answer up twice i would. :)
And yeah, it works now. I didn't realize that the position had to be defined within the statement. I think I have the concept mostly down now. turnTowards this is the variable that stores the location specified and then the transform lookat allows you to rotate to look at that location. Again, thanks. I was pretty lost before :P