- Home /
Move selected object.
Hey Unity geeks! Iv'e made a script that is supposed to move the object that is selected. The problem is that it only move when you press the right mouse button down, then stops moving once it's been pressed, (You have to click several times real quick to make it move, you can't hold it) since i have the if ( Input.GetMouseButtonDown(0) )
. I want the selected object to move as long as it's selected. (Until i select some other object.) Please help me out here! Here's the whole script:
function Update () { if ( Input.GetMouseButtonDown(0) ) { var hit : RaycastHit; var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition); if (Physics.Raycast (ray, hit, 100.0)) { Debug.Log(hit.collider.gameObject.name); var objektnamn = hit.collider.gameObject.name; } }
if ( objektnamn == this.gameObject.name ) { var speed = 9.0; var rotateSpeed = 9.0; var controller : CharacterController = GetComponent(CharacterController); transform.Rotate(0, Input.GetAxis ("Horizontal") rotateSpeed, 0); var forward = transform.TransformDirection(Vector3.forward); var curSpeed = speed Input.GetAxis ("Vertical"); controller.SimpleMove(forward * curSpeed); } }
Cheers! //Tommy
Answer by Mike 3 · Oct 16, 2010 at 09:57 PM
Remove var from
var objektnamn = hit.collider.gameObject.name;
It'll be making a local scope variable, and not storing it elsewhere
Then what you do is add
var objektnamn : String;
At the top of the script, above update