Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Celludriel · Apr 19, 2017 at 11:42 AM · 3dpositioningrelative position

Find a position forward of GameObject with possible angle

This question has been asked in one way or another over several years but none of the responses on them seem to give me the required result. Most of the time because the question is not clear enough either. So here goes my attempt in trying to ask clearly what the problem is.

I have a GameObject, in this case a capsule mesh, sitting at coordinates (0.0,1.0,-20.0) with rotation (0,0,0). I want to rotate and move to a position 10f units forward in an angle. Here are a couple of use cases:

move 10f forward in a 0° angle move 10f forward in a 15° angle move 10f forward in a -15° angle

So I need to find the target destination relative from (0.0,1.0,-20.0) to distance forward in x angle. I have tried a couple of implementations based on past answers. Lets take the basic use case 10f on a 0° angle so basically 10 units forward. So the end destination should be (0.0,1.0,-10.0)

this one gives me as destination (0.0, 0.0, 4.0). I lose my y axis from 1 to 0 and the z coordinate is way of.

 private void setDestinationAndDistance(int angle, float distance)
 {
     Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
     this.destination = rotation * (Vector3.forward * distance);
 }

This one gives me (1.0, 0.0, 3.9). Again I lose my y axis and it's still wrong with all the rest

 private void setDestinationAndDistance(int angle, float distance)
 {
     this.destination = Quaternion.AngleAxis(angle, Vector3.up) * Vector3.forward * distance;
 }

Next I tried another proposed solution ending me up with (0.0, 0.0, 4.0) which seems familiar to my first result

 private void setDestinationAndDistance(int angle, float distance)
 {
     Quaternion startAngle = Quaternion.Euler(0, angle, 0);
     Quaternion qAngle = transform.rotation * startAngle;
     this.destination = qAngle * Vector3.forward * distance;
 }

I then turned to old circle math with radians to find an x, y coordinate in a Carthaginian grid. This works but it does not cover my usecase, this only works if my GameObject is pointing directly "North" aka 0° to begin with. If it would have been pointing 15° to the right and I do this on a 15° turn I won't end up 30° from north but yep only 15°

 private void setDestinationAndDistance(int angle, float distance)
 {
     float radians = angle * Mathf.Deg2Rad;
     float y = Mathf.Cos(radians);
     float x = Mathf.Sin(radians);

     this.destination = transform.position + (new Vector3(x, 0, y) * distance);
 }

This is where I realized I need more help and started another one of these questions, cause none so far are yielding desired results. Can anyone explain how to do it properly, keeping in mind I'm not native English and certainly won't understand complex English math terms without wiki every one of them, where they are probably explained with even more complex English math terms ... So math for dummies would apply here.

Comment
Add comment · Show 6
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 Celludriel · Apr 17, 2017 at 12:28 PM 0
Share

I believe I found the answer I haven't tested all use cases yet but consider this function

     private void setDestinationAndDistance(int angle, float distance)
     {
         // local coordinate rotation around the Y axis to the given angle
         Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.up);
         // local coordinate to world coordinate rotating direction forward
         Vector3 translateToWorldCoordinates = transform.TransformDirection(rotation * transform.forward);
         // add the desired distance to the direction
         Vector3 addDistanceToDirection = translateToWorldCoordinates * distance;
         // add the distance and direction to the current position to get the final destination
         this.destination = transform.position + addDistanceToDirection;
     }
avatar image Celludriel · Apr 17, 2017 at 09:31 PM 0
Share

Well it almost worked, I put this one a cube mesh and the cube moves along the points if I do not rotate the cubes and they stay perfect at rotation 0,0,0 once I start to rotate the cube to the position they are going things go wrong with subsequent calls. The target destinations start jumping all over the place after three calls. I tested this with 12 calls with 30 degrees which should move the cube in a circle, however this is not what happens. It's hard to describe what is going on but it seems the angles start jumping around. So this isn't the solution to the problem yet :(

avatar image jobigoud · Apr 19, 2017 at 05:52 PM 0
Share

Are you sure your GameObject isn't child to an object that has a scale component to its transform, or that there isn't scaling anywhere in its hierarchy?

avatar image Celludriel · Apr 19, 2017 at 06:10 PM 0
Share

There was some actual scaling it was more a long brick then a cube 1, 1, 2 was the scaling but I found the solution a small while ago, it's the current accepted answer. I believe it is correct

avatar image jobigoud Celludriel · Apr 19, 2017 at 06:24 PM 0
Share

Hum, I can't see any answer. $$anonymous$$aybe it's in the mod queue.

avatar image Celludriel jobigoud · Apr 20, 2017 at 06:12 AM 0
Share

Well they removed the answer it seems , maybe I can't answer my own question like on stackoverflow ? When I get home from work I'll try posting it again. The solution seems to work, my cubes move as they should.

1 Reply

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

Answer by Celludriel · Apr 20, 2017 at 04:56 PM

Turns out I didn't need to transform to world coordinates. This solution seems to work. I tried with several degrees

      public void Move(float degrees, float distance)
      {
             // local coordinate rotation around the Y axis to the given angle
             Quaternion rotation = Quaternion.AngleAxis(degrees, Vector3.up);        
             // add the desired distance to the direction
             Vector3 addDistanceToDirection = rotation * transform.forward * distance;
             // add the distance and direction to the current position to get the final destination
             this.destination = transform.position + addDistanceToDirection;
             Debug.DrawRay(transform.position, addDistanceToDirection, Color.red, 10.0f);
             transform.LookAt(this.destination);
     }

I tried to understand Quaternions from wiki and some other sources but I just can't get a grip on complex numbers, so might be above my head. I hope this will help people finding locations from one spot to another in an angle.

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How would you sort gameobjects by their position in a 3D grid? 1 Answer

How can I move this part in a rectangle when I press a key? [C#] 1 Answer

How can I set the default position of a 3D object? 2 Answers

Placing object based on child relative to another gameObject. 1 Answer

Position next to other item 0 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