Search Unity

Movement along a circle using a simulated joystick (touchscreen joystick)

Discussion in 'iOS and tvOS' started by ecnalyr, Mar 17, 2011.

  1. ecnalyr

    ecnalyr

    Joined:
    May 23, 2010
    Posts:
    38
    Edit:
    I was thinking it may be easier to change to this idea:

    The ship is stuck on a fixed circle and you touch somewhere on the screen and the ship travels to the closest point it can on that circle near your touch - without joysticks.

    How would this be done?


    Original post:

    I am attempting to make a scene where an object (ship) is locked on an invisible circle around a center point. The ship can move freely around the circle while always facing the center point, and can fire weaponry inward toward that center point. I am having a few problems with this situation, and I would like to receive some tips on handling at least on of them.

    My biggest problem currently is as follows:

    My ship does not stay a fixed distance from the center point, when I tell the ship to move using the joystick, it glides fine up and down the left (7 o'clock through 11 o'clok) and right (1 o'clock through 5 o'clock) sides of the circle, but when the ship gets to the top or bottom of the circle (12 and 6), the ship jiggles around a lot (flips around facing all directions) and gradually gets further and further away from the center point until I (through awkward seemingly luck-filled controlling of the joystick) manage to get the ship onto the other side of the circle where it begins to glide smoothly again.

    The controls make sense when the ship is gliding smoothly (if i move the joystick up, the ship moves up, if i move it down, it moves down, and left and right 'feel' as they should when using a joystick). The problem seems to arrive when the ship is needing to switch to the other side of the circle, ie:

    The scene starts with the ship at the 9 o'clock position, I press up with the joystick and the ship starts to move clockwise toward 12. When it gets close to 12, it wiggles around wildly until I randomly jiggle the controls to a point where the ship can begin to move on the other side of the clock (1 - 5 o'clock positions). When I am on the 1-5 side of the clock though, up now makes the ship move counter-clockwise, which is indeed intuitive to the controls - but I think this may be the seed of the wild wiggling.​

    I need to know how to lock the ship x units away from the center point along the circle.

    and

    I need to know how to make the controls function smoothly while going around the circle.

    Can anyone point me in the right direction?

    ps, I am currently using "Dual Joysticks" prefab from the Standard Assets (Mobile) package along with a customized version of the "Player Relative Controls" prefab.
     
    Last edited: Mar 17, 2011
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    The problem with the ship jiggle at 6 and 12 o'clock and the reversed controls are probably the result of using Quaternion.LookRotation or transform.LookAt to control the rotation. These functions take an optional second parameter to specify the upward direction but by default, they use the global upward direction. This becomes a problem when the object can rotate so that the forward direction coincides with upward because the function then has no way to tell which orientation is required. The way to fix it is to pass a vector for the upward direction which is the same as the axis around which the ship rotates. If you are in any doubt, post your code (it will be easier to suggest what to modify that way).

    You might find transform.RotateAround an easier way to control your ship, depending on exactly what is involved in the game.
     
  3. ecnalyr

    ecnalyr

    Joined:
    May 23, 2010
    Posts:
    38
    I like the idea of rotateAround, but after 'playing' with the game without a fixed circle for a while - it may simply be more fun to not be fixed as we initially planned - although I still want to test it.

    I still need to get rid of the 6 and 12 o'clock flipping thing though, so here is my current code:

    Code (csharp):
    1. // Touch to move, tap to fire
    2.  
    3. // Moves object toward first finger's touch and fires weapon when second finger taps.
    4.  
    5. //Ecnalyr added vars
    6. //var blackhole : Transform.target;
    7. static var blackhole : Vector3 = Vector3(20 , 5 , 0 );
    8. var blasterPrefab : Transform; // The blaster prefab
    9.  
    10. //end of Ecnalyr added vars
    11.  
    12. var smooth:int; // Determines how quickly object moves towards position
    13.  
    14. private var targetPosition:Vector3;
    15.  
    16. function Update ()
    17. {
    18.     transform.LookAt(blackhole); //Forces ship to look at blackhole
    19.    
    20.     if(Input.touchCount>0)
    21.     {  
    22.     if(Input.GetTouch(0).phase==TouchPhase.Moved)
    23.     {
    24.         var playerPlane = new Plane(Vector3.forward, transform.position);
    25.         var ray = Camera.main.ScreenPointToRay (Input.touches[0].position);
    26.         var hitdist = 0.0;
    27.        
    28.         if (playerPlane.Raycast (ray, hitdist))
    29.         {
    30.             var targetPoint = ray.GetPoint(hitdist);
    31.             targetPosition = ray.GetPoint(hitdist);
    32.             //var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
    33.             //transform.rotation = targetRotation;
    34.         }
    35.     }
    36.    
    37.     transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smooth);
    38.            
    39.            
    40.  
    41.             //fire weapon phase via taps           
    42.     if(Input.touchCount>1)
    43.     {
    44.         if(Input.GetTouch(1).phase==TouchPhase.Began)
    45.         {  
    46.             var blaster : GameObject = Instantiate(blasterPrefab, GameObject.Find("blasterPoint").transform.position, Quaternion.identity);
    47.             // Propells blasted ammunition forward
    48.             blaster.rigidbody.AddForce(blaster.transform.forward * 2000);  
    49.             //end fire weapon phase
    50.         }
    51.     }
    52.     }
    53.    
    54. }
     
  4. ecnalyr

    ecnalyr

    Joined:
    May 23, 2010
    Posts:
    38
    I changed:

    Code (csharp):
    1.     transform.LookAt(blackhole);
    to

    Code (csharp):
    1.     transform.LookAt(blackhole, Vector3.forward);
    I was not aware of how to use the optional second parameter. Thank you sincerely :)