- Home /
Using Raycast shooting while using a ps4 controller?
hi guys, i was making a small game and i have been trying to get my weapon to shoot using raycast much like in the survival game tutorial,the issue is that i am not using my mouse as a controller but a PS4 controller.
public class GunController : MonoBehaviour {
public bool isFiring;
public int playerNum;
public int GunDamage;
public string fire;
//public BulletController bullet;
public Transform firePoint;
public float range;
public float timeBetweenShots;
public float timer;
Ray shootRay;
RaycastHit shootHit;
public ParticleSystem gunParticles;
public LineRenderer gunLine;
public AudioSource gunAudio;
public Light gunLight;
float effectsDisplayTime = 0.2f;
int shootableMask;
private float shotCounter;
void awake(){
shootableMask = LayerMask.GetMask("Shootable");
}
// Use this for initialization
void Start () {
//bullet = GetComponent<BulletController> ();
}
// Update is called once per frame
void Update () {
timer += Time.deltaTime;
if (Input.GetButton (fire) && timer >= timeBetweenShots) {
shoot ();
}
if (timer >= timeBetweenShots * effectsDisplayTime) {
DisableEffects ();
}
/*
if (isFiring) {
shotCounter -= Time.deltaTime;
if (shotCounter <= 0) {
shotCounter = timeBetweenShots;
//bullet.playerNum = playerNum;
//BulletController newBullet = Instantiate (bullet, firePoint.position, firePoint.rotation) as BulletController;
//newBullet.speed = bulletSpeed;
}
} else {
shotCounter = 0;
}
*/
}
public void DisableEffects(){
gunLine.enabled = false;
gunLight.enabled = false;
}
void shoot(){
timer = 0f;
gunAudio.Play ();
gunLight.enabled = true;
gunParticles.Stop ();
gunParticles.Play ();
gunLine.enabled = true;
gunLine.SetPosition (0, transform.position);
shootRay.origin = transform.position;
shootRay.direction = transform.forward;
if (Physics.Raycast (shootRay, out shootHit, range, shootableMask)) {
EnemyHealth enemyHealth = shootHit.collider.GetComponent<EnemyHealth> ();
if (enemyHealth != null) {
enemyHealth.TakeDamage (GunDamage, shootHit.point);
enemyHealth.PATK = playerNum;
}
gunLine.SetPosition (1, shootHit.point);
} else {
gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
}
}
}
yeah i know it is pretty long but that is the code in my gun, it shoots as intended but it does not recognize any enemies nor walls and the shot just goes right through the wall and enemies. i have all my enemies and walls using the shootable layer but it keeps telling me "shootray" is not set. anyone can help. thank you
Comment
Your answer
Follow this Question
Related Questions
Shooting problem Unity3D C# 0 Answers
Shoot only swipe 0 Answers
Flickering Rendering off Camera 0 Answers
i want to add fire shot at enemy gun explosion 1 Answer
GetMouseButton Not Working 1 Answer