- Home /
RotateAround without transform
Hi,
Unfortunately my game cannot rely on transform functions, so I have to rewrite them. So for example for:
transform.Translate(Vector3.forward * speed);
I use:
transform.position += Vector3.forward * speed;
How would I be able to apply this to RotateAround? Specifically:
transform.RotateAround(Vector3.forward, speed / 10f);
I tried things like
transform.position = Quaternion.Euler(0, 0, 1f) * transform.position; //etc
but that didn't work.. =/
Answer by aldonaletto · Jul 09, 2013 at 01:36 AM
I don't know why your game can't use Transform functions, but it can be done with the following code:
function RotateAround(center: Vector3, axis: Vector3, angle: float){
var pos: Vector3 = transform.position;
var rot: Quaternion = Quaternion.AngleAxis(angle, axis); // get the desired rotation
var dir: Vector3 = pos - center; // find current direction relative to center
dir = rot * dir; // rotate the direction
transform.position = center + dir; // define new position
// rotate object to keep looking at the center:
var myRot: Quaternion = transform.rotation;
transform.rotation *= Quaternion.Inverse(myRot) * rot * myRot;
}
Hope you can at least use Quaternion functions!
NOTE: This function is based on the original Unity code for RotateAround (thanks to ILSpy for that!).
1- Fixed an error at line 6: it should be center + dir ins$$anonymous$$d of pos + dir
2- Fixed error in rotation: again with help of ILSpy, found that the implementation of Rotate isn't that easy!
Worked flawlessly, thanks!
Here the same code in C#:
void RotateAround(Vector3 center, Vector3 axis, float angle) {
Vector3 pos = transform.position;
Quaternion rot = Quaternion.AngleAxis(angle, axis); // get the desired rotation
Vector3 dir = pos - center; // find current direction relative to center
dir = rot * dir; // rotate the direction
transform.position = pos + dir; // define new position
transform.rotation *= rot; // rotate object to keep looking at the center
}
Correct code in C#.
private void RotateAround(Vector3 center, Vector3 axis, float angle) {
Vector3 pos = this.transform.position;
Quaternion rot = Quaternion.AngleAxis(angle, axis); // get the desired rotation
Vector3 dir = pos - center; // find current direction relative to center
dir = rot * dir; // rotate the direction
this.transform.position = center + dir; // define new position
// rotate object to keep looking at the center:
Quaternion myRot = transform.rotation;
transform.rotation *= Quaternion.Inverse(myRot) * rot * myRot;
}
There were two errors in the original script at lines 6 and 7 - take a look at the fixed answer: replace pos with center at line 6, and the object rotation at line 7 must be more complex in order to keep the original orientation relative to the center.
The last line has an extra quaternion multiplication. It should be: transform.rotation = rot * myRot;
I had some weird rotation issues resulting in NaN quaternion, changing last line to transform.rotation = rot * myRot;
solved it! Thank you!
Answer by JuanseCoello · Jul 09, 2013 at 02:09 AM
You can use this C # script to rotate around with "D" AND "A" or arrows.
using UnityEngine; using System.Collections;
public float rotateSpeed = 2.0F;
private Vector3 moveDirection = Vector3.zero;
void Update() {
CharacterController controller = GetComponent<CharacterController>();
{
transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0); // Rotation
}
}
}