Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by JayArtist · Feb 10, 2016 at 02:02 PM · androidrotationtouchclamp

Unity How to limit rotation of canon with touch for Android

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:

 using UnityEngine;
 using System.Collections;
 
 public class SwipeRotate : MonoBehaviour {
 
 public GameObject myGameObject;
 
 // Allows rotation of target object via touch vertically
 
 void Update () 
 {
     if (Input.touchCount == 1)
     {
 
         Debug.Log ("Z Rotation = " + myGameObject.transform.rotation.z);
 
         //if (myGameObject.transform.rotation.z >= 0f && myGameObject.transform.rotation.z <= 6f)
         {
             // Get Touch 0
             Touch touch0 = Input.GetTouch(0);
 
             // Apply The Rotation
             if (touch0.phase == TouchPhase.Moved)
             {
                 myGameObject.transform.Rotate(0f, 0f, touch0.deltaPosition.y);
             }
         }
 
     }
 }}

(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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Android Touch Double Jump - Not working 0 Answers

Android/Apple native plugin for touch input? 0 Answers

Android| Camera scrolling 1 Answer

How to ignore touches on a layer 0 Answers

Error : The second touch should not work at all. How do I fix this error?,Error: The second touch should not work at all. How do I fix this error? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges