Есть ли способ в Unity 3d определить, сколько раз произошло столкновение?сколько раз действует void oncollisionenter?
Например, если 3 раза убить врага.
или если два раза уменьшить срок службы на 50 процентов.
Я хочу сделать это с помощью функции void OnCollisionEnter
..
Это мой код AI и мой код игрока:
public Transform[] Targets;
private int DestPoint = 0;
private NavMeshAgent Agent;
public Transform Player;
public Rigidbody Bullet;
public Transform Instantiator;
public float BulletSpeed;
public float fireRate;
private float nextFire = 0F;
void Start()
{
Agent = GetComponent<NavMeshAgent>();
Agent.autoBraking = false;
}
void Update()
{
if (Vector3.Distance(transform.position, Player.position) < 100)
{
transform.LookAt (Player);
if (Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Agent.Stop();
Shoot();
}
}
else if (Vector3.Distance(transform.position, Player.position) > 100)
{
GotoNextPoint();
}
}
void GotoNextPoint()
{
Agent.destination = Targets [DestPoint].position;
DestPoint = (DestPoint + 1) % Targets.Length;
}
void Shoot()
{
Rigidbody Clone = Instantiate (Bullet, Instantiator.position, Instantiator.rotation) as Rigidbody;
Clone.AddForce (Instantiator.forward * Time.deltaTime * BulletSpeed);
}
public float Speed;
public float RotationSpeed;
public Rigidbody Bullet;
public float BulletSpeed;
public Transform Instantiator;
public float fireRate;
private float nextFire = 0F;
void Update()
{
if (CrossPlatformInputManager.GetAxis("Vertical") > 0)
{
transform.Translate (Vector3.forward * Time.deltaTime * Speed);
}
if (CrossPlatformInputManager.GetAxis("Vertical") < 0)
{
transform.Translate (Vector3.back * Time.deltaTime * Speed);
}
if (CrossPlatformInputManager.GetAxis("Horizontal") > 0)
{
transform.Rotate (Vector3.up * Time.deltaTime * RotationSpeed);
}
if (CrossPlatformInputManager.GetAxis("Horizontal") < 0)
{
transform.Rotate (Vector3.down * Time.deltaTime * RotationSpeed);
}
if (CrossPlatformInputManager.GetButtonDown ("Shoot") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Rigidbody Clone = Instantiate (Bullet, Instantiator.position, Instantiator.rotation);
Clone.AddForce (Instantiator.forward * Time.deltaTime * BulletSpeed);
}
}
спасибо спасибо! –
Anytime Arash ... – Programmer
Если у вас нет только одного врага, и единственное, что сталкивается с игроком, это враги, вам нужно будет иметь это на самом вражеском объекте. В противном случае я мог бы дважды столкнуться против одного врага, а затем один раз против другого и сделать один выстрел. – code11