Mark Zareal
Mark Zareal

Reputation: 23

How to make object move continously without stopping(unity2d)

So I've got this script here that moves the player by -1.25 when you click it but I want it to be continuously adding -1.25 until you release the button. Right now it only moves once when you click the button. My code:

var character : GameObject;
function OnMouseDown () {
    character.GetComponent(Animator).enabled = true;
    BlahBlah ();
}
function OnMouseUp () {
    character.GetComponent(Animator).enabled = false;
}

function BlahBlah () {
    character.transform.position.x = character.transform.position.x + -1.25;
}

Does anyone have any ideas? Thanks!

Upvotes: 0

Views: 2030

Answers (3)

Gounemond
Gounemond

Reputation: 353

You're simply forgetting to work in the Update()

var character : GameObject;
function OnMouseDown () 
{
    character.GetComponent(Animator).enabled = true;
}

function OnMouseUp () 
{
    character.GetComponent(Animator).enabled = false;
}

function BlahBlah () 
{
    // I added time.deltaTime, since you'll be updating the value every "frame",
    // and deltaTime refers to how much time passed from the last frame
    // this makes your movement indipendent from the frame rate
    character.transform.position.x = character.transform.position.x  - (1.25* time.deltaTime);
}

// The standard Unity3D Update method runs every frame
void Update() 
{
    if (character.GetComponent(Animator).enabled)
    {
        BlahBlah ();
    }
}

What I did here is using Unity logic. Almost everything works in the Update() function, which gets called every frame. Note that frame rate can vary based on machine / complexity of scene, so make sure to use always Time.deltaTime when adding something related.

Another note for you: modifying the position directly won't make your object react to collisions ( so you'll be moving through objects, but you will still "trigger" the collision ). So, if you want to manage collision, remember to use physics!

Upvotes: 1

Mrk Fldig
Mrk Fldig

Reputation: 4496

As I remember onMouseDown fires every frame if you're holding the button, so you'll need to do the movement within that function, let me check one sec.

Upvotes: 0

Milad Qasemi
Milad Qasemi

Reputation: 3059

you can use Input.GetMouseButton because it registers every frame so when you hold down the mouse it gets it but because it checks every frame while the mouse is down so your object will move so fast so you may want to add a timer that if the time is reached it moves so it moves a bit slower so we check if a specified amount of time that we set in timeLeft is passed and the mouse is held down then we move our object

   float timeLeft=0.5f; 

        void Update(){
          timeLeft -= Time.deltaTime;
        if (Input.GetMouseButton(0)){
                 if(timeLeft < 0)
                 {
                      BlahBlah ();
                 }
            }
        }

        void BlahBlah () {
            timeLeft=0.5f;
            character.transform.position.x = character.transform.position.x + -1.25;
        }

Upvotes: 0

Related Questions