- Home /
left and right player movements restricted to a circle
i'm build a boss fight level where there is a cirlce platform this is what work: if i press up arrow the player go torwards the boss if i press down arrow the player go in the opposite site (as you can see in the image)
how can i move the player (left and right arrow ) with mathematical circle equation, to move the player around the boss??
Thanks in advance :)
here's the code
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.UpArrow)) {
transform.LookAt(boss.transform.position);
player.transform.Translate(boss.transform.position.x, boss.transform.position.y, boss.transform.position.z);
}
That's a very strange way to move your player, but anyway ...
Try something with Transform.RotateAround
http://docs.unity3d.com/ScriptReference/Transform.RotateAround.html
and set the rotation back of your player.
maybe you're right hellium, but that's is how i think and i want to make for moving the player around,
this is my $$anonymous$$d pseudocode:
float XZradius // current position of player from the center of circumference
update() if (up or down arrow) float XZradius= player position x & player position z
if(left arrow //or right arrow//) go to left //or right arrow// while rotating around the boss with XZradius
you know what i mean...? i complicate my life -.-'
Answer by Owen-Reynolds · Jul 22, 2015 at 02:39 PM
I think you have the right idea, with XZradius
. One trick to programming is to set up your personal variables that describe what you want. Change them based on how you're thinking of the problem. Then, "apply" them to the object.
Try:
float playerAngle; // 0 to 360. Angle, from center, of player
float playerRadius; // same as your XZradius
Left/right arrow keys add and subtract from the player angle. Very easy, it's just a number.
Then, don't use translate or rotateAround, or anything that moves the player form where it was. You know the angle from the center and the distance, so just put the player there. This is a little tricky,
// how far the player is. This is straight North, which is 0 degrees:
Vector3 toPlayer=Vector3.forward*XZradius;
// now spin it to the correct angle:
toPlayer = Quaternion.Euler(0,playerAngle,0)*toPlayer;
// now start it from the center: player.position=center+toPlayer;
Quaternion.Euler isn't as bad as it looks. It's just the way you "make" an angle. Multiply (angle times Vector3) is the way you spin a vector by an angle. But, if you've never worked with standard (non-Unity) vector math -- like Intro High School Geometry -- this will probably still be confusing.