UPDATE: I solved it! See comment below.
Hi all,
Background: I have a decent knowledge of math and physics. I learned a fair bit of R years ago to do data analysis and plotting for my undergrad thesis, and basically that’s all my programming experience until last week when I started this course on Udemy (I’m about 50% in). I also started making my own separate game on the side, and that’s what I need help with.
Problem: It’s all going quite well but I’m stuck with the code below. Basically what happens is that after the player wins, there’s another method that transports the player object to the orbiting starting point (0,0,2). Everything works just fine until here.
But then I want to use the method below to make the player object orbit around point (0,0,0) on the Y = 0 plane.
void Orbit()
{
if (atOrbitPosition == true)
{
transform.RotateAround(Vector3.zero, Vector3.up, 45f * Time.deltaTime);
}
}
…but instead, what happens is that the player object rotates around itself, and I have no idea why. The answer is probably something silly but I’ve already googled and troubleshooted this in any way I can think of, multiple times :'D halp?
Because you’re placing your player on position 0,0,0, if you want it to orbit instead of just spin, then you have to give some other value that’s not zero to the point parameter in RotateAround.
Consider that your object position and point have to be different in order for it not spin around itself.
Thanks for the reply!
But the player is at (0,0,2), so that’s not the actual reason 
Solved it!!! The problem was actually elsewhere:
This is a bit more of the original code (fixed version below). For context, WinSequence() gets called in FixedUpdate() when won == true; also orbitPosition and linearStep are defined elsewhere).
void WinSequence()
{
MoveToOrbitPosition();
Orbit();
}
void MoveToOrbitPosition()
{
if (distanceToOrbitPosition > 0f)
{
transform.position = Vector3.MoveTowards(transform.position, orbitPosition, linearStep);
}
else if (distanceToOrbitPosition <= 0f)
{
atOrbitPosition = true;
}
}
void Orbit()
{
if (atOrbitPosition == true)
{
transform.RotateAround(Vector3.zero, Vector3.up, 45f * Time.deltaTime);
}
}
And this is the fixed version:
void WinSequence()
{
MoveToOrbitPosition();
Orbit();
}
void MoveToOrbitPosition()
{
if (atOrbitPosition == false)
{
if (distanceToOrbitPosition > 0f)
{
transform.position = Vector3.MoveTowards(transform.position, orbitPosition, linearStep);
}
else if (distanceToOrbitPosition <= 0f)
{
atOrbitPosition = true;
}
}
}
void Orbit()
{
if (atOrbitPosition == true)
{
transform.RotateAround(Vector3.zero, Vector3.up, 45f * Time.deltaTime);
}
}
1 Like