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 directusgames · May 08, 2015 at 03:13 PM · 2dsmoothtransitionorbit

(2D) How do i create a smooth movement transition from a point to another point on an orbit?

Edit: Refined question at end.

I have an enemy that spawns at the edge of my screen and makes it way in the direction of the player. Once it reaches a certain distance from the player it will begin an orbit around them.

Any suggestions on how I can make the transition from "approaching player" to "orbiting player" smooth? I'll see if I can illustrate what I mean:

alt text

Ideally the steps would go something like this:

  1. Enemy spawns

  2. Set distance from player

  3. Approach this distance

  4. Set point in orbit (potentially this should be a set distance away from where the 'approach' ends)

  5. Begin smooth transition to this point.

  6. Once point is reached, begin orbit.

Currently the enemy will hit the orbit range and begin orbiting straight away, making it look like it hits an "invisible wall" briefly.

My thoughts are I could use something like a Bezier curve for the smooth transition, but I'm not sure how to go about getting a point on the orbit to set as an end point for the curve.

This is the code i'm currently using to orbit once the enemy reaches a certain distance from the player:

 transform.RotateAround(target.transform.position, new Vector3(0,0,1), (orbitSpeed * Time.deltaTime));

Additionally, this is all in 2D.

Hope that's enough information, I don't necessarily need code, but more ideas on how this can be done: anything is greatly appreciated!

Edit: I've solved the issue of getting a point on a circle. Specifically, I divided by circle into quadrants to get a random point along the circle in a certain quadrant.

 if(quadrant == 1)
 {
     angle = Random.Range (90, 180);
 }
 else if(quadrant == 2)
 {    
     angle = Random.Range(0, 90);
 }
 else if(quadrant == 3)
 {
     angle = Random.Range(180,270);
 }
 else
 {
     angle = Random.Range(270, 360);
 }

 Vector3 circ = new Vector3(Mathf.Cos (angle*Mathf.Deg2Rad), Mathf.Sin (angle*Mathf.Deg2Rad), 0);
 
 orbitPoint = target.transform.TransformPoint (circ * radius);

My question now is: With a starting position vector and a distance, how can i get a point where the start position + distance would intersect with a point on the circle? Illustration:

  ![alt text][2]
         


[2]: /storage/temp/46036-circlepoints.png

orbittransition.png (11.4 kB)
circlepoints.png (6.9 kB)
Comment
Add comment · Show 2
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 fighder · May 08, 2015 at 03:55 PM 0
Share

If I were you, I would set up an inner circle trigger collider and an outer one around the player.

The enemy runs towards the player, but when it collide with the outer trigger collider, it starts rotating around the player, still try to run to the player. When it hits the inner trigger collider, it stops running, but the rotation continues.

avatar image directusgames · May 08, 2015 at 05:49 PM 0
Share

Ooh this sounds good! I'll give it a try

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Mike-Geig · May 08, 2015 at 06:18 PM

If you already have your orbiting code working the way you want, you can do this to remove the "invisible wall" problem. These steps assume a correct distance from player of 10 units (substitute your own number) and that you are setting the velocity of your object:

1) begin your orbiting code when the object is not quite in range of the player, say 11 units away 2) Once the object is within some distance (again, we'll say 11 units), modify its velocity each FixedUpdate to be (I am assuming a normal speed of 1 unit a second):

 1.0f * (distanceToPlayer - 10.0f); //change the 10 to be what your distance should be

3) Now, as the object transitions from 11 units to 10 units away, it will slow down while the orbit code kicks in. That will give it a nice smooth transition.

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 directusgames · May 08, 2015 at 07:47 PM 0
Share

Thanks for your input!

I'm currently using transform.position to move my object, rather than velocity. I've tried to adapt your code for use in this way but still seem to be getting that 'wall' issue. I'll elaborate a bit more on my code below and maybe you can provide some insight into what I'm doing wrong.

So I begin by spawning my enemy, setting it to face a point on the orbit circle (which has a radius of 3.5 units).

 Vector3 dir = orbitPoint1 - transform.position;
 float lookAngle = $$anonymous$$athf.Atan2(dir.y, dir.x) * $$anonymous$$athf.Rad2Deg - 90;
 transform.rotation = Quaternion.AngleAxis(lookAngle, Vector3.forward);

It then begins to head in this direction

 transform.position += (orbitPoint1 - startPos) * speed * Time.deltaTime;

Once it reaches a distance of 4.5 units from the player it should begin to transition towards the point smoothly. I think what you're suggesting is I do this by beginning the orbit now, while still moving towards the player until it reaches the actual orbit circle.

 Vector3 distVec = transform.position - target.transform.position;
 dist = distVec.magnitude;
 
 if(dist <= 4.5)
 {
     transitioning = true;
     approaching = false;
 }
 
 //once transitioning is true it will call this each FixedUpdate:
 
 transform.RotateAround(new Vector3(0,0,0), new Vector3(0,0,1), (orbitSpeed * Time.deltaTime));        
 Quaternion rotation = Quaternion.LookRotation(target.transform.position - transform.position, transform.TransformDirection(Vector3.up));
 transform.rotation = new Quaternion(0, 0, rotation.z, rotation.w);
 
 transform.position += (target.transform.position - transform.position) * speed * Time.deltaTime;
 
 
 

I'm pretty sure this is where the problem is, but I can't quite wrap my head around why it doesn't work. Thanks for your help so far!

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

Smooth transition for RotateAround 1 Answer

Is it possible to turn off/suppress event calls in an animation at runtime? 2 Answers

Parameterize the Animators transition condition (in 2D) 1 Answer

Can anyone tell me what I'm doing wrong here? 1 Answer

why does if statement ignore bool? 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