Beginner needs help. Moving object from a to b only in the beginning of the scene.

Hello. I just started playing with Unity and C# and I am a complete beginner. I did Flappy Birds tutorial by Renessaince Coders ( one of top Flappy birds-tutorials that pops up in Youtube) and I want to play around with it and try to understand how the code and different scripts work.

So, I have a portrait style flappy birds game. And what I am trying to do is to get my bird fly in to the game scene everytime you start a new game. I’ve tried everything what I can think of, also this bit of code (which by the way makes most sense to me) But I cannot get it to work.

transform.position = Vector3.MoveTowards(transform.position, destination, Time.deltaTime);

I thought this line would do the trick, but nope. Will post the TapController - script below. Which I think is the script where this movement should happen.

How simple is the solution that seems to be avoiding me? :smile:

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

[RequireComponent(typeof(Rigidbody2D))]
public class TapControlPlayer: MonoBehaviour {

    public delegate void PlayerDelegate();
    public static event PlayerDelegate OnPlayerDied;
    public static event PlayerDelegate OnPlayerScored;


    public float tapForce = 10;
    public float tiltSmooth = 5;
    public float movementSpeed = 10;
    public Vector3 startPos,endPos;

    public AudioSource tapAudio;
    public AudioSource scoreAudio;
    public AudioSource dieAudio;

    Rigidbody2D rigidBody;
    Quaternion downRotation;
    Quaternion forwardRotation;

    GameManager game;

    void Start(){

        rigidBody = GetComponent<Rigidbody2D>();
        downRotation = Quaternion.Euler(0, 0, -90);
        forwardRotation = Quaternion.Euler(0, 0, 35);
        game = GameManager.Instance;
        rigidBody.simulated = false;   
    }

    void OnEnable() {
        GameManager.OnGameStarted += OnGameStarted;
        GameManager.OnGameOverConfirmed += OnGameOverConfirmed;
       
    }
   


    void OnGameStarted() {
        transform.position = Vector3.MoveTowards(startPos, endPos, Time.deltaTime);
        rigidBody.velocity = Vector3.zero;
        rigidBody.simulated = true;

    }

    void OnGameOverConfirmed() {
        transform.localPosition = startPos;
        transform.rotation = Quaternion.identity;

    }
    void OnDisable() {
        GameManager.OnGameStarted -= OnGameStarted;
        GameManager.OnGameOverConfirmed -= OnGameOverConfirmed;
   
    }

    void Update() {
        if (game.GameOver) return;

        if (Input.GetMouseButtonDown(0)) {
            tapAudio.Play();
            transform.rotation = forwardRotation;
            rigidBody.velocity = Vector3.zero;
            rigidBody.AddForce(Vector2.up * tapForce, ForceMode2D.Force);
           
             
        }

        transform.rotation = Quaternion.Lerp(transform.rotation, downRotation, tiltSmooth * Time.deltaTime);

    }


    void OnTriggerEnter2D(Collider2D col) {
        if (col.gameObject.tag == "ScoreZone") {
            //register a score event
            OnPlayerScored(); //event sent to GameManager;
            //play a sound
            scoreAudio.Play();

        }

        if (col.gameObject.tag == "DeadZone") {
            rigidBody.simulated = false;
            //register dead event
            OnPlayerDied(); //event sent to GameManager;
            //play a sound
            dieAudio.Play();
        }
       
           
    }

}

MoveTowards is a one-time thing…move over this much right now once.

You would need to either use a Coroutine or else use Update() to move it multiple times by a small distance each frame.

Instead of doing this all in this one main script I recommend making an intro script that:

  • disables the playing script
  • flaps him in (using Update())
  • upon his arrival, that script disables itself and enables the main control script.

Alternately just make an animation of the bird coming in and play that animation, then turn on your main control script to take player control. That’s super-easy, almost no coding.

Technically even TapController has way too much code in it right now: it has collision code too so it looks more like a EntireBirdController… always best to keep areas of concern separated.

Thanks!

Will look into both of these options!
We’ll he did say in a tutorial that TapController got a it’s a bit messy but…