- Home /
How can i modify this script to add vertical and also horizontal camera recoil?
using System; using UnityEngine;
public class Gun : MonoBehaviour {
public float damage = 10f;
public float range = 100f;
public float fireRate = 15f;
public float impactForce = 30f;
public Camera fpsCam;
public ParticleSystem muzzleFlash;
public GameObject impactEffect;
private float nextTimeToFire = 0f;
public static Action EventPlayerInput { get; internal set; }
// Update is called once per frame
void Update () {
//animate recoil
if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f / fireRate;
Shoot();
}
}
public class weaponinf
{
}
void Shoot ()
{
muzzleFlash.Play();
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit)) { }
{
Target target = hit.transform.GetComponent<Target>();
if (target !=null)
{
target.TakeDamage(damage);
}
if (hit.rigidbody !=null)
{
hit.rigidbody.AddForce(-hit.normal * impactForce);
}
GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactGO, 2f);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Proper Recoil for gun? 1 Answer
Weapon recoil recoils in world space 1 Answer
Gun Recoil Help 0 Answers
Using recoil on a Gun? 1 Answer