Search Unity

Question How to calculate a position between object A and B?

Discussion in 'Scripting' started by Kekeshi, Mar 12, 2023.

  1. Kekeshi

    Kekeshi

    Joined:
    Oct 19, 2022
    Posts:
    5
    So I have two positions and I want a position that is always the same distance away from object A and is at object B's direction. Here is what I tried:

    Code (CSharp):
    1.                    
    2. Vector3 dir = Vector3.Normalize(hitInfo.point - Camera.main.transform.position);
    3. Vector3 pos = Camera.main.transform.position + dir * distance; // distance is 2
    4. placeableObject.transform.position = new Vector3(pos.x, 0.5f, pos.z);
    The problem is, when B is moving further away from A, the dir vector is changing as well and the position I'm looking is for is moving further away from A as well. The directions are good, but I don't know how to calculate a position that is always the same distance away from A.
     
  2. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
  3. Kekeshi

    Kekeshi

    Joined:
    Oct 19, 2022
    Posts:
    5
    Well, not really. Or at least I don't know how this would be useful in my case.
    I'm not looking for the distance variable, that would be constantly 2 or whatever I change it to.
    I want to calculate a position that is always x distance away from point A.
     
    Last edited: Mar 12, 2023
  4. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    Kurt-Dekker and Kekeshi like this.
  5. Kekeshi

    Kekeshi

    Joined:
    Oct 19, 2022
    Posts:
    5