Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ltrout1999 · Jan 12, 2016 at 03:53 AM · vector3distanceproblem during runtime

Vector3.Distance returning weird distances?

So I set it up to where the enemy tank would shoot the distance so it wouldn't miss. I had it setup to where the tank would miss 45% of the time but here is the code

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using PlayerInfo;
 using Shell;
 
 namespace EnemyInfo
 {
     public class EnemyAttack : MonoBehaviour 
     {
         public float timeBetweenAttacks = 0.75f;
         public float attackDamage;
         public Rigidbody Shell;
         public Transform FireTransform;
         public AudioSource ShootingAudio;
         public AudioClip Firing;
         public float LaunchForce;
         public Transform ETank;
         public Transform PTank;
         public int missChance = 45;
 
         GameObject player;
         PlayerHealth pHealth;
         EnemyHealth eHealth;
         PlayerAttack pShoot;
         EnemyAttack eShoot;
         float timer;
         EnemyMovement eMove;
         ShellExplosion sExplo;
         bool isMiss;
         float distance;
         int randPercent;
 
         void OnEnable ()
         {
 
         }
 
         void Awake ()
         {
             player = GameObject.FindGameObjectWithTag ("Player");
             pHealth = player.GetComponent<PlayerHealth> ();
             eHealth = GetComponent<EnemyHealth> ();
             eMove = GetComponent<EnemyMovement> ();
         }
 
         void FixedUpdate ()
         {
             float distance = Vector3.Distance (PTank.position, ETank.position);
         }
 
         void LateUpdate ()
         {
             timer += Time.deltaTime;
 
             if (timer >= timeBetweenAttacks && eMove.inRange && eHealth.CurrentHealth > 0 && pHealth.CurrentHealth > 0)
             {
                 Attack ();
             }
         }
 
         void Attack ()
         {
             int randPercent = Random.Range (1, 100);
 
             if (randPercent <= missChance) 
             {
                 LaunchForce = Random.Range (30, 50);
             } 
             else 
             {
                 LaunchForce = distance;
             }
 
             Debug.Log (LaunchForce);
 
             timer = 0f;
 
             Rigidbody shellInstance = Instantiate (Shell, FireTransform.position, FireTransform.rotation) as Rigidbody;
 
             shellInstance.velocity = LaunchForce * FireTransform.forward;
         }
     }
 }

The problem I am having is the enemy tank will shoot, but it will be a distance of 0 units. No matter what I do. Unless I stick an actual number in there, it will shoot 0.

Comment
Add comment · Show 10
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 ltrout1999 · Jan 12, 2016 at 03:53 AM 0
Share

@dhore , since the other thread was getting out of control lol.

avatar image ltrout1999 · Jan 12, 2016 at 04:02 AM 0
Share

Also, HERE is a link to my GitHub which has all of the code on it.

avatar image brunocoimbra · Jan 12, 2016 at 04:15 PM 0
Share
 void FixedUpdate ()
 {
     float distance = Vector3.Distance (PTank.position, ETank.position);
 }

Just delete "float" here, like this:

 void FixedUpdate ()
 {
     distance = Vector3.Distance (PTank.position, ETank.position);
 }

It should work now...

avatar image ltrout1999 brunocoimbra · Jan 12, 2016 at 04:19 PM 0
Share

Tried that... Still, the tank doesn't correctly shoot. The bullet flies 0 units and does damage to the enemy tank since it's not actually moving.

avatar image brunocoimbra ltrout1999 · Jan 12, 2016 at 04:45 PM 0
Share

Are you setting the transforms (player, enemny and fire) in the inspector correctily? Did you tried to use "Debug.Log(distance);" to see the actual distance value?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by dhore · Jan 12, 2016 at 04:26 AM

Try changing your "Shell" variable on line 13 to be a GameObject instead of a Rigidbody, then change line 79 to this:

 Rigidbody shellInstance = (Instantiate (Shell, FireTransform.position, FireTransform.rotation) as GameObject).GetComponent<Rigidbody>();
Comment
Add comment · Show 9 · 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
avatar image ltrout1999 · Jan 12, 2016 at 04:33 AM 0
Share

Nope still doing the same thing. As soon as the tank shoots the shell travels 0 units and blows up the enemy.

avatar image dhore ltrout1999 · Jan 12, 2016 at 04:37 AM 0
Share

ooohhh... are you spawning the bullet inside of the enemy ?? You need to spawn it in front of the enemy enough so that it doesn't collide with the enemy. Depending on how large your enemy's collision area is, enemy.transform.position + enemy.transform.forward will generally suffice for a spawn location.

avatar image ltrout1999 dhore · Jan 12, 2016 at 04:44 AM 0
Share

No i'm not spawning the bullet inside of the enemy. I have a FireTransform for the bullet setup about .5-1 units infront of the barrel of the enemy.

Show more comments

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

42 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

Related Questions

How I can make my camera keep a distance between two objects? 0 Answers

InverseTransformPoint() help 0 Answers

Calculate a points distance from a vector 3 2 Answers

vector3 distance same script different objects 0 Answers

Dot Product not equals 1 0 Answers


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