Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c sharp gun shooting

using UnityEngine;

public class Gun : MonoBehaviour
{
    public float damage = 10f;
    public float range = 100f;
    public float impactForce = 30f;

    public Camera fpsCam;


    // Update is called once per frame
    void Update()
    {
        if (Input.GetButtonDown("Fire1")) {

            Shoot();
        }
    }

    void Shoot () {

        RaycastHit hit;
        if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range)) {

            Target target = hit.transform.GetComponent<Target>();

            if (hit.rigidbody != null) {

                hit.rigidbody.AddForce(-hit.normal * impactForce);
            
            }

            if (target != null) {

                target.TakeDamage(damage);
            
            }
        
        }  
    }
}


health


using UnityEngine;

public class Gun : MonoBehaviour
{
    public float damage = 10f;
    public float range = 100f;
    public float impactForce = 30f;

    public Camera fpsCam;


    // Update is called once per frame
    void Update()
    {
        if (Input.GetButtonDown("Fire1")) {

            Shoot();
        }
    }

    void Shoot () {

        RaycastHit hit;
        if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range)) {

            Target target = hit.transform.GetComponent<Target>();

            if (hit.rigidbody != null) {

                hit.rigidbody.AddForce(-hit.normal * impactForce);
            
            }

            if (target != null) {

                target.TakeDamage(damage);
            
            }
        
        }  
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# application hangs while running 
Csharp :: how to get executable path in wpf 
Csharp :: how to check if a number is even in c# 
Csharp :: unity ui change sprite 
Csharp :: unity gameobject.findobjectswith tag set active 
Csharp :: unity c# set list to set active true 
Csharp :: string to list c# 
Csharp :: mvc select list order by 
Csharp :: check if ienumerable is empty c# 
Csharp :: currentTimeMillis c# 
Csharp :: add dynamically buttons in loop with events winform c# 
Csharp :: c# write text before user input 
Csharp :: c# conver date utc to cst 
Csharp :: nepali phone number regex 
Csharp :: snx disconnect linux 
Csharp :: make mesh follow wheel collider unity 
Csharp :: system.linq.iorderedenumerable`2[system.char,system.char] çözümü 
Csharp :: remove first object from list c# 
Csharp :: how to generate random letters in C# 
Csharp :: struct constructor c# 
Csharp :: c# ascii to char 
Csharp :: ++ operator c# 
Csharp :: c# read from text documenmt 
Csharp :: rotation facing mouse unity 
Csharp :: convert int to uint c# 
Csharp :: readonly vs const c# 
Csharp :: c# retrieve files in folder 
Csharp :: c# encrypt decrypt string 
Csharp :: perlin noise unity 
Csharp :: wpf get screen size 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =