Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Cazhero · Oct 02, 2020 at 08:40 AM · rotationraycastraycasthitobject position

Make 2 object not go into each other when modifying one object's position

Hi I'm not sure how the rooms work, so I put it on default..

I'm basically trying to get my "character" (for now it's an egg) to move all around the inside of a pipe; https://imgur.com/a/eQAXHsA

The code I tried to make was the following:

 public class EggControl : MonoBehaviour
 {
     public Transform midPipe;
     public float rayDownDistance;
     public float rayUpDistance;
 
     // Update is called once per frame
     void Update()
     {
         RaycastHit hitDown;
         RaycastHit hitUp;
 
         //Raycast from object perspective-------------------------------------------
 
         if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), out hitDown) &&
             Physics.Raycast(transform.position, transform.TransformDirection(Vector3.up), out hitUp))
         {
             rayDownDistance = hitDown.distance;
             rayUpDistance = hitUp.distance;
 
 
             Debug.DrawRay(transform.position, hitDown.point, Color.red);
             Debug.DrawRay(transform.position, hitUp.point, Color.green);
 
             transform.localPosition = new Vector3(hitDown.point.x, transform.position.y, transform.position.z);
         }
 
         if (Input.GetKey(KeyCode.A))
         {
             transform.Rotate(0, 0, -1);
         }
         if (Input.GetKey(KeyCode.D))
         {
             transform.Rotate(0, 0, 1);
         }
 
         Debug.Log("update is going through");
   }
 }

So basically I change the x.position of the main object when the ray is cast from underneath, but once it gets onto another wall it has to change both x and y, but if I put the egg precisely on the hit.point it'll get stuck in the pipe instead. Is there a way so that the main object stays outside the pipe and never actually goes into it? Or is there a better way to approach this?

I have tried to get the middle of the pipe and cast a ray from there, but than first of all, the ray gets cast into the wrong direction somehow; https://imgur.com/a/eQAXHsA . And second the movement is completely incorrect and I can't tell why (it starts at the bottom => goes up automatically => resets down => goes up automatically => resets down => goes up automatically => resets down => etc.

 //Raycast from midPipe perspective---------------------------------------------------------------
 
         if (Physics.Raycast(midPipe.position, midPipe.TransformDirection(midPipe.up * -1), out hitDown))
         {
             rayDownDistance = hitDown.distance;
 
             Debug.DrawRay(midPipe.position, hitDown.point, Color.red);
 
             transform.position = new Vector3(hitDown.point.x, hitDown.point.y, transform.position.z);
         }
 
         if (Input.GetKey(KeyCode.A))
         {
             midPipe.Rotate(0, 0, -1);
         }
         if (Input.GetKey(KeyCode.D))
         {
             midPipe.Rotate(0, 0, 1);
         }

Anybody got a clue on how I could approach this?

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

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

224 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 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 can you rotate a raycast on input? 1 Answer

Help finding out which side of a cube a raycast hits 1 Answer

Using Raycast Normal to match surface rotation to instantiated gameobject? EXAMPLE GIF Included 0 Answers

Quarternion.LookRotation is only rotating my game object on one axis 1 Answer

default cylinder to indicate hit.normal 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