- Home /
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!
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
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
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
Follow this Question
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