- Home /
why dont my enemy bullets kill the playership?,why dont my enemy bullets kill the player?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Projectiles : MonoBehaviour
{
public float range = 50.0f;
public float enemyGunImpulse = 20.0f;
// The target marker.
[SerializeField] Transform playerShip;
[SerializeField] Transform target;
[SerializeField] Transform parent;
// Angular speed in radians per sec.
[SerializeField] float speed;
bool onRange = false;
public Rigidbody projectile;
private void Start()
{
float rand = Random.Range(1.0f, 2.0f);
InvokeRepeating("Shoot", 2, rand);
AddSphereCollider();
}
private void AddSphereCollider()
{
Collider sphereCollider = gameObject.AddComponent<SphereCollider>();
sphereCollider.isTrigger = false;
}
void Shoot()
{
if (onRange)
{
Rigidbody enemyGun = (Rigidbody)Instantiate(projectile, transform.position + transform.forward, transform.rotation);
enemyGun.AddForce(transform.forward * enemyGunImpulse, ForceMode.Impulse);
}
}
void Update()
{
Vector3 targetDir = target.position - transform.position;
// The step size is equal to speed times frame time.
float step = speed * Time.deltaTime;
Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0f);
Debug.DrawRay(transform.position, newDir, Color.red);
// Move our position a step closer to the target.
transform.rotation = Quaternion.LookRotation(newDir);
onRange = Vector3.Distance(transform.position, playerShip.position) < range;
if (onRange)
transform.LookAt(playerShip);
}
}
Answer by SlowCircuit · Feb 22, 2019 at 03:26 AM
You're destroying the bullet immediately after creating it.
Answer by $$anonymous$$ · Feb 22, 2019 at 03:59 AM
Hi, I want to Thank you for responding. I was able to get the enemy gun to kill the player finally but the death fx explosion for the player doesn't happen? Any feedback is appreciated :)
I removed this line of code: Destroy(bullet.gameObject, 2);
Hi, I updated my code. At one point I had scripts separated but then when I did the player ship would be cloned in spawn time and I was getting many errors but the enemy bullets worked. I just combined the enemy management.cs with the projectiles.cs and no longer get errors but the enemy bullets do not kill the player ship.
Any feedback Ino longer get enemy bullets to kill player ship is welcomed :/
Your answer
Follow this Question
Related Questions
How to stop weapon from firing after depleting ammo? 1 Answer
Increasing the "hang time" of a projectile 0 Answers
Firing projectiles on the XZ Plane 2 Answers
[SOLVED] Top down shooter projectiles can shoot side to side, but not up and down, why? 0 Answers
Setting the velocity of a projectile based on the angle of the cannon 2 Answers