Quantcast
Channel: Questions in topic: "invokerepeating"
Viewing all articles
Browse latest Browse all 220

Raycast ignoring InvokeRepeating intervals

$
0
0
Hi, so i posted a question to the default answers page but it's been under moderation for 2 days now so was advised to post my question here instead. I'm fairly new to Unity and coding so bear with me but essentially I have 2 separate codes; the first is for the player to shoot when i hold down the "Fire1" key which works perfectly, and then there's this one which doesn't work so nicely... I'm trying to add a raycast to my object so that whenever the player is in line of sight it invokes the "shooting" function, the actual raycast works but I believe that raycasts are once every tick so the code is getting invoked then cancelled repetitively causing it to shoot every frame instead of at the determined interval. should i add a interval between the raycasts using boolean's so that it isn't every frame but instead at a determined interval? or do i need to change it from Update? using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyShootingScript : MonoBehaviour { public GameObject BulletEmitter; public GameObject Bullet; public float BulletForwardForce; public float FireRateSeconds; public GameObject Enemy; private float rayDirection; public float RayOffset; void Raycast () { RaycastHit hit; if (Physics.Raycast (transform.position, transform.forward * RayOffset, out hit)) { Debug.DrawRay(transform.position, transform.forward * RayOffset, Color.green, 2); //print ("Something in line of sight"); print (hit.collider.gameObject.name); if (hit.collider.gameObject.name == "Player") { //print ("Player in line of sight"); InvokeRepeating ("Shooting", .001f, FireRateSeconds); } else { CancelInvoke ("Shooting"); } } } void Shooting () { GameObject TemporaryBulletHandler; TemporaryBulletHandler = Instantiate (Bullet, BulletEmitter.transform.position, BulletEmitter.transform.rotation) as GameObject; TemporaryBulletHandler.transform.Rotate(Vector3.forward); Rigidbody TemporaryRigidbody; TemporaryRigidbody = TemporaryBulletHandler.GetComponent (); TemporaryRigidbody.AddForce (transform.forward * BulletForwardForce); Destroy (TemporaryBulletHandler, 3.0f); } } Thanks in advance :) Pen

Viewing all articles
Browse latest Browse all 220

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>