Finding the midpoint between 2 objects and off setting the mid point left or right slightly

I am have made a line render tool that has a start middle and end point. The line curves with a bezier curve algorithm. The middle point moves around by finding the mid point between the start and end points of the Line and the height scales with the distance between the two points. What im trying to do is offset the floating mid point left or right as if the line is curving up and to the left or right.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveMiddlePoint : MonoBehaviour
{
    [Header("Mid Point Box")]
    public GameObject player;
    public Transform targetTransform;
    public GameObject target;
    public GameObject midPoint;
    public float initialDistance;
    public float height;
    public bool isDistanceIncreasing;
    public KeyCode heightUp = KeyCode.T;
    public KeyCode heightDown = KeyCode.G;
    public KeyCode curveLeft = KeyCode.Z;
    public KeyCode curveRight = KeyCode.X;
    public float offset = 0;

    // Start is called before the first frame update
    void Start()
    {
 
    }

    // Update is called once per frame
    void Update()
    {
        //if the start or end point has changed, run UpdateMidTransform
        if (player.transform.hasChanged || target.transform.hasChanged)
        {
            UpdateMidTransform();
        }
        //raises or lowers midpoint
        if (Input.GetKey(heightUp))
        {
            height = height + 0.1f;
        }

        if (Input.GetKey(heightDown))
        {
            height = height - 0.1f;
        }

        // Calculate the distance between ObjectA and ObjectB
        float distance = Vector3.Distance(player.transform.position, target.transform.position) /2f;

        // Adjust the Y position based on the distance
        float newYPosition = distance * height;

        Vector3 newPosition = transform.position;
        newPosition.y = newYPosition;
        transform.position = newPosition;

    }

    void UpdateMidTransform()
    {
        //finds middle point by addding both start and end position of line and dividing it by 2
        Vector3 middlepoint = (player.transform.position + target.transform.position) / 2f;
        transform.position = middlepoint;

    }
}

2 Answers

2

When you say “left or right” do you really mean “closer-to-player or closer-to-target”. Or do you mean “left or right from the player’s perspective”?

If you want to offset it from the player’s perspective, you could do something like this:

Vector3 middlepoint = (player.transform.position + target.transform.position) / 2f;

Vector3 direction = target.transform.position - player.transform.position.
//The cross product is always mutually perpendicular to the input vectors
Vector3 cross = Vector3.Cross(direction. Vector3.up);
cross.Normalize();
float leftwardsOffset = 5f; //or however you want to set this
middlepoint += leftwardsOffset * cross;

transform.position = middlepoint;

Thanks for the reply, yes i mean maintain the box in the middle between start and end but move left and right while maintaining in the middle position of both ends. I hope to be able input and make the the curve bend to either go straight up and down or up to the left or right and back down to the target. I will try this solution but i am also going to try and make extra mid point objects for left and right that get the position of the original mid point and offset them that way.