Hello guys ! I have a problem and I have no idea how to solve it. So basically I have an archer which shoots an arrow from a SpawnPoint. Then, the arrow goes out of the screen by Vector3.MoveTowards to a GameObject outside the screen, then, each enemy has 1 gameobject attached to it, which is outside the screen and a little to the left. I teleport the arrow to one of this points randomly, then I tell it to go to the parent gameobject(which is the enemy) by using Vector3.MoveTowards, but it doesnt work and i have no idea why. Here is the full code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Arrow : MonoBehaviour {
public Transform firstDestination;
public GameObject[] destinations;
private bool gotToFirstDestination;
public int randomDestination;
public float arrowSpeed = 7f;
public float step;
void Start ()
{
firstDestination = GameObject.Find ("FirstDestination").transform;
destinations = GameObject.FindGameObjectsWithTag ("ArrowDestinations");
gotToFirstDestination = false;
}
void Update ()
{
step = arrowSpeed * Time.deltaTime;
if (!gotToFirstDestination)
{
transform.position = Vector3.MoveTowards (transform.position, firstDestination.transform.position, step);
}
if (transform.position == firstDestination.transform.position)
{
gotToFirstDestination = true;
randomDestination = Random.Range (0, destinations.GetLength(0));
transform.position = destinations [randomDestination].transform.position;
if (transform.position == destinations [randomDestination].transform.position)
{
transform.position = Vector3.MoveTowards(transform.position, destinations [randomDestination].transform.position, step);
}
}
}
}[code]
Use code tags.
It looks like you MoveTowards, then set to a specific point, then MoveTowards again. The setting to a specific point will overwrite anything you’d done to it up to that point.
1 Like
Sorry about that, it was my first post, I edited it. I am also new to unity(this is only my second project) and even after you told me why that doesn’t work I dont really know how to fix it.
Let me see if I have your intended goal correct: You fire the arrow from point A, it goes off the screen towards point B, then it instantly moves to point C, and then flies towards point D, which is the enemy - I’m picturing like in Smash Bros when a character gets knocked off the screen and then can be seen falling in the distance, something like that?
1 Like
Yeah, you’ve got it completly correct ! Here is an image which allows me to explain easier : Untitled hosted at ImgBB — ImgBB
Point A is my SpawnPoint, firstDestination is point B, destinations [randomDestination] is my point C and destinations[randomDestination].parent is point D(also the enemy).
Alright, got it.
So, since this is a complex sequence of events that takes place over the course of many frames, I recommend not using Update to control, but a Coroutine. That way you get a lot less if-else type logic, and instead just run straight through the function.
void Start() {
StartCoroutine(FireSequence() );
}
IEnumerator FireSequence() {
while (transform.position != firstDestination) {
transform.position = Vector3.MoveTowards(transform.position, firstDestination, step);
yield return 0; // "wait 1 frame"
}
//pick destination here
transform.position = destinations[randomDestination].transform.position;
Vector3 secondDestination = destinations[randomDestination].transform.parent.position;
while (transform.position != secondDestination) {
transform.position = Vector3.MoveTowards(transform.position, secondDestination, step);
yield return 0; // "wait 1 frame"
}
//whatever end of sequence code happens here
}
1 Like
Honestly mate I don’t know how to thank you, it works perfectly ! Thanks a lot !