0
\$\begingroup\$

My player has the following hierarchy:

Hero (GameObject)
      Model (child of "Hero", centered at 0-0-0)
      Camera (child of "Hero")

The camera should be allowed to

- change its position around the Model in a circle ("rotate around the 
  center")
- rotate so that it always faces the Model
- move up and down
- move nearer and further from / to the Model

I know how to do that using Quaternions, but as I'm doing this in a coroutine and using Lerp, I found myself in situations where the camera would get right through the model as this was the shortest path between the destination and the target vector.

I would now like to use pure angles (Sin and Cos), preferrably without LookAt. Can somebody share how I would position and rotate the camera according to the angle, perhaps even taking a camera height variable into account as the camera should also be able to move up and down, still facing the player?

That would be the bomb!

\$\endgroup\$
1
\$\begingroup\$

Have normalized vector from hero towards camera. Rotate the vector. Place camera at multiplied vector by range(distance). Rotate camera towards hero. Basically rotation around is placing object at the same distance but moving that object in some direction.

Transform.RotateAround

Basically point - is hero position, axis is usually up, if your hero is in vertical position, and angle.

Camera.transform.RotateAround(hero.position, Vector3.up, rotationAngle * rotationSpeed);

Result reference - basically this is how I made planets rotate around star. You can access the whole project via link under video.

public void RotateAround(Transform transform)
{
    this.transform.RotateAround(transform.position, this._rotationAxis, this._rotationSpeed);
}

private void Update()
{
    this.RotateAround(this._star);
}

private void Awake()
{
    this._rotationAxis = Quaternion.Euler(0f, 0f, 90f) * (this.transform.position - this._star.position).normalized;
}

If you want to do it manually you can just store rotation angle, and change it. Using this method you can calculate the vector. Though, it's more for Top-Down, you will have to modify the method to suit your needs.

public static Vector3 GetDirectionFromAngle(float angleInDegrees, float localRotationAngle = 0)
{
    angleInDegrees += localRotationAngle;
    return new Vector3(Mathf.Sin(angleInDegrees * Mathf.Deg2Rad), 0, Mathf.Cos(angleInDegrees * Mathf.Deg2Rad));
}
\$\endgroup\$
  • \$\begingroup\$ Thank you. It's getting interesting where you left off... Could you perhaps elaborate the "manual" approach? Because that's what I'm fighting with. RotateAround doesn't help me unfortunately. \$\endgroup\$ – tmighty Feb 10 at 18:17
  • \$\begingroup\$ I have also added 2 objectives to the post to make it understood why RotateAround wouldn't help me. \$\endgroup\$ – tmighty Feb 10 at 18:19
  • \$\begingroup\$ @tmighty zoom - you change the range of the camera, you can make it work with RotateAround (it's basically the same thing that doing this manually, it just makes all the calculations based on parameters). Same can be said for moving up or down, you can both RotateAround on horizontal and vertical axis. Look at how I calculate rotation axis in Awake. Basically you need to figure out how to change the rotation axis along the way when you are rotating vertically and horizontally, order shouldn't matter. \$\endgroup\$ – Candid Moon _Max_ Feb 10 at 18:24
  • \$\begingroup\$ @tmighty About manual approach - I am not very good with math to give a solution right away, it may take a lot of effort and testing to make it work. It would even be harder without RotateAround for me personally. \$\endgroup\$ – Candid Moon _Max_ Feb 10 at 18:25
  • \$\begingroup\$ Good to know that I'm not the only one having problems with this. :-) \$\endgroup\$ – tmighty Feb 10 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.