Search Unity

Moving in a circle

Discussion in 'Scripting' started by Rhuantavan, Jun 27, 2006.

  1. Rhuantavan

    Rhuantavan

    Joined:
    Feb 23, 2006
    Posts:
    53
    I am attempting to write a game, where the player character moves in a circle... so that the world's reality is basically a cylinder around which everyhting moves... remember an old nes game called castelian?

    Anyway I am having some basic math problem, but which I am unable to solve. Using the script below attached to a CharacterController object, the character isn't moving right. Ok, besides the movement being a little jerky, the problem is that the character seems to be moving fine in the first 180 degrees, then it turns in the other direction... like a sine wave. Can anyone help me out, I am not a very math type... I am sure you guys can figure this out in the blink of an eye. ;)

    I am also looking for pitfalls in my coding practices here, so I would be grateful if you could give me a few pointers on that too.

    Thanks!

    Code (csharp):
    1.  
    2. var pivotPoint = Vector3.zero;
    3. var radii : float;
    4. var walkSpeed = 0.5;
    5. var jumpSpeed = 15.0;
    6. var angle = 0.0;
    7.  
    8. private var posNow : Vector3;
    9. private var posStart : Vector3;
    10.  
    11. function Awake() {
    12.     posNow = transform.position;
    13.     posStart = posNow;
    14.     radii = posStart.magnitude; // set the radii from the starting position
    15. }
    16.  
    17. function FixedUpdate() {
    18.     posNow = transform.position;
    19.    
    20.     var controller : CharacterController = GetComponent(CharacterController);
    21.        
    22.     angle = Vector3.Angle(posStart, posNow);
    23.     transform.rotation = Quaternion.AngleAxis(-angle, Vector3.up);
    24.    
    25.     var walkMovement = Input.GetAxis("Horizontal") * walkSpeed;
    26.     var rAngle = angle * Mathf.Deg2Rad;
    27.     var movX = radii * Mathf.Cos(rAngle);
    28.     var movZ = radii * Mathf.Sin(rAngle);
    29.     var movement = Vector3(movX, posNow.y, movZ) * walkMovement + Physics.gravity;
    30.    
    31.     controller.Move(movement * Time.deltaTime);
    32. }
    33.  
    34. @script RequireComponent(CharacterController)
    35.  
     

    Attached Files:

  2. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    I remember that game on my Amiga under the name TowerToppler, and there's a free version of it for Mac I think (name escapes me). I know you're not doing the same thing, but it was fun it its day :)

    I had some trig like that to solve in my game (although my player is on the INside of a ring) and it was a pain. I think your problem is that cos and sin expect angles limited to 90 degrees (PI/2), and so you have to text beforehand for angles greater than 90 (testing which quadrant you're in), and then (in my experience) you have to have some "ifs" that negate one or both of your two movement axes in response to which quadrant you're in.

    What if you parented your character to an empty GameObject you place at the center of the world? Then you'd just rotate that GameObject to move your character around the cylinder. To make the character face a different direction, you'd rotate the character. And to move vertically, it would be your choice to move the player or the invisible parent. You could even move in and out from the cylinder by moving the player on the player's Z.

    Also, do you have to use FixedUpdate? I've been led to believe that Update offers better performance if you have the choice.
     
  3. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    Rhuantavan, that script looks to me like you're trying to code stuff that is already inherently built in to Unity. You don't really need to fuss with all the trigonometry there to make a character walk around the topology of a cylinder. I'd go with Morgan's suggestion and just use parenting to get a rotation around the center of the cylinder. I'm making a Tron clone where the topology is the inside of a torus for the game arena. If I had to script it all through trig I wouldn't be done until next year with my coding skills. But using parenting with a simple hierarchy for rotational relationships I was able to set up the whole control scheme in less than an hour.
     
  4. Rhuantavan

    Rhuantavan

    Joined:
    Feb 23, 2006
    Posts:
    53
    Yeah sometimes I can't help but to complicate things all the way... I thank you guys for the suggestions. I'll try the parenting trick, which should work just fine for me.

    Now that I checked it out TowerToppler IS Castelian, but I haven't played that amiga version yet. And a remake: http://toppler.sourceforge.net/
     
  5. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    Thanks for the remake link--Nebulus is the name I was thinking of. But I'm sure your take on it will be better, with actual 3D perspective :)

    (Not that Unity forces you to use perspective, but I think that would be pretty neat.)
     
  6. Rhuantavan

    Rhuantavan

    Joined:
    Feb 23, 2006
    Posts:
    53
    Yeah that game screamed to be remade in 3D :)