- Home /
Question by
magnusOP · Jul 26, 2013 at 05:59 PM ·
javascriptraycastweapon
Raycast firerate shooting
i have a raycast that shoots too fast i need to incorporate a firerate into it. i am a noob and i have spent an hour trying to fuse them together. if anyone could fuse them and help me out i would really appreciate it.
#pragma strict
var Effect : Transform;
var TheDammage = 100;
function Update () {
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
if (Input.GetMouseButton(0))
{
if (Physics.Raycast (ray, hit, 100))
{
var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(particleClone.gameObject, 2);
hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
}
}
}
I NEED TO HAVE THIS INCORPORATED INTO IT
var timeSinceLastShot : float = 0f;
var timeBetweenShots : float = 1 / (shots per second);
void Update() {
timeSinceLastShot += Time.deltaTime;
if(timeSinceLastShot >= timeBetweenShots) {
shoot();
timeSinceLastShot = 0f;
}
}
Comment
Best Answer
Answer by robertbu · Jul 26, 2013 at 06:54 PM
A bit different than the code you asked to merge, but I'd do this way:
#pragma strict
var Effect : Transform;
var TheDammage = 100;
var shotsPerSecond = 8.0;
private var timestamp = 0.0;
function Update () {
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
if (timestamp <= Time.time && Input.GetMouseButton(0))
{
if (Physics.Raycast (ray, hit, 100))
{
var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(particleClone.gameObject, 2);
hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
timestamp = Time.time + 1.0 / shotsPerSecond;
}
}
}
Your answer
