- Home /
Hi all. I am new to unity. My problem is with the mouse touch. Details are below
I have two scripts. 1) In one script, if my cow is touched a GUI menu appears and menu can be scrolled by pressing mouse left button. 2) In other script, i want to move camera so by clicking mouse left button i can move the camera.
Now the problem is when i scroll the menu camera also move. If have to stop it. Please suggest something.
Scripts are below.
Cow Touch.cs
using UnityEngine; using System.Collections;
public class CowTouched : MonoBehaviour { private bool clicked__ = false; public Vector2 scrollViewVector = Vector2.zero; private string innerText = "I am inside the ScrollView";
// Use this for initial
void Start ()
{
;
}
void OnGUI()
{
if( clicked__ )
{
// Begin the ScrollView
scrollViewVector = GUI.BeginScrollView (new Rect ( 25, 25, 150, 100 ), scrollViewVector, new Rect ( 0, 0, 130, 140 ) );
// Put something inside the ScrollView
/*innerText = GUI.TextArea (new Rect (0, 0, 400, 400), innerText);*/
//Making A Button Background
GUI.Box( new Rect( 10, 20, 120, 120 ), " Menu " );
//Making a Button
if( GUI.Button( new Rect( 30, 60, 80, 25 ), "Button1" ) )
{
Debug.Log( "Your 1 Button Clicked");
}
if( GUI.Button( new Rect( 30, 100, 80, 25 ), "Button2" ) )
{
Debug.Log( "Your 2 Button Clicked");
}
// End the ScrollView
GUI.EndScrollView();
}
}
// Update is called once per frame
void Update ()
{
//if left mouse button is clicked
// create a ray cast that originates from the mouse clicked position
if( Input.GetMouseButtonDown( 0 ) )
{
Ray rayOrigin = Camera.main.ScreenPointToRay( Input.mousePosition );
RaycastHit hitInfo;
if( Physics.Raycast( rayOrigin, out hitInfo, 500f ) )
{
Debug.Log( "I Am Doing RayCasting" );
Debug.DrawLine( rayOrigin.direction, hitInfo.point );
if( hitInfo.collider.name == "Cow Walk Cycle" )
{
Debug.Log( "Your Cow Clicked");
clicked__ = true;
}
}
}
}
}
RTS.cs
using UnityEngine; using System.Collections;
public class RTS : MonoBehaviour { public float mouseSpeed; private int border = 5;
public bool isMouseLeftButtonDown = false;
public Vector3 mouseStartPosition = new Vector3();
public Vector3 cameraStartPosition = new Vector3();
public float cameraSpeed = 1.0f;
public int zoomMax;
public int zoomMin;
public float zoomSpeed = 5.0f;
public static float RotateAmount { get { return 10; } }
public static float RotateSpeed { get { return 100; } }
public Camera IsometricCamera;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
MoveCamera();
RotateCamera();
ZoomCamera();
}
private void MoveCamera()
{
if( isMouseLeftButtonDown == false && Input.GetMouseButton( 0 ) )
{
isMouseLeftButtonDown = true;
mouseStartPosition.x = Input.mousePosition.x;
mouseStartPosition.y = Input.mousePosition.y;
cameraStartPosition = transform.position;
}
if( isMouseLeftButtonDown )
{
if( !Input.GetMouseButton( 0 ) )
{
isMouseLeftButtonDown = false;
mouseStartPosition = Vector3.zero;
cameraStartPosition = Vector3.zero;
}
else
{
float diffX = Input.mousePosition.x - mouseStartPosition.x;
float diffY = Input.mousePosition.y - mouseStartPosition.y;
transform.position = new Vector3( cameraStartPosition.x + ( diffX * cameraSpeed ), cameraStartPosition.y, cameraStartPosition.z + ( diffY * cameraSpeed ) );
}
}
}
//ZoomCamera
private void ZoomCamera()
{
if( Input.GetKey( KeyCode.LeftAlt ) )
{
transform.Translate( 0 , -zoomSpeed, zoomSpeed );
}
if( Input.GetKey( KeyCode.RightAlt ) )
{
transform.Translate( 0 , zoomSpeed, -zoomSpeed );
}
}
private void RotateCamera()
{
Vector3 origin = transform.eulerAngles;
Vector3 destination = origin;
//detect rotation amount Right mouse button is down
if( Input.GetMouseButton( 1 ) )
{
//destination.x -= Input.GetAxis("Mouse Y") * RotateAmount;
destination.y += Input.GetAxis("Mouse X") * RotateAmount;
}
//if a change in position is detected perform the necessary update
if( destination != origin )
{
transform.eulerAngles = Vector3.MoveTowards( origin, destination, Time.deltaTime * RotateSpeed );
}
}
}
Answer by zharik86 · Jan 11, 2014 at 08:50 PM
Decisions it is possible to invent much. For example, to try to write everything explained in one script and to be guided by the clicked_ variable. When clicked _ == true to disconnect rotation processing. It is also possible to try to transfer this variable to your script of the camera RTS (I hope that this script is attached to the camera):
public class CowTouched:MonoBehaviour {
...
if(Input.GetMouseButtonDown( 0 )) {
Ray rayOrigin = Camera.main.ScreenPointToRay( Input.mousePosition );
...
if( hitInfo.collider.name == "Cow Walk Cycle" ) {
...
Camera.main.GetComponent<RTS>().setMyClicked(true); // add this your if
clicked_ = true;
}
...
And add variable and function in your RTS class:
public class RTS : MonoBehaviour {
...
private bool myClick = false; //adding variable
...
//adding new function
public void setMyClicked(bool cl) {
myClick = cl;
}
...
And change your function Update in RTS class:
void Update () {
if (!myClick) {
MoveCamera();
RotateCamera();
ZoomCamera();
}
}
Also don't forget when you quit the GUI menu to transfer the click_ variable to false status. And also the myClick variable similarly as I wrote above. Though, it is best of all to connect two scripts in one.
Your answer
![](https://koobas.hobune.stream/wayback/20220613130022im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Enable and disable a button selection 3 Answers
Issues creating a progress bar 1 Answer
Issues with Render Texture 1 Answer