Trying a script for movement like the flying Parrots of Donkey Kong 2

As a begginer, I’m trying to make a script to my character fly like the parrot in DKC.

What I wanted:
Firstly I was searching for a very simple movement:
Mouse (Fire1) to rise up a little bit in the direction of the mouse is pointing.

Optionally, it could be like this:
WASD for directions
Mouse (Fire1) to rise up a little bit in the direction of the mouse is pointing.

What I tried:

I didn’t try WASD yet because I was thinking in a only-mouse game.

public class MoveControl : MonoBehaviour
{
    public float upSpeed = 2;
    public float sideSpeed = 1;
 
   Vector3 mousePosition;
    private bool mouseClicked;
  
    void Update()
    {
       

        mousePosition = Input.mousePosition;
        mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);

        if (Input.GetButtonDown("Fire1")) mouseClicked = true;
      
    }   
  
    void FixedUpdate()
    {
        if (mouseClicked)
            {
               if (mousePosition.x > transform.position.x)
               {
                transform.GetComponent<Rigidbody2D>().velocity = new Vector2(sideSpeed, upSpeed);
                mouseClicked = false;
               }
              
               else
              
               {
               transform.GetComponent<Rigidbody2D>().velocity = new Vector2(-sideSpeed, upSpeed);
               mouseClicked = false;
               }
              
            }
      
    }

  
}

The results are far from the goal. I’ve tried different functions of rigidbody like AddForce and Impulse and I don’t get good results

I don’t know if te DKC parrot doesn’t have gravity.

The objective is something like minute 3 in the video:

Let’s start from there. How did it “fail”?

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • links to documentation you used to cross-check your work (CRITICAL!!!)
  • what actually happened, especially any errors you see

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

You may edit your post above.

As for algorithm, I haven’t played this game but it appears to:

  • move downwards all the time
  • move left and right, perhaps based on wind AND player input?
  • when “up” or “flap” is pressed, it travels upwards for a short time (use a timer, NOT a coroutine!)

Thank you. First timer here. I edited my post.

Excellent, and welcome!

So let’s start by vertically pulling your code apart.

First, the Input.GetKeyDown() method is only valid in Update(). Docs tell you this but it’s a common oversight. Usually you check that click was pressed in Update() and set a boolean. Then in FixedUpdate() you check it, act on it ONCE, and clear the boolean.

Next remember that the mouse comes in Screen coordinates, the transform is in world coordinates, so comparing them isn’t going to be straightforward like you do there.

The Camera has methods to go back and forth between the two coordinate spaces.

One thing I cannot see from the video is what the controls are doing. Are they single taps causing the bird to climb like the Joust arcade game? Or do you hold it and he flaps as long as you are holding it?

Thanks a lot. I edited the code in the main post. I’ve adjusted as you said and the movement is a lot better. However, it still far from the DK parrot. But it’s my fault. I’ll try the WASD inputs to make it closer to DK.

If I remember, DK works like the Joust example.

You’re right again. I’m getting troubles with the camera.

Here’s a jousty-joust I just put together. Slap a cube in front of the camera, REMOVE the Box Collider from the cube (this uses Rigidbody2D) and run.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// @kurtdekker
// joust flapping ostrich controls
// drop me on a blank cube in front of the camera
// use left / right, FLAP to flap
// gravity might be pretty gnarly; pull the camera back...

[RequireComponent( typeof( Rigidbody2D))]
public class Joust : MonoBehaviour
{
    // how far left/right of UP can you thrust?
    const float MaxThrustVectorTilt = 30.0f;
    // the larger this is, the more effective each flap is
    const float MasterForceScale = 30.0f;

    Rigidbody2D rb;

    void Start ()
    {
        Physics2D.gravity = Vector2.down * 3.0f;            // LESS GRAVITY!

        rb = GetComponent<Rigidbody2D>();
    }

    bool flap;

    void Update ()
    {
        // read flap intent
        if (Input.GetKeyDown( KeyCode.Space) ||   
            Input.GetKeyDown( KeyCode.UpArrow) ||
        false)
        {
            flap = true;
        }
    }

    void FixedUpdate()
    {
        // forces only happen during a flap
        if (flap)
        {
            flap = false;        // consume

            // raw input
            float xInput = Input.GetAxisRaw("Horizontal");

            // TODO: gather other left / right inputs?

            // clamp
            xInput = Mathf.Clamp( xInput, -1.0f, 1.0f);

            // tilt
            float angle = xInput * MaxThrustVectorTilt;

            // rotation
            Quaternion rotation = Quaternion.Euler ( 0, 0, -angle);

            // rotate vector
            Vector2 force = rotation * Vector2.up;

            // scale for gravity
            force *= Physics2D.gravity.magnitude;

            // scale for mass
            force *= rb.mass;

            // scale for scale !
            force *= MasterForceScale;

            // use the force luke!!
            rb.AddForce( force);
        }
    }
}

Thx a lot. I’ll try it with mouse button!