I have two scripts.
- In one script, if my cow is touched a GUI menu appears and menu can be scrolled by pressing mouse left button.
- 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 );
}
}
}