- Home /
Detection bullets hit
Is there any way to get from EmptyGameObject, what he hitted and where? Not loosing FPS, for example for scene, where going to be, more than 1000+ bullets (for IOS).
Gun Script (that shoots empty game objects with sprite on them):
#pragma strict
var bulletPrefab : GameObject;
var spawnPoint : Transform;
var frequency : float = 10;
var coneAngle : float = 1.5;
var firing : boolean = false;
var damagePerSecond : float = 20.0;
var forcePerSecond : float = 20.0;
var hitSoundVolume : float = 0.5;
private var lastFireTime : float = -1;
function fireOn()
{
var coneRandomRotation = Quaternion.Euler (Random.Range (-coneAngle, coneAngle), Random.Range (-coneAngle, coneAngle), 0);
if (Time.time > lastFireTime + 1 / frequency)
{
lastFireTime = Time.time;
var bulletfly = Instantiate(bulletPrefab, spawnPoint.transform.position, spawnPoint.transform.rotation*coneRandomRotation);
}
}
function Start () {
}
function Update () {
fireOn();
}
Answer by Mr.Jwolf · Jul 10, 2012 at 02:49 PM
You can use Raycasting. Your bulletPrefab should have a script that tests if it hits something (this requires the objects you want to hit, to have a collider).
The bullet script could look like this:
#pragma strict
// The position in the previous frame private var previousPosition: Vector3;
function Start () { previousPosition = transform.position; }
function Update () { Fly(); HitTest(); }
function Fly() { //... moves the bullet ... }
function HitTest() { var forwardDirection = transform.forward; var raycastDistance : float = Vector3.Distance(transform.position, previousPosition); var raycastHitInfo : RaycastHit; // Makes a raycast that returns true if it hits any colliders. if (Physics.Raycast (previousPosition, forwardDirection, raycastHitInfo, raycastDistance)) { // -- Hitted a collider -- OnBulletHit(raycastHitInfo); } previousPosition = transform.position; }
function OnBulletHit(raycastHitInfo : RaycastHit) { var hitObject : GameObject = raycastHitInfo.transform.gameObject; Debug.Log("Hitted: "+hitObject +" at point: "+raycastHitInfo.point); // ((AIScript) hitObject.GetComponent("AIScript")).OnBulletHit(); Destroy (gameObject); }
Answer by Ranger-Ori · Jul 10, 2012 at 02:08 PM
Yes, if you are using raycasting, you can save the points (Vector3). Which afterward you can use for anything you would like. I wouldn't save all of the points, for as you said, it will decrease the FPS and performance, however, you can use just one variable for a point and use it to "capture" each of the shots.
Your answer
Follow this Question
Related Questions
Prevent Bullets from going through objects/ No raycast 3 Answers
Ammo Script 1 Answer
Rotate Bullet with rotating Turret PROPERLY 2 Answers
Circular Bullet Spread for Shotgun / Bloom 1 Answer
Beginner to raycasting: making a gun 1 Answer