x


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();
}
more ▼

asked Jul 10 '12 at 01:46 PM

seashell gravatar image

seashell
25 3 6 9

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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);
}
more ▼

answered Jul 10 '12 at 02:49 PM

Mr.Jwolf gravatar image

Mr.Jwolf
88 3 4 4

thanks man! Perfect!

Jul 11 '12 at 10:54 AM seashell
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jul 10 '12 at 02:08 PM

Ranger Ori gravatar image

Ranger Ori
108 3 11 15

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x446
x308
x193

asked: Jul 10 '12 at 01:46 PM

Seen: 392 times

Last Updated: Jul 11 '12 at 10:54 AM