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 mkjrfan · Oct 13, 2012 at 04:46 PM · 2dspeedplatformmovingteleport

How can I make a player teleport?

Hi! I am using javascript to code my 2d platforming game and I wanted to know how you can make someone teleport about 100 pixels away from their current position.

For example if I press space wherever the person is currently standing it would basically just shift over a 100 pixels. But if something is in between the 100 pixels like a wall it will pass through it.

Does anyone know how to do this in Javascript?

Comment
Add comment · Show 4
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 Fattie · Oct 13, 2012 at 04:47 PM 0
Share

change transform.position

avatar image mkjrfan · Oct 13, 2012 at 05:00 PM 0
Share

is there away to limit how far it can go? I did: if (Input.GetButtonDown("shiftRight")) { transform.position=Vector3(shiftDistance,0,0); }

and shiftDistanc=1 I tried making it less like .1 but it doesn't seem to make it teleport any closer

avatar image RC-1290 · Oct 13, 2012 at 05:05 PM 0
Share

You should use translate, in s$$anonymous$$d of directly changing the position, to avoid problems with the physics.

avatar image Fattie · Oct 13, 2012 at 06:44 PM 0
Share

at his level it will be no problem, RC

just do this

transform.position.x += 2.0;

see how that works for you champ !

1 Reply

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

Answer by RC-1290 · Oct 13, 2012 at 04:59 PM

For a basic teleport effect, you can simply translate the player forward.

But you mention that you want the player to teleport beyond objects that are in their way. To do that, you could cast a ray from the player's initial position, to the target position. If an object is hit, you find the first clear area beyond the object, to where the player can stand again.

One basic method to do this, would be to fire a ray from 1 meter beyond the original target, back to the player's position, then 2 meters, then 4 (doubling the distance every time), until the distance from where the ray is fired to the first object that is hit, is high enough for the player to fit there.

You could also come up with different or more complex solutions, to make sure you don't teleport through multiple objects.

[edit] Another method for finding a place beyond the object: Fire a ray from within the object in the direction the player is teleporting in, ignoring that first obstacle, then fire a ray back towards to the original player position, to ensure there is enough room for the player to fit there.

Comment
Add comment · Show 4 · 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 mkjrfan · Oct 13, 2012 at 05:37 PM 0
Share

is there any way to detect collision without rays? Or ins$$anonymous$$d make the rays form a collision box of where the player could land so if a block is in there it can't teleport.

avatar image RC-1290 · Oct 13, 2012 at 06:01 PM 0
Share

Capsule Cast is probably what you are looking for. It basically works like a really big ray. Capsules are often used as stand-in for the player, because you can make it roughly the size and shape of a human, while it costs a lot less CPU time than using the actual player mesh.

The other colliders in Unity are most useful for its built in collision detection. Those collisions are automatically calculated each Fixed Update. With capsule cast or one of the other methods, you don't have to wait for the next frame, before getting results.

avatar image Fattie · Oct 13, 2012 at 06:45 PM 0
Share

@mkjrfan , it will be almost impossible for you to move ahead unless you get really good at using raycasting

it is used everywhere, constantly, in games I'm afraid to say !

good luck !

avatar image mkjrfan · Oct 13, 2012 at 07:09 PM 0
Share

thanks for the help!

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

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need help with a relatively simple issue. 1 Answer

Increase variable for a few seconds 2 Answers

Cant get moving platform to go the other direction 1 Answer

Setting Velocity of Rigidbody to Mouse Speed 1 Answer

Make player automatically move up on a tile 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