x


2 scripts?

Hi

I have a script on an object to make it bounce on click and I have added another script to make the ball roll when the screen is tilted, however since adding the second script my ball dosent bounce as well now.

Any ideas? do I have to put this all in one script??

I am using JS as I am a more confident java programmer.

Thanks

more ▼

asked Jul 19 '12 at 02:35 PM

hanimalP gravatar image

hanimalP
5 2 3

It would help, if you'd posted the scripts...probably you are overwriting the bounce with the roll in your second script, but how exactly to prevent this I cannot say without seeing the scripts...

Jul 19 '12 at 02:39 PM Piflik

as Piflik said it would have helped to post the scripts and he is most likely right in why this is causing a problem however this can be fixed, make sure in the bounce script you are ONLY changing the vertical (probably y) position of the ball and in the roll script you are ONLY changing the horizontal(x or z) component of the ball, that way neither of the scripts will effect the other script. hope that helps, Scribe

Jul 19 '12 at 02:47 PM Scribe
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Ok...transform.Translate invalidates the rigidbody.AddForce. Merging these two scripts wouldn't help. Either use AddForce for the tilting or split the scripts even more: Parent the ball to an empty, attach the bounce script to the ball and the tilt script to the empty.

more ▼

answered Jul 19 '12 at 02:53 PM

Piflik gravatar image

Piflik
5.4k 15 26 44

Hi, ok this kind of makes sense, I am very much a beginner at this so still learning my way around. When you say parent the ball to an empty, whats an empty?? sorry for my ignorance.

Thanks

Jul 19 '12 at 06:08 PM hanimalP

An empty is the simplest gameobject in Unity. It has no components whatsoever, just a transform.

Jul 19 '12 at 06:14 PM Piflik

Ok great, Thank you for this. I will give it a try :)

Jul 19 '12 at 06:15 PM hanimalP

Ok, this option didn't work :( how do I add force for the tilting??

Jul 19 '12 at 06:43 PM hanimalP
(comments are locked)
10|3000 characters needed characters left

opps, scripts would help!

This is the tilt script

// Move object using accelerometer
var speed = 10.0;

function Update () {
    var dir : Vector3 = Vector3.zero;

    // we assume that device is held parallel to the ground
    // and Home button is in the right hand

    // remap device acceleration axis to game coordinates:
    //  1) XY plane of the device is mapped onto XZ plane
    //  2) rotated 90 degrees around Y axis
    dir.x = -Input.acceleration.y;
    dir.z = Input.acceleration.x;

    // clamp acceleration vector to unit sphere
    if (dir.sqrMagnitude > 1)
        dir.Normalize();

    // Make it move 10 meters per second instead of 10 meters per frame...
    dir *= Time.deltaTime;

    // Move object
    transform.Translate (dir * speed);


}

and the bounce script

var speed : float = 0.1;
function Update () {
    for (var i = 0; i < Input.touchCount; ++i) {
        if (Input.GetTouch(i).phase == TouchPhase.Began) {
        print ("touched");
        gameObject.rigidbody.AddForce(new Vector3(20, 360, 50), ForceMode.Force);
        }
    }
}

Thanks

more ▼

answered Jul 19 '12 at 02:49 PM

hanimalP gravatar image

hanimalP
5 2 3

(comments are locked)
10|3000 characters needed characters left

Yes put it in one script.

If you have two independend Update functions, you can never know which on is called first. So the two position changes may or may not combine as you wish, and that each frame. There are ways to provide that via Script Execution Order Settings, but I would handle the movement of one object in one Script. Otherwise you would have a lot more work to have it consistently and the possibility of hard to find errors is reduced.

For more info we gotta take a look at your scripts.

more ▼

answered Jul 19 '12 at 02:46 PM

Nightwatch gravatar image

Nightwatch
31 1

Ah I see the problem you mix rigidbody.AddForce and transform.Translate. So the physics engines transformations conflict with yours added by hand-

You can try to do the rigidbody operations in FixedUpdate() instead of Update() and try if this helps.

But a rule of thumb would be to either use forces and physics or translate by hand to move an object and avoid mixing both mechanics. It can work, but a lot has to be considered.

i.E. you can calculate the bounce too and apply it to transform. Or you can move the ball left right forward backward by forces or velocitys.

Jul 19 '12 at 03:01 PM Nightwatch
(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:

x342

asked: Jul 19 '12 at 02:35 PM

Seen: 243 times

Last Updated: Jul 19 '12 at 06:43 PM