- Home /
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;
}
}
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;
}
}
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

Follow this Question
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