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 SrBilyon · Jul 02, 2012 at 08:23 AM · aicharactercontrollercontrollerrotateorbit

RotateAround and Character Controller

I'm trying to create a circling behavior for my AI that uses a character controller. I've created a Seeking and Fleeing behavior that involves modifying the agent's forward direction, storing it, and using it inside: CharacterController.Move(direction);

I'm not sure how to mathematically have the agent orbit around the target, so I wanted to use Transform.RotateAround, but then physics wouldn't be checked by the character controller, and I can't store the result of RotateAround, because it returns void. I also want to make sure that I have the AI facing his forward direction still.

This is the code I have so far, excluding the use of Controller.Move:

  transform.RotateAround(targetPos, Vector3.up, projectedDistance * Time.deltaTime);

Any help with having my AI with a character controller rotate around a target?

=========================================================

UPDATE:

Thanks to whydoidoit, i'm getting closer to solving it. The agent moves in a circular motion while moving towards the target, but instead of a constant orbit, it stops when it gets close to the target, then runs in place.

Here is the code I have:

      public bool Circle(Vector3 targetPos)
             {
                 var tempDir = (targetPos - trans.position);
                 direction = new Vector3(tempDir.x, direction.y, tempDir.z);
     
                 var rotDirection = RotateAbout(direction, targetPos, Vector3.up, 1);
     
                 if ((tempDir.magnitude > minDistance) && tempDir.magnitude < safeDistance)
                 {
                     ObstacleAvoidance(ref direction, new Vector3(2f, 1, 2f), checkObstacles);
                //Applies gravity and stuff
                CharacterBasics();
     
                     //Check for potential pitfalls
                     RaycastHit hit;
                     if (Physics.Raycast(trans.position + trans.forward, Vector3.down, out hit, 3))
                     {
                         targetSpeed = maxSpeed;
                         controller.Move((rotDirection) * Time.deltaTime);
                     }
                     else
                     { targetSpeed = 0; }
                     return false;
                 }
         }
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 whydoidoit · Jul 02, 2012 at 08:36 AM 0
Share

Well the result of RotateAround is the value of transform.position afterwards

avatar image SrBilyon · Jul 02, 2012 at 09:02 AM 0
Share

I'm not sure what you mean. The compiler tells me it returns void, unless I'm suppose to be writing it out another way.

avatar image whydoidoit · Jul 02, 2012 at 09:03 AM 0
Share

I mean that after you do a transform.RotateAround the result is the new transform.position. It updates transform.position.

avatar image SrBilyon · Jul 02, 2012 at 09:06 AM 0
Share

That's correct, though, I want to know if there is a way I can store that change in transformation and place it inside Controller.$$anonymous$$ove, because just changing the position ignores collision checking.

That, or would you happen to know how transform.RotateAround actually performs the rotation mathmatically?

avatar image whydoidoit · Jul 02, 2012 at 09:34 AM 0
Share

Right so the point it is rotating about is a reducing direction at the moment. Rather than a physical point. Is that what you meant?

Show more comments

1 Reply

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

Answer by whydoidoit · Jul 02, 2012 at 09:09 AM

Well you could store the transform.position before you do RotateAround, then work out the difference afterwards.

Or you could use this code which returns a new position that you have to set yourself:

    static function RotateAbout(position : Vector3, rotatePoint : Vector3, axis : Vector3, angle : float) : Vector3       {
           return (Quaternion.AngleAxis(angle, axis) * (position - rotatePoint)) + rotatePoint;

    }
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 SrBilyon · Jul 02, 2012 at 09:10 AM 0
Share

Now THAT'S what I'm looking for! Thanks! I'll try this now :)

avatar image SrBilyon · Jul 02, 2012 at 09:27 AM 0
Share

I've updated the question to give information on my current problem. Your code has helped out a lot though :D

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotate character controller on all axis 0 Answers

rotate around character 1 Answer

Having trouble making my camera orbit an object 2 Answers

Can you help me make this code turn smoothly? 0 Answers

Debug.DrawRay Issue? 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