Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Priyanshi · Jan 10, 2014 at 12:00 PM · c#gui

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 );
     }
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

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

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

19 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

Related Questions

Enable and disable a button selection 3 Answers

Issues creating a progress bar 1 Answer

Issues with Render Texture 1 Answer

What is frame, How OnGuI is called every frame? 2 Answers

C# Applying Transparency to a Single GUI.Button 1 Answer


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