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 pab001 · Mar 07, 2014 at 10:56 PM · mousekeys

Select object with mouse and make it steerable with AWSD keys

When my game start it instantiates a number of balls. The player can select any of these balls and move them to a place. Now everything works OK. I can select any of the balls and move them with ASWD keys but only when the mouse is over a specific ball. Once it looses focus, it will disable the script that activated movement with ASWD keys. Is there a way, looking at the script below to make the AWSD keys independent from onMouseOver function()?

 #pragma strict
 
 private  var screenPoint:Vector3 ;
 private  var offset:Vector3;
 public var speed : float = 0.2;
 
 function  OnMouseDown() { 
 
     screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
     offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
         
 }
 
 function OnMouseEnter() {  
 
     var curScreenPoint:Vector3 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 6);
     var curPosition:Vector3 = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
 
     if (Input.GetKey("a"))
     {
         this.transform.position.x -= this.speed;
     }
     
     if (Input.GetKey("d"))
     {
         this.transform.position.x += this.speed;
     }
     
     if (Input.GetKey("w"))
     {
         this.transform.position.z += this.speed;
     }
     if (Input.GetKey("s"))
     {
         this.transform.position.z -= this.speed;
     }    
         
 }        
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 robertbu · Mar 07, 2014 at 11:12 PM

I'm assuming that all the object you want to move have this same script. Below is a solution. It has a 'currTrans' variable. This variable is 'static' which means that all the instances of this class will share the value. The OnMouseDown() code sets this variable to the transform of the object clicked on. The movement code has been moved into Update(). If the last clicked object is not the object with this script, then Update() just returns and no movement is done.

Note I've allso added 'Time.deltaTime' to your movement code so that movement will be frame rate independent.

 #pragma strict
 
 private  var screenPoint:Vector3 ;
 private  var offset:Vector3;
 public var speed : float = 3.0;
 
 static private var currTrans : Transform = null;
 
 function  OnMouseDown() { 
 
     screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
     offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
     currTrans = transform;    
 }
 
 function Update() {  
 
     if (currTrans != transform) return;
 
     var curScreenPoint:Vector3 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 6);
     var curPosition:Vector3 = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
 
     if (Input.GetKey("a"))
     {
         transform.position.x -= speed * Time.deltaTime;
     }
     
     if (Input.GetKey("d"))
     {
         transform.position.x += speed * Time.deltaTime;
     }
     
     if (Input.GetKey("w"))
     {
         transform.position.z += speed * Time.deltaTime;
     }
     if (Input.GetKey("s"))
     {
         transform.position.z -= speed * Time.deltaTime;
     }    
         
 }        
Comment
Add comment · Show 1 · 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
avatar image pab001 · Mar 08, 2014 at 11:07 PM 0
Share

Thank you very much for this answer. This worked for my project. I'm also trying to make make game in such a way that any of the balls will be red once you clicked on any of them. However only one ball will be red (which is the ball which the player last clicked on). $$anonymous$$y code looks like this and it almost works. At this point all the balls will get the color red when I click on them. I assume the solution is the same kind as proposed with the static variable keeping somehow track of the state of the color. Below is the code I used:

 #pragma strict
 
 private var screenPoint:Vector3 ;
 private var offset:Vector3;
 private var speed : float = 12;
 static private var currTrans : Transform = null;
 
 function  On$$anonymous$$ouseDown() { 
  
     screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
     offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
     currTrans = transform;
 }
 
 function ActivateColor() {
     renderer.material.color = Color.red;
 }
  
 function DeActivateColor() {
     renderer.material.color = Color.white;      
 }
 
 function FixedUpdate() {  
  
     if (currTrans != transform) return;{
     DeActivateColor();
     }
                          
     var curScreenPoint:Vector3 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 6);
     var curPosition:Vector3 = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
     ActivateColor();
      
     if (Input.Get$$anonymous$$ey("left"))
     {
        transform.position.x -= speed * Time.deltaTime;
     }
  
     if (Input.Get$$anonymous$$ey("right"))
     {
        transform.position.x += speed * Time.deltaTime;
     }
  
     if (Input.Get$$anonymous$$ey("up"))
     {
        transform.position.z += speed * Time.deltaTime;
     }
     if (Input.Get$$anonymous$$ey("down"))
     {
        transform.position.z -= speed * Time.deltaTime;
     }  
 
             
 }    

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

20 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

Related Questions

Mouse Orbit - Change From Mouse, to Keys 1 Answer

using left right keys to turn 1 Answer

C# inputString mouse Keys? 1 Answer

Moving a game piece with the mouse 1 Answer

MouseCursor.Arrow and MouseCursor.Link 3 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