So I borrowed this basic scripts from a website and it’s used as the basic function of a bullet. This works absolutely fine.
using UnityEngine;
using System.Collections;
public class ShotScript : MonoBehaviour {
public int damage = 1;
public bool isEnemyShot = false;
void Start () {
Destroy (gameObject, 50);
}
}
However this next script is used for a different kind of bullet. This script does not work when used.
using UnityEngine;
using System.Collections;
public class RayShotScript : MonoBehaviour {
public int damage = 1;
public bool isEnemyShot = false;
void Start () {
Destroy (gameObject, 200);
}
}
I am not very knowledgeable in C# and have mostly been using Scripts I find on the web. If anyone can please explain to me why the second code doesn’t work and how I can make it work that would be great!
if the last script is supposed to kill enemies using raycast this is how it should be done: (sorry this is about all I can provide of help - you’d have to be more precise otherwise…) also note that is hasn’t been tested - might be a few typos.
public float rawLength = 100.0f;
void Update () {
if (Input.GetMouseButton(0 /* left mouse button */)) {
RaycastHit hit;
Ray screenCenteredRay = Camera.main.ScreenPointToRay(Input.mousePosition);
bool isHit = Physics.RayCast (screenCenteredRay, rayLength, &hit);
if (isHit) {
Destroy (hit.gameObject); // < ---- This is not a good idea ;p
}
}
}
Both scripts are identical in function. They are both designed to be attached to the projectile. Both destroy the projectile after a certain amount of time. 50 seconds for the first, 200 seconds for the second.
There are a lot of other things required to make a functioning gun. By itself the script will not:
Fire the weapon
Create the projectile
Aim the projectile
Move the projectile
Detect collision
Do damage
There are plenty of tutorials to be found on google that will explain how to do all of the above.