Unity Vector3.Distance calculating wrong
So my problem is that I have a script that throwes an object along an parabola to the mouse position with a maximum distance but when I move the origin the distance is calculated wrong(it seems that its calculated from 0,0,0) can anyone help I dont know what the problem is? (Sorry for the bad english I'm from Germany) :) :)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;
[ExecuteInEditMode]
[RequireComponent(typeof(LineRenderer))]
public class Normal_Thrower : MonoBehaviour
{
public Image UltiImage;
[HideInInspector]
public float Ulti;
public float maxAmmo = 3;
public float currentAmmo;
public float reload;
public HealthBar healthBar;
public Camera viewCamera;
public GameObject Projectile;
private new Rigidbody rigidbody;
public Transform target;
public Vector3 InitialVelocity;
public LineRenderer lineRenderer;
public bool everhigh;
public float maxDistance = 2f;
public float maximumHeightOfArc;
public float minimumHeightOfArc;
public float gravity;
public int pathResolution;
private bool aim;
public float height;
private bool isLaunching;
private Vector3 savedPosition;
struct LaunchData
{
public readonly Vector3 initialVelocity;
public readonly float durationTime;
public LaunchData(Vector3 velocity, float time)
{
this.initialVelocity = velocity;
this.durationTime = time;
}
}
LaunchData CalculateLaunchData()
{
float displacementY = target.position.y - transform.position.y;
Vector3 displacementXZ = new Vector3(target.position.x - transform.position.x, 0, target.position.z - transform.position.z);
Vector3 velocityY = Vector3.up * Mathf.Sqrt(-2 * gravity * height);
Vector3 velocityXZ = displacementXZ / (Mathf.Sqrt(-2 * height/ gravity) + Mathf.Sqrt(2 * (displacementY - maximumHeightOfArc) / gravity));
float time = Mathf.Sqrt(-2 * height / gravity) + Mathf.Sqrt(2 * (displacementY - height) / gravity);
return new LaunchData(velocityXZ + velocityY * -Mathf.Sign(gravity), time);
}
public void Shoot(InputAction.CallbackContext ctx)
{
if (ctx.started)
{
aim = true;
}
if (ctx.canceled)
{
aim = false;
if(currentAmmo >= 1f)
{
TakeDamage(1f);
Launch();
}
}
}
void Launch()
{
GameObject currentBullet = Instantiate(Projectile, transform.position, Quaternion.identity);
currentBullet.GetComponent<Throw_Bullet>().LifeTime(gameObject);
rigidbody = currentBullet.GetComponent<Rigidbody>();
rigidbody.useGravity = true;
Physics.gravity = Vector3.up * this.gravity;
this.isLaunching = true;
this.savedPosition = rigidbody.position;
LaunchData data = CalculateLaunchData();
if (!float.IsNaN(data.initialVelocity.y) && !float.IsInfinity(data.initialVelocity.y))
rigidbody.velocity = data.initialVelocity;
else
{
rigidbody.useGravity = false;
this.isLaunching = false;
rigidbody.position = new Vector3(Random.insideUnitCircle.x * Random.Range(-100, 100), 0, Random.insideUnitCircle.y * Random.Range(-100, 100));
rigidbody.velocity = Vector3.zero;
}
}
void DrawPath()
{
LaunchData launchData = CalculateLaunchData();
if (float.IsNaN(launchData.initialVelocity.y) || float.IsInfinity(launchData.initialVelocity.y))
{
return;
}
Vector3 originalPosition = transform.position;
Vector3[] positions = new Vector3[this.pathResolution + 1];
for (int i = 0; i <= this.pathResolution; i++)
{
float simulationTime = (i / (float)this.pathResolution) * launchData.durationTime;
Vector3 displacement = launchData.initialVelocity * simulationTime + (Vector3.up * gravity) * simulationTime * simulationTime / 2f;
positions[i] = originalPosition + displacement;
}
this.lineRenderer.positionCount = positions.Length;
this.lineRenderer.SetPositions(positions);
this.InitialVelocity = launchData.initialVelocity;
}
void Start()
{
currentAmmo = maxAmmo;
healthBar.SetMaxHealth(maxAmmo);
UltiImage.fillAmount = 0f;
aim = false;
rigidbody.useGravity = false;
this.isLaunching = false;
}
void Update()
{
if (currentAmmo < maxAmmo)
{
currentAmmo += reload;
}
else
{
currentAmmo = maxAmmo;
}
healthBar.SetHealth(currentAmmo);
Ulti = UltiImage.fillAmount;
Vector3 mousePos = viewCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, viewCamera.transform.position.y));
Vector3 targetpos = mousePos + Vector3.up * transform.position.y;
float Distance = Mathf.Clamp(Vector3.Distance(transform.position, targetpos), 0, maxDistance);
Debug.Log(Distance);
if (everhigh)
{
height = maximumHeightOfArc;
}
else
{
height = Mathf.Clamp((minimumHeightOfArc + Distance/maximumHeightOfArc), minimumHeightOfArc, maximumHeightOfArc);
}
if (aim)
{
target.position = transform.forward * Distance;
target.position += Vector3.up;
lineRenderer.enabled = true;
target.GetComponent<MeshRenderer>().enabled = true;
DrawPath();
}
else
{
lineRenderer.enabled = false;
target.GetComponent<MeshRenderer>().enabled = false;
}
}
void OnValidate()
{
if (aim)
{
DrawPath();
}
}
public void ReloadUlti(float Reload)
{
Ulti += Reload;
UltiImage.fillAmount = Ulti;
}
void TakeDamage(float damage)
{
currentAmmo -= damage;
healthBar.SetHealth(currentAmmo);
}
}
Comment
This seems like too much code to look through. In line 163 getting the distance to targetPos seems fine. Probably targetPos is the wrong number. You could try having the code place a red ball at targetPos, to check.
Answer by Omikron1 · Jul 31, 2021 at 06:28 AM
I have an plane with an texture on it being at targetpos and the position is wrong! :(