Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Resolved Move towards has object moving in another direction

Discussion in 'Scripting' started by Carl_Bread, Apr 25, 2024.

  1. Carl_Bread

    Carl_Bread

    Joined:
    Jun 8, 2019
    Posts:
    8
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class BallLogic : MonoBehaviour
    4. {
    5.     public Vector3 offset;
    6.     public float ballSpeed;
    7.     private Transform target;
    8.     private Rigidbody rb;
    9.     public bool goingToTarget;
    10.     void Start()
    11.     {
    12.         if (target == null)
    13.         {
    14.             target = GameObject.Find("QB").transform;
    15.         }
    16.  
    17.         rb = GetComponent<Rigidbody>();
    18.     }
    19.  
    20.     void Update()
    21.     {
    22.         // if close to target, no longer going to target as has been caught
    23.         if (Vector3.Distance(transform.position, target.position) < .5)
    24.         {
    25.             goingToTarget = false;
    26.         }
    27.  
    28.         //
    29.         if (goingToTarget)
    30.         {
    31.             //   rb.velocity = Vector3.MoveTowards(transform.position, target.position, Mathf.Infinity)* ballSpeed * Time.deltaTime;
    32.             rb.velocity = Vector3.MoveTowards(transform.position, target.position, ballSpeed * Time.deltaTime);
    33.             Debug.Log(target.position);
    34.         } else {
    35.             target.gameObject.GetComponent<PathfindingScript>().hasBall = true;
    36.             transform.position = target.position + offset;
    37.             transform.rotation = target.rotation;
    38.         }
    39.     }
    40.  
    41.     public void GoToTarget(Transform targetz)
    42.     {
    43.         // I tried having it here but had the same issue
    44.         goingToTarget = true;
    45.         target = targetz;
    46.        
    47.         transform.position = transform.position + offset + new Vector3(0, 0, 2);
    48.     }
    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.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,949
    You're using the wrong tools in the wrong way here.

    Code (CSharp):
    1. rb.velocity = Vector3.MoveTowards(transform.position, target.position, ballSpeed * Time.deltaTime);
    This line of code makes no sense for several reasons
    1. MoveTowards with two positions as parameters is going to result in a position vector.
    2. A position vector is not suitable to use as a velocity. Velocity requires a direction and a magnitude. Not a position.
    3. Time.deltaTime likewise is not suitable here
    If you want to give your object a velocity towards a certain object you would do this:
    Code (CSharp):
    1. // Calculate the difference vector from our Rigidbody to the target
    2. Vector3 diff = target.position - rb.position;
    3. // Normalize the difference to get a unit direction vector
    4. Vector3 direction = diff.normalized;
    5.  
    6. // The desired velocity will be direction * speed.
    7. rb.velocity = direction * ballSpeed;
     
    Last edited: Apr 26, 2024
  3. Carl_Bread

    Carl_Bread

    Joined:
    Jun 8, 2019
    Posts:
    8
    Thank you appreciate it!