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.
@dhore , since the other thread was getting out of control lol.
Also, HERE is a link to my GitHub which has all of the code on it.
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...
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.
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?
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>();
Nope still doing the same thing. As soon as the tank shoots the shell travels 0 units and blows up the enemy.
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.
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.
Your answer
Follow this Question
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