Hello! I’ve looked around I can’t seem to find the answer I’m looking for and was hoping to get some help from here.
I want to have a birdsnest and when one of the players walks into it a bird will spawn outside the screen and fly towards that position and keep going until it goes outside the other side of the screen where it gets removed from the game. I’m stuck, any help is appreciated!
what do you have so far?
I was just about to write that haha. Currently I have drawn a sprite for the birdsnest and it has a rigidbody and a tag. If player one walks into it it prints 1 and if player two walks into it it prints 2. It’s not much, I’m trying to figure out how to create and move the bird towards the one who walked into it
public class BirdAI : MonoBehaviour
{
public float speed;
private Rigidbody2D rb;
public Vector2 aPosition0 = new Vector2(0,0);
public Vector2 aPosition1 = new Vector2(3,3);
// Use this for initialization
void Start ()
{
rb = GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void Update ()
{
transform.position = Vector2.MoveTowards (new Vector2 (transform.position.x, transform.position.y), aPosition1, 3 * Time.deltaTime);
}
}
The Bird moves with this right now.
ok, you don’t need the “aPosition0” (assuming thats the initial spawn point?) in the bird script, you need that in the birdnest script. The birdnest needs a reference to a prefab of the bird, and the start location so it can spawn it there.
Personally I’d have both the start and destination as values on the birdnest and just pass the destination to the bird when it’s spawned, I’d also have the two points as empty gameobjects as children of the nest so you can easily manipulate them in the scene;
GameObject birdie = Instantiate(birdPrefab, spawnpoint.transform.position, spawnpoint.transform.rotation) as GameObject;
birdie.SetDestination(destinationPoint.transform.position); // new function in the BirdAI class that sets "aPosition1"
If you’re moving rigidbodies using direct position changes (rather than forces) remember to set the rigidbody to kinematic.
Thank you for the answer but I think that is too complicated for me considering my current knowledge of programming
But I did a “aggro” function in the birdsnest and if player1 walks into it it sets aggro to true. Then in the bird script it checks if aggro is true and if it is it starts to move.
Is there an easy way to tell the bird only to take the position of the player on time and then travel towards that point?
get a reference to the player (inspector/find/tag etc.) and store the position as a variable in the start function.
Vector2 targetPosition;
void Start()
{
targetPosition = GameObject.FindWithTag("Player").transform.position;
}
… assuming you are instantiating the bird at the time it needs to start moving. Or add that to the “aggro” function.
Yeah I managed to get the Bird to move towards the player who collided with its nest with this.
BirdsNest:
void OnTriggerEnter2D(Collider2D collider)
{
GameObject theBird = GameObject.Find("Bird");
BirdAI bird = theBird.GetComponent<BirdAI>();
if (collider.gameObject.tag == "Player1")
{
aggro = true;
player1 = true;
player2 = false;
}
if (collider.gameObject.tag == "Player2")
{
aggro = true;
player2 = true;
player1 = false;
}
}
Bird:
void Start ()
{
rb = GetComponent<Rigidbody2D> ();
player1 = GameObject.FindGameObjectWithTag ("Player1");
player2 = GameObject.FindGameObjectWithTag ("Player2");
}
// Update is called once per frame
void Update ()
{
GameObject theNest = GameObject.Find("Nest");
BirdsNest nest = theNest.GetComponent<BirdsNest>();
if (nest.aggro && nest.player1)
{
transform.position = Vector2.MoveTowards (new Vector2 (transform.position.x, transform.position.y),new Vector3(player1.transform.position.x,player1.transform.position.y), 3 * Time.deltaTime);
}
if (nest.aggro && nest.player2)
{
transform.position = Vector2.MoveTowards (new Vector2 (transform.position.x, transform.position.y),new Vector3(player2.transform.position.x,player2.transform.position.y), 3 * Time.deltaTime);
}
}
It’s not the best solution but it works for now. Do you have any tips on how to set a start position that it can return to after it collides?