Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Omikron1 · Jul 30, 2021 at 01:48 PM · vector3distance

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
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Jul 30, 2021 at 02:44 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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! :(

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

196 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Dot Product not equals 1 0 Answers

Don't trust "Vector3.Distance"? 1 Answer

Error with vector3.distance 0 Answers

Distance between two objects, from the view of the camera. 0 Answers

Vector3.Distance - Triggering an action on distance 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges