Search Unity

How to limit rotation of canon with touch for Android

Discussion in 'Scripting' started by JayArtist, Feb 9, 2016.

  1. JayArtist

    JayArtist

    Joined:
    Jul 1, 2014
    Posts:
    89
    I'm creating a small 2D game that fires an object from a canon for Android. To make the controls easy for the user I've decided on touch input for one finger to swipe up or down to rotate the angle of the canon. (Later I'll hopefully add a means to fire a projectile with the other finger).

    The problem I'm having is limiting the angle between what the inspector says is 0 degrees (horizontal) and 70 degrees (not completely pointing up). Here's what I have so far:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class SwipeRotate : MonoBehaviour {
    4. public GameObject myGameObject;
    5. // Allows rotation of target object via touch vertically
    6. void Update ()
    7. {
    8.      if (Input.touchCount == 1)
    9.      {
    10.          Debug.Log ("Z Rotation = " + myGameObject.transform.rotation.z);
    11.          //if (myGameObject.transform.rotation.z >= 0f && myGameObject.transform.rotation.z <= 6f)
    12.          {
    13.              // Get Touch 0
    14.              Touch touch0 = Input.GetTouch(0);
    15.              // Apply The Rotation
    16.              if (touch0.phase == TouchPhase.Moved)
    17.              {
    18.                  myGameObject.transform.Rotate(0f, 0f, touch0.deltaPosition.y);
    19.              }
    20.          }
    21.      }
    22. }
    23. }
    (I've commented out the conditional line for now).

    I was hoping to use Mathf.Clamp, but all the examples rely on me using vector 3 for rotation - and as you can see I didn't use that method to rotate the canon as I'm not sure how to change the code to work well.

    The problem with my current 'fix' is that the canon inevitably gets stuck when it reaches one of it's maximums or minimums.

    I've looked into how this limiting is done with the mouse, but I couldn't find a good example where the mouse moves and object but is limited between two fixed points. A real head scratcher for me!

    I have another question regarding this, and that's regarding the angles. The inspector shows numbers that don't tally with my debug I added. Am I missing something?

    Any help appreciated, thanks, Jay.
     
  2. Gxnx

    Gxnx

    Joined:
    May 20, 2016
    Posts:
    2
    Just in case someone comes across this thread and has the same problem, this might help.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GunRotation : MonoBehaviour
    5. {
    6.  
    7.     public float speed = 10f;
    8.  
    9.     private float RotX;
    10.     private float RotY;
    11.  
    12.  
    13.     public void MyRotation()
    14.     {
    15.  
    16.         RotX += Input.GetTouch(0).deltaPosition.y * speed * Mathf.Deg2Rad;
    17.         RotY += Input.GetTouch(0).deltaPosition.x * speed * Mathf.Deg2Rad;
    18.  
    19.         RotX = Mathf.Clamp(RotX, -15, 15);
    20.         RotY = Mathf.Clamp(RotY, -15, 15);
    21.  
    22.         this.transform.eulerAngles = new Vector3(-RotX, RotY, 0.0f);
    23.     }
    24. }
     
    Last edited: Sep 28, 2016