Hi,
Im trying to delay the tracking of my missile for 3 seconds after launch, however the coroutine will not execute the tracking after 3 seconds even though the debug.log appears after 3 seconds. Here is the code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class andanotherhomingmissile : MonoBehaviour
{
public Transform target;
public Rigidbody RocketRgb;
public float turnspeed = 1f;
public float flyspeed = 10f;
private Transform rocketlocaltrans;
// Start is called before the first frame update
void Start()
{
if (!target)
Debug.Log("please set the rocket target");
rocketlocaltrans = GetComponent<Transform>();
StartCoroutine(Begintracking());
}
// Update is called once per frame
private void FixedUpdate()
{
if (!RocketRgb)
return;
RocketRgb.velocity = rocketlocaltrans.forward * flyspeed;
}
public IEnumerator Begintracking()
{
yield return new WaitForSeconds(3f);
var TargetRot = Quaternion.LookRotation(target.position - rocketlocaltrans.position);
RocketRgb.MoveRotation(Quaternion.RotateTowards(rocketlocaltrans.rotation, TargetRot, turnspeed));
Debug.Log("Begintracking");
}
}
Try to use invoke:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class andanotherhomingmissile : MonoBehaviour
{
public Transform target;
public Rigidbody RocketRgb;
public float turnspeed = 1f;
public float flyspeed = 10f;
private Transform rocketlocaltrans;
// Start is called before the first frame update
void Start()
{
if (!target)
Debug.Log("please set the rocket target");
rocketlocaltrans = GetComponent<Transform>();
Invoke("Begintracking", 2f) ;
}
// Update is called once per frame
private void FixedUpdate()
{
if (!RocketRgb)
return;
RocketRgb.velocity = rocketlocaltrans.forward * flyspeed;
}
public void Begintracking()
{
var TargetRot = Quaternion.LookRotation(target.position - rocketlocaltrans.position);
RocketRgb.MoveRotation(Quaternion.RotateTowards(rocketlocaltrans.rotation, TargetRot, turnspeed));
Debug.Log("Begintracking");
}
}`
If your console is logging the Debug.Log("Begintracking")
then clearly this is not an issue with how you’re calling your IEnumerator
, so no worries there. Your issue is that you’re only rotating on one single frame 3 seconds after start. An IEnumerator
does not inherently repeat anymore times than it’s told to, its just a method that can run async, whether that be one or infinite times.
So, either you mean to set the rotation to the target rotation once after 3 seconds or, more likely, you mean to repeatedly iterate your `IEnumerator` to face the coordinates and *home in on your target* after an initial 3 second wait:
public class andanotherhomingmissile : MonoBehaviour
{
public Transform target;
public Rigidbody RocketRgb;
public float turnSpeed = 180f;
public float flyspeed = 10f;
private Transform rocketlocaltrans;
// Start is called before the first frame update
void Start()
{
if (!target)
Debug.Log("please set the rocket target");
rocketlocaltrans = GetComponent<Transform>();
StartCoroutine(Begintracking());
}
// Update is called once per frame
private void FixedUpdate()
{
if (!RocketRgb)
return;
RocketRgb.velocity = rocketlocaltrans.forward * flyspeed;
}
public IEnumerator Begintracking()
{
yield return new WaitForSeconds(3f);
Debug.Log("Begintracking");
Quaternion TargetRot = Quaternion.identity;
while(true)
{
TargetRot = Quaternion.LookRotation(target.position - rocketlocaltrans.position);
RocketRgb.MoveRotation(Quaternion.RotateTowards(rocketlocaltrans.rotation, TargetRot, turnSpeed * Time.deltaTime));
yield return null;
}
}
}
It's worth noting that the `while(true)...yield return null;` means that this coroutine will repeat continuously until it's told to do something else, but I'll leave that up to you. Hope this helps!