Search Unity

Looking At Last Position (Mobile Input)

Discussion in 'Scripting' started by LongformStudios, Dec 2, 2015.

  1. LongformStudios

    LongformStudios

    Joined:
    Nov 27, 2014
    Posts:
    6
    Hi!

    So I'm making a 2D top down shooter game for mobile and I ran into an issue I'm having with the second joystick which handles rotation (as of now).

    The way I want it to function is that whenever the user is using the joystick it's actively looking in the same direction as the position of the joystick, and when the user isn't using the joystick it stays looking the same direction that it did when the user was using the joystick itself.

    What its doing is when the user is actively using the joystick it's looking in the same direction as the position of the joystick, but whenever you aren't it snaps into a fixed right angle.

    Here's my code

    Code (CSharp):
    1.     void FixedUpdate() {
    2.         Vector2 lastCachedRotation = crosshairs.position;
    3.  
    4.         if (health > 0) {
    5.             movement = new Vector2(CrossPlatformInputManager.GetAxisRaw("Horizontal_1"), CrossPlatformInputManager.GetAxisRaw("Vertical_1"));
    6.  
    7.             my.position = new Vector2(my.position.x + movement.x * speed * Time.deltaTime, my.position.y + movement.y * speed * Time.deltaTime);
    8.         }
    9.         else
    10.             Destroy(gameObject);
    11.  
    12.         if (transform.position.x + CrossPlatformInputManager.GetAxisRaw("Horizontal_2") + transform.position.x + CrossPlatformInputManager.GetAxisRaw("Vertical_2") != 0)
    13.             crosshairs.position = new Vector2(transform.position.x + CrossPlatformInputManager.GetAxisRaw("Horizontal_2"), transform.position.y + CrossPlatformInputManager.GetAxisRaw("Vertical_2"));
    14.         else
    15.             crosshairs.position = lastCachedRotation;
    16.  
    17.         LookAt();
    18.     }
    19.  
    20.     void LookAt() {
    21.         // Get the mouse position in world space. Using camDis for the Z axis.
    22.         Vector3 diff = crosshairs.position - transform.position;
    23.         diff.Normalize();
    24.  
    25.         float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
    26.         transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 90);
    27.     }
    28.  
    29.     void OnTriggerEnter2D(Collider2D info) {
    30.         if (info.tag == "Enemy") {
    31.             isEnemyPresent = true;
    32.         }
    33.     }
    Please help I've never made a game for mobile

    Thanks,
    James
     
  2. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    When i should guess, your lookat is getting the differenz for your movement, if you release your joystick, your differenz rot_z will be 0 ? so if its zero, you still are setting the rotation to Euler (0f, 0f, -90f); so maybe this is your fixed right angle when releasing.

    Code (CSharp):
    1.  void LookAt() {
    2.         // Get the mouse position in world space. Using camDis for the Z axis.
    3.         Vector3 diff = crosshairs.position - transform.position;
    4.         diff.Normalize();
    5.         float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
    6.         if (!Mathf.Approximately(rot_z, 0f)) {
    7.             transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 90);
    8.        }
    9. }
    you could try this but not sure.

    Also for savety, i would rename your method LookAt() because Unity Transform hast a method with same name. (Transform.LookAt , just to avoid later mistakes ;) )