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 Leprekhaun · May 26, 2014 at 08:28 AM · c#formatting

Why is it only using one mouse input?

Ok so here is the script I have so far and it is suppose to be selecting and deselecting the unit by clicking on the child objects collider when clicking the left mouse button but it only does the first mouse click option for instantiating a target. I'm sure its just a formatting error on my part so any help to this would be wonderful. I'm very new to this and I'm stumped.

     RaycastHit hit;

 public GameObject Target;
 private GameObject targObj;

 private float raycastLength = 500;

 public static GameObject CurrentlySelectedUnit;
 private static Vector3 mouseDownPoint;
 
 bool objExists;
 bool leftRightMouseCheck;

 void awawke(){
     mouseDownPoint = Vector3.zero;
     }
 void Update () {        
             if (Input.GetMouseButtonDown (1)) {
                     if (objExists == false) {
                             //create targObj if not already created                                
                             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
                             if (Physics.Raycast (ray, out hit, raycastLength)) {
                                     Debug.DrawRay (ray.origin, ray.direction * raycastLength, Color.yellow);
                                     if (hit.collider.name == "TerrainMain") {
                                             //Creates Object if it doesn't exist
                                             targObj = Instantiate (Target, hit.point, Quaternion.identity) as GameObject;
                                             targObj.name = "TargetInstantiated";
                                             Debug.Log ("Created");
                     
                                             objExists = true;
                                     }
                             }
                     } else {                                                    
                             Destroy (targObj);
                             Debug.Log ("Destroyed");
             
                             //recreate targObj after destroying previous
                             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
                             if (Physics.Raycast (ray, out hit, raycastLength)) {
                                     Debug.DrawRay (ray.origin, ray.direction * raycastLength, Color.yellow);
                                     if (hit.collider.name == "TerrainMain") {
                                             //Creates Object if it doesn't exist
                                             targObj = Instantiate (Target, hit.point, Quaternion.identity) as GameObject;
                                             targObj.name = "TargetInstantiated";
                                             Debug.Log ("Created");
                     
                                             objExists = true;
                                     }
                             }                                
                     }

             }
 
             //store mouse point
             if (Input.GetMouseButtonDown (0)) {
                     Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
                     if (Physics.Raycast (ray, out hit, raycastLength)) {

                             if (hit.collider.name == "TerrainMain") {

                                     mouseDownPoint = hit.point;                                        
                                     if (Input.GetMouseButtonUp (0) && DidUserClickLeftMouse (mouseDownPoint)) {
                                             DeselectGameobjectIfSelected ();
                                             if (hit.collider.transform.FindChild ("character")) {
                                                     Debug.Log ("Found a Unit");                    
                                                     //are we selecting a different 
                                                     if (CurrentlySelectedUnit != hit.collider.gameObject) {
                                                             //activate the selector
                                                             GameObject SelectedObj = hit.collider.transform.FindChild ("character").gameObject;
                                                             SelectedObj.SetActive (true);
                                                             //deactivate currently selected
                                                             if (CurrentlySelectedUnit != null) {
                                                                     CurrentlySelectedUnit.transform.FindChild ("character").gameObject.SetActive (false);
                                                                     //replace currently selected unit
                                                                     CurrentlySelectedUnit = hit.collider.gameObject;
                                                             }
                         
                                                     } else {
                                                             //if this object is not a unit
                                                             DeselectGameobjectIfSelected ();
                                                     }
                                             }
                                     }                                                        
                             } else {
                                     if (Input.GetMouseButtonUp (0) && DidUserClickLeftMouse (mouseDownPoint)) {
                                             DeselectGameobjectIfSelected ();
                                     }
                             }
                     }
     
             }
     }
     
 #region helper functions

 //check if user clicked mouse
 public bool DidUserClickLeftMouse(Vector3 hitPoint)
 {
             float clickZone = 1.3f;

             if (
                     (mouseDownPoint.x < hitPoint.x + clickZone && mouseDownPoint.x > hitPoint.x - clickZone) &&
                     (mouseDownPoint.y < hitPoint.y + clickZone && mouseDownPoint.y > hitPoint.y - clickZone) &&
                     (mouseDownPoint.z < hitPoint.z + clickZone && mouseDownPoint.z > hitPoint.z - clickZone)
         )
                     return true;
             else
                     return false;
     }
  static void DeselectGameobjectIfSelected()
 {
             if (CurrentlySelectedUnit != null) {
                     CurrentlySelectedUnit.transform.FindChild ("character").gameObject.SetActive(false);
                     CurrentlySelectedUnit = null;
             }
     }
 #endregion

}

Comment
Add comment · Show 3
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 Maerig · May 26, 2014 at 09:01 AM 0
Share
 if (Input.Get$$anonymous$$ouseButtonDown (0)) {
     ...
     if (Input.Get$$anonymous$$ouseButtonUp (0)) {

means that the mouse left button needs to be pressed and released at the same time. Also, supposing that the unit is being clicked, it is referred by hit.collider, so most likely

 (hit.collider.name == "Terrain$$anonymous$$ain")

will be false and

 DidUserClickLeft$$anonymous$$ouse (mouseDownPoint)

will be true.

Are you just trying to detect a left click or is it something more complex ?

avatar image Leprekhaun · May 26, 2014 at 09:39 AM 0
Share

that bool leftrightmousecheck isnt being used and i forgot to remove it. Im trying to get it so i can select the unit by the left mouse click and then use this function (by clicking the right mouse button) to instantiate the pathfinding target for the unit to go to.

 `if (Input.Get$$anonymous$$ouseButtonDown (1)) {
               if (objExists == false) {
                    //create targObj if not already created                    
                    Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
                    if (Physics.Raycast (ray, out hit, raycastLength)) {
                         Debug.DrawRay (ray.origin, ray.direction * raycastLength, Color.yellow);
                         if (hit.collider.name == "Terrain$$anonymous$$ain") {
                              //Creates Object if it doesn't exist
                              targObj = Instantiate (Target, hit.point, Quaternion.identity) as GameObject;
                              targObj.name = "TargetInstantiated";
                              Debug.Log ("Created");
  
                              objExists = true;
                         }
                    }`
           `} else {                                 
                Destroy (targObj);
                Debug.Log ("Destroyed");
 
                //recreate targObj after destroying previous
                Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
                if (Physics.Raycast (ray, out hit, raycastLength)) {
                     Debug.DrawRay (ray.origin, ray.direction * raycastLength, Color.yellow);
                     if (hit.collider.name == "Terrain$$anonymous$$ain") {
                          //Creates Object if it doesn't exist
                          targObj = Instantiate (Target, hit.point, Quaternion.identity) as GameObject;
                          targObj.name = "TargetInstantiated";
                          Debug.Log ("Created");
 
                          objExists = true;
                     }
                }                    
           }
 
      }

`

but it wont let me select the unit when i click on the "character" collider. "character" being a child to the whole unit. it only lets me instantiate the target. and it wont do both.

avatar image Leprekhaun · May 26, 2014 at 09:43 AM 0
Share

im sure it's some kind of issue with how i set it up because i did get the selecting to work but then the instantiating target part did not work.

0 Replies

· Add your reply
  • Sort: 

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

21 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

An OS design issue: File types associated with their appropriate programs 1 Answer

formatting errors converting this code! 2 Answers

how to arrangement child objects using c# 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