- Home /
transform.RotateAround does not rotate to the object I want
Hi guys i have a problem with transform.RotateAround and I hope that you could help me out. I want my GameObject(which is attached to the script) to rotate around an object with a OnTriggerStay function. I made another object, so when he triggers with that my GameObject will rotate around the object i want, but instead it rotates around the object it triggers with. Any Idea ? Here is my code (I dont move my GameObject via Script at the moment i do it manually on the playmode):
public class Player : MonoBehaviour
{
[SerializeField]
GameObject target;
bool rotate = true;
void FixedUpdate()
{
Launch();
}
void Launch()
{
if (Input.GetKeyDown(KeyCode.Space))
{
rotate = false;
}
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Planet")
{
Destroy(gameObject);
}
}
void OnTriggerStay(Collider other)
{
if (other.tag == "PerfectLanding" && rotate == true)
{
transform.RotateAround(target.transform.position, Vector3.up, 200 * Time.deltaTime);
}
}
}
I can't see an issue with your code, maybe there's another behaviour somewhere that's affecting the transform and causing the unexpected behavior.
Answer by Statement · Jun 01, 2021 at 11:31 PM
You probably set the target to reference the trigger instead the object you want to rotate around. I tested your code and I couldn't see any problem with it.
The things that exists in the scene are the following :
sphere (the GameObject with the tag "Planet" and also the target on the script) and extra components a rigidbody with no gravity and it's sphere collider triggered
capsule(the GameObject with the tag "Player") and extra components a rigidbody with no gravity, the script above and its capsule collider triggered.
cube(the GameObject with the tag "PerfectLanding") with its box collider triggered and its mesh renderer unchecked.
So what I want to do is when the capsule triggers with the cube, the capsule should RotateAround the sphere(the target), but instead it rotates around the cube.
I hope this made it more clear to what my problem is and i hope you can help me out.
Reply me or not, thank you for your time.
Answer by PeacefuImind · Jun 03, 2021 at 03:20 PM
The target is the GameObject i want to rotate around, but instead it rotates around the GameObject that it is being trigger. I can not see any mistake to my code or in the scene as well, that is why I am so confused.
Your answer
Follow this Question
Related Questions
Issue understanding transform.RotateAround,Questions regarding transform.RotateAround 0 Answers
Rotate around moves object out of position, why? 1 Answer
Can you simulate the behavior of Transform.RotateAround using a Rigidbody? 0 Answers
Wheels that just plain won't spin 0 Answers
How to limit the rotation of an object 2 Answers