Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by RayneAvalon · Nov 30, 2012 at 08:58 PM · movementselectionmouse click

How to make a mouse click selection for in game use

Alright so i have been developing a level builder game for a little while now and my biggest problem to date is getting the mouse click selection system working. What I need it to do is simple enough. When the left mouse button is clicked and the code recognizes that it has hit an object it needs to highlight or somehow note that the object is selected and then pass a variable to the object so that the player can use the keyboard controls to move and rotate the object, and only the object selected. Bellow is the code i have thus far:

var clickedGmObj:GameObject = null; function GetClickedObject() { var ray:Ray = Camera.main.ScreenPointToRay(Input.mousePosition); var hit:RaycastHit;

if(Physics.Raycast(ray, Mathf.Infinity)) { return hit.transform.gameObject; } else { return null; } }

function Update () {

 if(Input.GetMouseButtonDown(0))
 {
     clickedGmObj = GetClickedObject();
     if(clickedGmObj != null)
     Debug.Log(clickedGmObj.name);
     }
    
 

}

this is the code used for the mouse selection, next is the code applied to the objects so they can be moved:

     var distance = 10;
     function Start () {
      
     }
      
     function Update () {
     if(Input.GetKey("w"))
     {
         transform.Translate(Vector3.forward * distance * Time.deltaTime);
      
     }
     if(Input.GetKey("s"))
     {
         transform.Translate(Vector3.back * distance * Time.deltaTime);
      
     }
     if(Input.GetKey("a"))
     {
         transform.Translate(Vector3.left * distance * Time.deltaTime);
      
     }
     if(Input.GetKey("d"))
     {
         transform.Translate(Vector3.right * distance * Time.deltaTime);
      
     }
      
     if(Input.GetKey("r"))
     {
         transform.Translate(Vector3.up * distance * Time.deltaTime);
      
     }
     if(Input.GetKey("f"))
     {
         transform.Translate(Vector3.down * distance * Time.deltaTime);
      
     }
     if(Input.GetKey("e"))
     {
         transform.Rotate(Vector3(0,45,0) * Time.deltaTime);
      
     }
     if(Input.GetKey("q"))
     {
         transform.Rotate(Vector3(0,-45,0) * Time.deltaTime);
      
     }
     }

its all javascript, any and all help would be much appreciated. im basically a novice at this. ive only gotten this far with alot of help from a few friends at school.

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 New_Game_Ideas · Sep 19, 2021 at 04:46 PM

I don't think there is a proper way to do it on update. I was just working on it and find a solution. Baiscally I started to increase a number (it is ClickCounter in the code) over-time in FixedUpdate() and when OnMouseDown() I started it from zero again. If the OnMouseUp() is called before the number reaches ten it is assumed a click Note: Do not forget to define ClickCounter(int) or any variable you want to use for the same function, do not forget to define Selected (bool) variable The code:`

region DragDrop

 private void FixedUpdate()
 {
     ClickCounter++;
 }
 private void OnMouseDown()
 {
     ClickCounter = 0;
     if(IsSelected == true)
     {
        
    distance = Vector3.Distance(gameObject.transform.position, Camera.main.transform.position);
     }
 }    
 private void OnMouseUp()
 {
     if(ClickCounter <= 10)
     if(gameObject.tag == "Dost birlik")
         {
             IsSelected = !IsSelected;
         }
     
 }
 private void OnMouseDrag()
 {
     if(IsSelected == true)
     {
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     Vector3 rayPoint = ray.GetPoint(distance);
     transform.position = new Vector3(rayPoint.x,transform.position.y,rayPoint.z);
     }       
 }
 #endregion

` And this hall code requires a click to work properly.

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

12 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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

simple ball rolling mechanic 0 Answers

TextField/User Controls Conflicts 2 Answers

Select something using mouseclick 2 Answers

Character Movement Issues 0 Answers


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