Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How do i substract value from a gameobject method(see code)

Discussion in 'Scripting' started by Mqtt30, Mar 31, 2023.

  1. Mqtt30

    Mqtt30

    Joined:
    Jun 8, 2017
    Posts:
    23
    This is my piece of script

    transform.DOMove((enemy.ClosestTarget().transform.position), .3f);


    how could i substract a specific value from that "(enemy.ClosestTarget().transform.position)".

    Enemy is a reference to another script, and ClosestTarget is a method inside that script, closest target is a gameobject method and it returns the closest object.
    In this case i want to take that position and substract a certain number, it works without but i'd like to move the player a little far away than the exact position.

    In case anyone needs it:

    Code (CSharp):
    1. public GameObject ClosestTarget()
    2.     {
    3.         GameObject[] gos;
    4.         gos = GameObject.FindGameObjectsWithTag(enemyTag);
    5.         GameObject closest = null;
    6.         float distance = maxDistance;
    7.         float currAngle = maxAngle;
    8.         Vector3 position = transform.position;
    9.  
    10.         // If there's already a current target and it's still within range, return it
    11.         if (currentTarget && (currentTarget.position - transform.position).magnitude < maxDistance)
    12.         {
    13.             return currentTarget.gameObject;
    14.         }
    15.         else
    16.         {
    17.             isTargeting = false;
    18.         }
    19.  
    20.         foreach (GameObject go in gos)
    21.         {
    22.             Vector3 diff = go.transform.position - position;
    23.             float curDistance = diff.magnitude;
    24.             if (curDistance < distance)
    25.             {
    26.                 Vector3 viewPos = mainCamera.WorldToViewportPoint(go.transform.position);
    27.                 Vector2 newPos = new Vector3(viewPos.x - 0.5f, viewPos.y - 0.5f);
    28.                 if (Vector3.Angle(diff.normalized, mainCamera.transform.forward) < maxAngle)
    29.                 {
    30.                     closest = go;
    31.                     currAngle = Vector3.Angle(diff.normalized, mainCamera.transform.forward.normalized);
    32.                     distance = curDistance;
    33.                 }
    34.             }
    35.         }
    36.  
    37.        
    38.         return closest;
    39.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    A position is a vector, a number is a scalar.

    Do you mean instead, "how do I adjust a position by a distance in a given direction?"

    If so, and given:

    Code (csharp):
    1. Vector3 position = ...
    2. Vector3 direction = ...
    3. float distance = ...
    Then:

    Code (csharp):
    1. Vector3 adjustedPosition = position + direction * distance;
    Otherwise, not sure what you might intend by subtracting a scalar from a vector.
     
  3. Mqtt30

    Mqtt30

    Joined:
    Jun 8, 2017
    Posts:
    23
    i'd like to extract the position from enemy.ClosestTarget().transform.position, to then subtract a number, for examle:
    vecto3 ex = new Vector3((enemy.ClosestTarget().transform.position.x) -2f), it doesnt work obviously, i literally want to create a vector3 out of that information
     
    Last edited: Mar 31, 2023
  4. Mqtt30

    Mqtt30

    Joined:
    Jun 8, 2017
    Posts:
    23
    i found a workaround, i give the enemy a rigid body and adjusted the angular drag, so that the force of the impact moves the enemy and i dont need to move the player instaed
     
  5. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    You can create a new vector and subtract it from the one you have:
    vecto3 ex = enemy.ClosestTarget().transform.position - new Vector3(-2f,0,0);