0
\$\begingroup\$

I am making a small game where the player is constantly moving upwards, and I am having slight problems with the movement to the sides. The script located under works quite perfectly in itself, but my problem is that I want the balloon-player to nudge in a direction, rather than go at a constant speed. Kinda like a small punch to the side of the balloon.

enter image description here

How could I easily implement this? GetMouseButton or GetMouseButtonDown? Thanks in advance. :)

    void Update () {

    transform.Translate(Vector3.up * Time.deltaTime, Space.World);

    if(Input.GetMouseButton(0))
        transform.Translate(Vector3.left * Time.deltaTime, Space.World);

    if(Input.GetMouseButton(1))
        transform.Translate(Vector3.right * Time.deltaTime, Space.World);

}
\$\endgroup\$
5
  • \$\begingroup\$ As in you want some push when the button is clicked once? Then you want it to slow down over time? \$\endgroup\$
    – Ben
    Commented Nov 10, 2014 at 11:35
  • \$\begingroup\$ Yes. This will be for my android tablet, and I want simple taps on the sides to nudge the balloon in that direction. so a small acceleration as well. :) \$\endgroup\$
    – Blobbey
    Commented Nov 10, 2014 at 11:38
  • \$\begingroup\$ You should use Lerp for this \$\endgroup\$
    – MGDroid
    Commented Nov 10, 2014 at 12:46
  • \$\begingroup\$ Sounds like you just need to apply some force. \$\endgroup\$ Commented Nov 10, 2014 at 14:14
  • \$\begingroup\$ For what I see you are not using physics, are you? Without using physics you need a couple of tricks to reach your final effect: 1) reproduce a little tilting/rotating animation on your baloon sprite (rotate around z-axis) and come back to initial rotation. 2) use Vector3.Lerp to add an extra slide-movement component to your translation vector and apply over a time-window. If you need more code just ask. \$\endgroup\$
    – Erik
    Commented Nov 10, 2014 at 14:54

2 Answers 2

1
\$\begingroup\$

This looks like it would be something the physics system could fairly easily handle. I'd add a Rigidbody2D and then you can manipulate the balloon via the AddForce function. Note that in order to make the balloon "rise" you can get rid of your translate and instead set GravityScale to something like -0.5 and it will "fall" but up.

So I'd do something like this:

void Update () 
{
    if(Input.GetMouseButtonDown(0))
    {
        rigidbody2D.AddForce(-Vector2.right);
    }
    else if(Input.GetMouseButtonDown(1))
    {
        rigidbody2D.AddForce(Vector2.right);
    }
}
\$\endgroup\$
7
  • \$\begingroup\$ Humm, this doesn't quite work. I use this script, and mess around with the Rigidbody2D components. With that script activated, any change to Gravity Scale does nothing, until I turn on Is Kinematic. \$\endgroup\$
    – Blobbey
    Commented Nov 10, 2014 at 19:22
  • \$\begingroup\$ You have to turn OFF IsKinematic and you have to make sure you're not trying to manually change the object's position yourself. \$\endgroup\$
    – McAden
    Commented Nov 10, 2014 at 19:37
  • \$\begingroup\$ Turning off IsKinematic makes it so the balloon continues to increase speed, to the point he flies through the "roof". The script also does nothing to move the player. I am trying to figure out why now. :) EDIT: Just set gravity scale to 0, and now I can barely nudge the balloon to the sides. I just got to tweak the numbers, and make it not continuously add speed upwards. \$\endgroup\$
    – Blobbey
    Commented Nov 10, 2014 at 19:45
  • \$\begingroup\$ If you set gravity scale to 0 and it's still going upwards - you're manually pushing it upwards somehow. If you're trying to nudge it harder left/right you need to manually create a left/right vector with a larger X value or multiply the Vector2.right by some number to increase the force. \$\endgroup\$
    – McAden
    Commented Nov 10, 2014 at 19:57
  • \$\begingroup\$ rigidbody2D.AddForce(-Vector2.right * 50); I ended up doing it like this. However, it feels stale, seeing as a nudge in the other direction just nulls out the first nudge, making the balloon stand still. And no, when I set Gravity to 0, it stands still. \$\endgroup\$
    – Blobbey
    Commented Nov 10, 2014 at 19:59
0
\$\begingroup\$

What you need is a Velocity/Inertia system. Create an additional vector to represent the amount of "energy" in a given direction for the object - I call mine "Velocity". Every update, apply a percentage of the velocity to the position and subtract it from the velocity. Every time you hit a button to move, add to the velocity rather than directly moving the object.

It's a bit convoluted to set up but it works quite nicely and is the way its always done afaik.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .