- Home /
Teleporting a character to mouse position
Hello, I am fairly new to Unity, I have done many of the tutorials and now I am trying to reach out on my own from scratch(Much more difficult than I thought). I am trying to make a simple game were Third Person runs through a maze and teleports to different layers or areas. He will teleport where ever the user clicks. How do I go about doing this. I figure I have to use a Ray of some sorts but every thing I have tried doesn't seem to work out. I know how to set the position of the character and change the position, just not how to pick up the X,Y,Z of my mouse at the time of a click. Any help getting pointed in the right direction would be appreciated. I have been learning in C# and would like to try to stay in C# if possible. Also I am not asking for you to do this for me but explain how to go about doing this so I know how to apply it to other projects later on.
Answer by MuffinMyst · Dec 03, 2014 at 09:57 AM
This link is a start.
Then there is this unity Answer that I think should help, if not solve your problem.
Detect Mouse Click coordinates on terrain in C
I'm not too fluent with RayCasting either though, so if you have further questions for clarification... perhaps other members can chime in too.
When you do (if you do) manage to get coords from the Raycast hit, remember to add the height of the character to the location/coords for movement (to make sure you don't fall through the terrain)
Basically the idea of ray casting is... it shoots an invisible Ray (the first link shows how to cast from mouse click (basically shoots from the camera object) to terrain/object. There are other uses for Raycast, and they can be shot from any position. (ie. objects, characters, collisions... etc)
if the "ray" you "cast" hits something, that object that was hit will have specific properties that you can look at. (It's position, its rotation, it's (if any) tag,)
Hope this helps
Have a look at this question, too: http://answers.unity3d.com/questions/540888/converting-mouse-position-to-world-stationary-came.html
So I think I might be getting close to making this work. The only problem I am running into (and correct me if I am wrong) is that I need to hit an object with a rigid body, I don't want to have to hit an object, just space. Is there a way around this?
I don't think a rigid body is required, just a collider. But perhaps I misunderstood the teleporting you want to do. Will they stay level with the ground? (then the collider on the terrain would be what is hit) or do you want them teleport to a point in space (so could be several feet above the terrain)? The later would be significantly harder to do with a mouse click, its converting 2d xy coord into 3d xyz. With the terrain as a collider, it helps to define that, but if in open space... how would the "ray" know when to stop? I suppose you could have a "max" distance that the ray would shoot, and if you didn't hit anything, it would use the coods as if at the "end" of the ray... but the math would be significantly harder....
If you wanted them to always pop out above the terrain... like... look up... click a point... and teleport there.... it'd still be a bit wonky, but you could have a mesh (sphere or box) collider with no mesh renderer.... but that might be a bit hard to predict... again, going back to how you would want it to convert an xy to an xyz without an object (or collider) to assist in defining it.
Added: as far as hitting an object such as the terrain, unity allows the terrain to have it's mesh broken up by triangle segments, so the ray would "hit" that triangle at that particular coord, and use location/rotation for that surface. I think a bit more code... I have something similar in my game, but it is a script I got off of here... so I'll have to go look at it again to better explain it.
yea, my code shoots a ray from the player directly forward. Then it uses hit.point to get the position of the surface hit with the ray. Then does a lerp transform (so its a smooth move rather than instantaneous movement) (it also uses the hit.normal to get the orientation of that surface... my player walks on walls ;) Do you have an example of the code and set up you are using? I (or someone else here) might be able to pin point what step(s) you might be missing
Your answer
Follow this Question
Related Questions
Teleporting Objects,Teleporting Object 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
First person Teleportation 2 Answers