x


Object creation by mouse click

Hi folks!

I'v a function than create a sphere over a plane on the hitpoint of the mouse cursor when I press the left button of the mouse.

I want to make the object growing while the mouse button is pressend, and stop growing when I release the mouse.

I'm trying to do that with Input.GetMouseButtonUp and Down without success :(

Here is my actual code

    var bomb : GameObject;
var timeD : float;
var drop : GameObject;


function Update (){



    var hit : RaycastHit = new RaycastHit();
    var cameraRay : Ray  = Camera.main.ScreenPointToRay(Input.mousePosition);


    if (Physics.Raycast (cameraRay.origin,cameraRay.direction,hit, 1000)) {
        var cursorOn = true;
    }

    var mouseReleased : boolean = false;

    //BOMB DROPPING 
    if (Input.GetMouseButtonDown(0)) {

        drop = Instantiate(bomb, transform.position, Quaternion.identity);
        drop.transform.position = hit.point;

        Resize();

    }
}

function Resize(){
    if (!Input.GetMouseButtonUp(0)){

            drop.transform.localScale = Vector3(timeD,timeD,timeD);
            timeD +=Time.deltaTime;
        }
}

Tnx!!

more ▼

asked Mar 21 '11 at 02:46 PM

superlol gravatar image

superlol
88 13 18 22

Hi, tnx for the reply :) Maybe I'm not explained exactly.

This is the list of operations I need to perform:

  • One Mouse click holding the mouse button
  • The object is created on the hit point of the cursor
  • The object grew until I release the mouse button
Mar 21 '11 at 03:07 PM superlol
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

A few things, you'll probably want something more like:

drop.transform.localScale += Vector3(Time.deltaTime, Time.deltaTime, Time.deltaTime);

And you'll want this to happen over course of many calls to Update:

function Update (){
    if(Input.GetMouseButton(0)) {
        // This means the left mouse button is currently down, so we'll augment the scale
        drop.transform.localScale += Vector3(Time.deltaTime, Time.deltaTime, Time.deltaTime);
    }
}

GetMouseButtonUp and GetMouseButtonDown only get called once.. just on the frames those events take place.

more ▼

answered Mar 21 '11 at 03:09 PM

flaviusxvii gravatar image

flaviusxvii
3k 13 19 43

You probably want to define a 'speed' so you can adjust that var speed = .5; drop.transform.localScale += Vector3(Time.deltaTime, Time.deltaTime, Time.deltaTime) * speed;

Mar 21 '11 at 03:33 PM DaveA
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1095
x983
x788
x26

asked: Mar 21 '11 at 02:46 PM

Seen: 1557 times

Last Updated: Mar 21 '11 at 02:46 PM