- Home /
Need Help with Bullets
Hi all. I have a simple game where my enemy chases my player and tries to catch the player. I have created a weapon script and, have a raycast that is always pointing at my player. So I'm shooting a ray cast at the player all the time? What I would like to know is instead of having a raycast how can I have a bullet hitting my player instead? I think I have something almost working but even before the bullet reaches the player the player dies because I'm destroying the player with the raycast? Any suggestions?
At the moment my Ai shoots the player if I click the mouse something I need to get rid of I need the Ai to shoot independently? Any Suggestions?
public class Weapon : MonoBehaviour {
public float fireRate = 0;
public float Damage = 10;
public LayerMask whatToHit;
public GameObject rabbit;
float timeToFire = 0;
Transform firePoint;
// Use this for initialization
void Awake () {
rabbit = GameObject.FindWithTag("Player");
firePoint = transform.FindChild ("FirePoint");
if (firePoint == null) {
Debug.LogError("No FirePoint");
}
}
void Update () {
if (Input.GetMouseButtonDown (0)) {
if (fireRate == 0) {
Shoot ();
} else {
return;
}
}
}
void Shoot(){
Vector2 rabbitPosition = new Vector2 (rabbit.transform.position.x, rabbit.transform.position.y);
Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
RaycastHit2D hit = Physics2D.Raycast (firePointPosition, rabbitPosition - firePointPosition, 100, whatToHit);
Debug.DrawLine (firePointPosition, (rabbitPosition - firePointPosition) * 100, Color.cyan);
if (hit.collider != null) {
Debug.DrawLine (firePointPosition, hit.point, Color.red);
}
}
if you want your AI to shoot independently, create a timer so it shoots once every second or something like that? was that your question ?
Hi $$anonymous$$elkorinos yes that is a nice Idea I will give that a go thanks. Can I ask toy one more small question.
I have applied an effect to the raycast so it makes a sort of bullet trail.
I can only get it to go to the right have you any ideas? How can I get it to fire in the direction the raycast is pointing?
using UnityEngine;
using System.Collections;
public class $$anonymous$$oveTrail : $$anonymous$$onoBehaviour {
public GameObject rabbit;
public int moveSpeed = 230;
void Awake(){
rabbit = GameObject.FindWithTag("Player");
}
// Update is called once per frame
void Update () {
float rabbitPosition = rabbit.transform.position.x; // I want to shoot the bullets at the player
transform.Translate (Vector3.right * Time.deltaTime * moveSpeed);
Destroy (gameObject, 1);
}
}
the best way to send something towards a direction is to do Vector3 dir = (mytransform.positon - target.position) that vector 3 should be used ins$$anonymous$$d of Vecotr3.right
Answer by barbe63 · May 11, 2015 at 12:16 PM
Hello, you should make a gameobject bullet with a rigidbody and a collider and in a script attached on it, you can set somthing like:
void OnCollisionEnter(collision other)
{
//make a way to recognize it's the player, that can also be setting your bullet to only collide the player with layers. Here I use the tag("Player")
if(other.CompareTag("Player")
{
//your function to kill or damage the player
}
}
Then when you fire the bullet you want to call something like this
//attach this script to the enemy or preferably the gun.
//...
public GameObject bulletPrefab;
//...
void FireBullet()
{
//before you need to assign the bulletPrefab value to your prefabed bullet in the inspector
GameObject bullet = GameObject.Instantiate(bulletPrefab, transform.position + transform.forward, transform.rotation) as GameObject;
float speed = 1000; // you might need to adjust this value for the desired speed
bullet.GetComponent<Rigidbody>().AddForce(bullet.transform.forward*speed);
}
Keep also in mind that you should actually use pooling instead of instatiate your bullets. There are some tutorials about that. Watch this one: https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/object-pooling
Your answer
Follow this Question
Related Questions
Finding RayCastHit's Origin Position 2 Answers
2D Sending Object From One Place To Mouse Position. 1 Answer
Implementing BUG2 algorithm 0 Answers
AI Field of vision 1 Answer
Another Raycast Issue. 0 Answers