- Home /
characterController rotate around an object help!
Is there a way to rotate an Object around a targetObject using the characterController.move(Vector3) function rather than the transform.rotation/transform.rotate function?
Why are you so desperate to use a 'move' function to do rotation? Doesn't that seem a little strange to you?
because I made a finished script on zoom pan dolly that I attached to a camera when the camera hits other game objects it does'nt pass through it because of the characterController that I attached to It, I used characterController to move the camera on its, zoom pan dolly functionality the only thing lacks right now is the orbit function that is the only thing right now that I'm looking for to orbit the camera around a target object using characterController.move() because this avoid the camera to pass through objects and see through it
I also tried to move it using transform.rotation and transform.rotate(), but the camera looses it's collision on other objects it see's through objects, I have tried to attached rigid body and collider to it but it fails, because when it hits other game objects it goes crazy, goes wild rotating rapidly unexpectedly and sometimes bounces
that's why I'm crazy knowing about how to orbit a an object to a target object using the characterController.move()
Answer by aldonaletto · Nov 12, 2011 at 01:12 AM
You can make the character follow a perfect circle around the target, but if there's some obstacle the character may get stuck. A better approach is trying to move in a circle, but allowing eventual trajectory changes due to obstacles - this can modify the circle radius sometimes.
The idea is to calculate a curPos vector from the target to the character, then save in newPos this vector rotated by an angle proportional to Time.deltaTime: newPos - curPos is the distance the character must move in this frame. If some object is hit, the character may move closer to or farther from the target, and this new distance will be kept until another obstacle is hit.
I included gravity effects in the script below; just comment this line out if you don't want gravity.
public Transform target; public float speed = 30f; // speed in degrees per second public float gravity = 10f; // gravity, if you want to use it
CharacterController character;
// call this function in LateUpdate void RotateChar(){ // make sure the variable "character" contains the controller: if (!character) character = GetComponent(CharacterController); // curPos = vector from target to character: Vector3 curPos = transform.position - target.position; // rotate curPos by the appropriate angle and save in newPos: Vector3 newPos = Quaternion.Euler(0, speed Time.deltaTime, 0) curPos; // calculate the displacement to move Vector3 displacement = newPos - curPos; // if you don't need gravity, comment out this line: displacement -= gravity Vector3.up Time.deltaTime * Time.deltaTime; character.Move(displacement); // move by the distance calculated transform.LookAt(target); // keep looking to the target }
@aldonaletto thank you for this (it works nicely) but how could i get to it to honor a fixed distance from the target? (i.e. keep the circle radius from changing, even if that means the character gets stuck)