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 Karsnen_2 · Mar 04, 2013 at 06:22 PM · rotationiosvector3logic

Help to figure a logic to achieve a movement.

Hello ,

Plot :

I have a bird. This bird revolves around a trunk of a tree. At certain point, this bird changes the tree. On the scene there are more than 2 trees. Hence let's say I have 3 trees. At certain point, the system generates a random tree (as the target) and the bird moves towards it. Once it reaches a specific distance from the tree, the bird starts revolving around the tree.

Constraints :

I would like the bird to be perpendicular to the trunk when it flies around; and face the trunk when it is moving towards the trunk until it reaches the specific distance.

Flexibility : The bird while flying around could rotate clockwise or anti-clockwise direction around the trunk of the tree.

Problem : I really do not know to rotate the bird while flying either towards the trunk or rotating around the trunk. The alignment of the bird is the issue I am facing.

CODE :

 // Inside the update function of the bird
             if(Vector3.SqrMagnitude(Trunks[TrunkIndex].position - this.transform.position) < 1000)
             {
                 this.transform.RotateAround(Trunks[TrunkIndex].position, Vector3.up, 20*Time.deltaTime);
             }
             else
             {
                 this.transform.localRotation = Quaternion.LookRotation(Trunks[TrunkIndex].position, Vector3.up);
                 this.transform.position = Vector3.Lerp(this.transform.position, Trunks[TrunkIndex].position, (Time.deltaTime));
             }

Request :

I am not sure as of how to approach this.

Your help is always appreciated.

Thank you,

Karsnen.

IMAGE: This is a top-down view.

Top - Down View

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

2 Replies

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

Answer by sparkzbarca · Mar 04, 2013 at 06:45 PM

if the bird is rotating clockwise then the right side of the bird should face the center, otherwise the left for counter-clockwise. keep that in mind for the next part.

to make one facing of an object align with the facing of another.

this is code i wrote to take the 2 nearest facings of 2 objects and rotate them to face (be parallel to eachother

 /* Function:
  *             To take 2 objects and rotate object Rotate such that its face closest to SnapTo
  * Params:
  *        Rotate: The object that you want to rotate.
  *           SnapTo: The object you want Rotate to snap to.
  *
  */
 public class AutoSnap : MonoBehaviour
 {
         Vector3 axis;
         float angle;
         Vector3 direction;
         Ray ray;
         RaycastHit SnapToHit;
         RaycastHit RotateHit;
         Vector3 MoveDistance;
         AutoSnappedMove MoveComponent;
     void Start(){
         MoveComponent = GetComponent<AutoSnappedMove>();
         ray = new Ray();
     }
     
     public void SnapToNearest (GameObject Rotate, GameObject SnapTo)
     {
         ray.origin = Rotate.transform.position;
         ray.direction = SnapTo.transform.position - Rotate.transform.position;
         Physics.Raycast (ray, out SnapToHit);
         
         ray.origin = SnapTo.transform.position;
         ray.direction = Rotate.transform.position - SnapTo.transform.position;
         Physics.Raycast (ray, out RotateHit);
         
         axis = Vector3.Cross (RotateHit.normal, -SnapToHit.normal);
         if (axis != Vector3.zero) {
             angle = Mathf.Atan2 (Vector3.Magnitude (axis), Vector3.Dot (RotateHit.normal, -SnapToHit.normal));
             Debug.Log(Mathf.Rad2Deg);
             Debug.Log(axis);
             if(angle > .5f)
             {
                 Rotate.transform.RotateAround (axis, angle);
             }
         }
         MoveComponent.Move (RotateHit, SnapToHit);
 
     }

in this case rotate would be the bird and snapto would be the tree (this code is designed to make 2 objects snap together but it will work just fine for what you want which is to snap together at a distance)

you'll want to replace

MoveComponent.Move (RotateHit, SnapToHit); (the last line of code)

with your own movement code (presumably the transform.rotatearound())

these lines

 ray.origin = SnapTo.transform.position;
 ray.direction = Rotate.transform.position - SnapTo.transform.position;
 Physics.Raycast (ray, out RotateHit);

are what work for myself. they find the NEAREST FACING of the moving(in your case bird) object. You may want to change it to just the left or right side of the bird using a boolean (clockwise or counter clock) to determine which side

in that case you would remove that code and just do

axis = vector3.cross(transform.right/-transform.right, snaptohit.normal)

the tranform.right or -transform.right of course depending on left or right side of bird.

when you move you'll want to set the distance from the trunk at some point thats easy just pick a distance say 3 and begin the rotate when it's approx 3 away as it moves in.

alternatly you can do

ray.direction = (bird.position - trunk.position) ray.origion = trunk.position

ray.getpoint(distance) or for a distance of 3

bird.transform.position = ray.getpoint(3);

also you could instead of moving towards the trunk you could slerp towards this point that is 3 units along the line drawn between the far trunk your aiming for and the bird however you would need to do a check to make sure the ray didnt pass through the tree it's already currently circling (and if it did just call a wait command and wait half a second for the bird to circle more so it's not in the way and try again)

otherwise it'd fly through the tree its already around and look wierd

Comment
Add comment · Show 1 · 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 Karsnen_2 · Mar 05, 2013 at 03:43 PM 0
Share

Thank you very much sparkzbarca. I highly appreciate your help. I took yours and fiddled a little bit here and there due to more requirements and it is working.

avatar image
1

Answer by robertbu · Mar 04, 2013 at 07:08 PM

When the bird first select a new tree, calculate a target to the side of the tree. You can calculate the target position by taking the cross product between the vertical of the tree and the vector between the bird and the tree and multiplying the result by how far you want the bird to be away from the center of the tree. Something like:

 v3T = newTreePos - birdPos;
 v3TargetPosition = Vector3.Cross(v3T, Vector3.up).normalized * birdDistanceFromTree;

This position will be where he flies to, but it is also the position you can use for transform.LookAt(). Also you can randomly select between using Vector3.up and Vector3.down to randomly indicate which side of the tree to fly around.

When the bird reaches the position, you can begin the transform.RotateAround(). Since the bird will be tangent to tree, you won't have to rotate the bird at all.

Comment
Add comment · Show 2 · 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 robertbu · Mar 04, 2013 at 07:23 PM 0
Share

I edited out a portion of my original answer. There is one more area to be addressed: when on the rotation around the tree does the bird stop rotating and starting heading to the new tree. Immediately after the bird starts the RotateAround() in the current tree, pick the new tree. Generate a new target using the the logic above, but use the vector between the current tree and new tree rather than the vector from the bird to generate the position. When the the angle between transform.forward vector of the bird and the vector from the bird to the position drops below some threshold, the bird flies to the next tree. Actually, you could use the tree vector rather than the bird vector in the calculation above as well.

avatar image Karsnen_2 · Mar 05, 2013 at 03:42 PM 0
Share

Thank you very much Robertbu. Appreciate your 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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Workaround for Quaternion.eulerAngles 360 Degree Limit? 1 Answer

Applying force with respect to angle with the ground? 1 Answer

Turning Vectors.... 2 Answers

Ride across a tree? 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