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;
}
}
