Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JustinC · Dec 03, 2014 at 09:37 AM · c#raycastteleportteleporting

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by MuffinMyst · Dec 03, 2014 at 09:57 AM

Mouse button input RayCast

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MuffinMyst · Dec 03, 2014 at 10:02 AM 1
Share

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

avatar image Lunatix · Dec 03, 2014 at 10:30 AM 0
Share

Have a look at this question, too: http://answers.unity3d.com/questions/540888/converting-mouse-position-to-world-stationary-came.html

avatar image JustinC · Dec 03, 2014 at 09:08 PM 0
Share

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?

avatar image MuffinMyst · Dec 03, 2014 at 10:19 PM 0
Share

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.

avatar image MuffinMyst · Dec 03, 2014 at 10:30 PM 0
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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

2d game Teleporter works only as a single instance 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges