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?
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();
}
}
}