using UnityEngine;
public class BallLogic : MonoBehaviour
{
public Vector3 offset;
public float ballSpeed;
private Transform target;
private Rigidbody rb;
public bool goingToTarget;
void Start()
{
if (target == null)
{
target = GameObject.Find("QB").transform;
}
rb = GetComponent<Rigidbody>();
}
void Update()
{
// if close to target, no longer going to target as has been caught
if (Vector3.Distance(transform.position, target.position) < .5)
{
goingToTarget = false;
}
//
if (goingToTarget)
{
// rb.velocity = Vector3.MoveTowards(transform.position, target.position, Mathf.Infinity)* ballSpeed * Time.deltaTime;
rb.velocity = Vector3.MoveTowards(transform.position, target.position, ballSpeed * Time.deltaTime);
Debug.Log(target.position);
} else {
target.gameObject.GetComponent<PathfindingScript>().hasBall = true;
transform.position = target.position + offset;
transform.rotation = target.rotation;
}
}
public void GoToTarget(Transform targetz)
{
// I tried having it here but had the same issue
goingToTarget = true;
target = targetz;
transform.position = transform.position + offset + new Vector3(0, 0, 2);
}
When the object is told to go to the target it goes in the opposite direction. I couldn’t find anything online about other people having this issue. I tried having it look at the target and then move forward but I had a similar issue.