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 /
avatar image
0
Question by stephanlevin · Jul 10, 2017 at 08:00 AM · scripting problemtouchscripting beginnertouch controlstouchscreen

define touch area

hallo! i have an object with an auto spin and i can rotate it by mouse drag - touch. i want that this action (rotating) only works in the lower part of the screen here's the code of my rotation by mouse drag:

 using UnityEngine;
 using System.Collections;
 
 public class tes01 : MonoBehaviour
 {
     float rotSpeed = 8;
 
     void OnMouseDrag()
     {
         float rotX = Input.GetAxis("Mouse X") * rotSpeed * Mathf.Deg2Rad;
 
         transform.RotateAround(Vector2.up, -rotX);
     }
 }

would be really thankful if someone could help me!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by SohailBukhari · Jul 10, 2017 at 01:58 PM

First make a rect

 Rect notouchable_area;

Declare the size of rect in the Start() method.

     private void Start ()
         {
             #region Touch Handler (Touch will not work in these rectangles)
                      //Setting any of xMin, xMax, yMin and yMax will resize the rectangle
             notouchable_area = new Rect (getX (-50.2f), getY (-78.3f), getX (1100), getY (180));
             #endregion
         }
 public float  getX (float valX)
     {
         float x = (valX / 800) * 100;
         return  (x / 100) * Screen.width;
     }
 
     public float  getY (float  valY)
     {
         float y = (valY / 480) * 100;
         return    (y / 100) * Screen.height;
     }

Draw rectangle on the screen where you want that touch not work. You can see rect in the game mode using the onGUI method and change size according to your need.

 void OnGUI()
         {
          GUI.Box(notouchable_area,"");
         }

and in the OnMouseDrag method check whether touch exist in the rectangle area if yes then do nothing otherwise rotate or destroy object what you want.

Do this like i have done in the update method.

 void Update ()
     {
         if ((Input.GetMouseButtonDown (0)&& !notouchable_area.Contains (new Vector2 (Input.mousePosition.x, Screen.height - Input.mousePosition.y))))
         {
             // do whatever you want
             
         }
     }

If youa re not familiar then see the section https://docs.unity3d.com/ScriptReference/Rect-ctor.html

Comment
Add comment · Share
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
avatar image
0

Answer by Abdu_Ki · Jul 10, 2017 at 08:52 AM

Edit:

I'm sorry I totally misunderstood the question but now this is what you want to do i think:

 using UnityEngine;
 using System.Collections;
 
 public class tes01 : MonoBehaviour
 {
     float rotSpeed = 8;
 
     void OnMouseDrag()
     {
             if (Input.mousePosition.y < Screen.height / 2) {
                 float rotX = Input.GetAxis ("Mouse X") * rotSpeed * Mathf.Deg2Rad;
                 transform.RotateAround (Vector2.up, -rotX);
             }
     }
 }


Still not sure if this is what you want to achieve but this should do the trick. In the OnMouseDrag() function you are checking if the mouse position on the y axis is lower than the screen height divided by 2 (lower part of the screen). @stephanlevin

Comment
Add comment · Show 1 · Share
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
avatar image stephanlevin · Jul 10, 2017 at 10:44 AM 0
Share

thank you king_a! could you maybe (if you have the time) give me the full script of what you mean, because it doesn't really work for me

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

127 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Possible to get touch area data? 1 Answer

Hide selected object with button 0 Answers

Working with screen's touch limit 0 Answers

Make object follow my finger (Touch)? 1 Answer

Reset touch Vector after movement 2 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