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 EpicGamer545 · Nov 28, 2021 at 08:29 AM · transform.positionteleportingempty game object

Getting stuck in the ground while teleporting

Hi I am new to Unity. I am making a project with a teleportation function like ender pearls in Minecraft. I used a game-object as a target, eg. -

 RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, transform.up, distance, solidChecker);
         if (hitInfo.collider != null)
         {
             if (hitInfo.collider.CompareTag("Ground"))
             {
                 Debug.Log("Teleport");
                 tel = true;
 
                 telPoint.transform.position = transform.position;
             }
 
             else
                 tel = false;
 
             DestroyProjectile();
         }

And another script in the player -

  public void boolTeleport(bool tel)
     {
         if (tel == true)
             shouldTel = true;
 
         else
             shouldTel = false;
 
         if (shouldTel == false)
             tel = false;
     }
 
     private void Update()
     {
         if(shouldTel == true)
         {
             transform.position = telPoint.transform.position;
             shouldTel = false;
         }
     }
 }

So these two scripts have a GameObject variable -

     public GameObject telPoint;

And in Unity, I added an empty object prefab as telPoint, as the projectile is a prefab, so I can't add an empty object directly.

Now, sometimes, if I throw the crystal on the ground and jump at the same time, I safely teleport above ground. Even without jumping I sometimes teleport above ground. But if I throw it at the side of a tile (of a tilemap) it teleports me inside the ground, where I can only move horizontally, and cannot jump or throw crystal outside. And if I throw it far away, it teleports me inside the ground almost always. What to do now?

Comment
Add comment · Show 12
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 MilitaryG · Nov 28, 2021 at 09:41 AM 0
Share

I'm lost in what script it doesn't work?

How is this teleporting supposed to be?

I think I need a little bit bigger code to understand the codeflow at the raycast

I don't know Minecraft.

avatar image MilitaryG · Nov 28, 2021 at 09:51 AM 0
Share

I think it should be like this:

 telPoint.transform.position = hitInfo.position + Vector2.Up*distanceFromGround;
avatar image EpicGamer545 MilitaryG · Nov 28, 2021 at 10:11 AM 0
Share

What is DinstanceFromGround, and how to use it?

avatar image AaronBacon EpicGamer545 · Nov 28, 2021 at 11:17 AM 1
Share

Basically the problem is that the "position" of an object is usually its center, so by setting the position of an object to the ground, you're teleporting its bottom half into the ground, and by putting it onto the wall, you will almost certainly end up in the wall most of the time. This answer is trying to account for that by offsetting the point of teleport upwards a little before you teleport. It's not really the best solution but it's definitely the simplest, and if it works for your case, then there's no problem.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

132 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I teleport one Object to an emptyGameObject? 1 Answer

teleport an object three quarters of its distance with the player after a certain time 2 Answers

How to add a teleportation mechanic like ender pearls in Minecraft 0 Answers

How do I enter/exit structures? 1 Answer

how do i teleport my player to an empty object? 3 Answers


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