- Home /
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;
}
}
Well the result of RotateAround is the value of transform.position afterwards
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.
I mean that after you do a transform.RotateAround the result is the new transform.position. It updates transform.position.
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?
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?
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;
}
Now THAT'S what I'm looking for! Thanks! I'll try this now :)
I've updated the question to give information on my current problem. Your code has helped out a lot though :D
Your answer
Follow this Question
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