using System;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    [SerializeField]
    private float moveSpeed = 1f;
    [SerializeField]
    private float turnSpeed = 150f;
    [SerializeField]
    private Transform mainCamera; 

    private Animation anim;
    private Transform tr;

    public enum eAnimState
    {
        Idle,
        RunB,
        RunF,
        RunL,
        RunR
    }

    private void Start()
    {
        this.tr = this.GetComponent<Transform>();
        this.anim = this.GetComponent<Animation>();

        this.anim.clip = this.anim.GetClip(eAnimState.Idle.ToString());
        this.anim.Play();

        if (mainCamera == null)
        {
            mainCamera = Camera.main.transform;
        }


        Cursor.visible = false;
        Cursor.lockState = CursorLockMode.Locked;
    }


    private void Update()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        float mouseX = Input.GetAxis("Mouse X");

        var dir = new Vector3(h, 0, v);

        Vector3 moveDir = (Vector3.forward * v) + (Vector3.right * h);
        this.transform.Translate(moveDir.normalized * this.moveSpeed * Time.deltaTime);


        tr.Rotate(Vector3.up * mouseX * turnSpeed * Time.deltaTime);

        this.PlayAnimation(dir);


        if (mainCamera)
        {
            mainCamera.LookAt(tr.position);
        }
    }

    private void PlayAnimation(Vector3 dir)
    {
        if (dir.x > 0)
        {    //right
            this.anim.CrossFade(eAnimState.RunR.ToString(), 0.25f);
        }
        else if (dir.x < 0)
        {   //left
            this.anim.CrossFade(eAnimState.RunL.ToString(), 0.25f);
        }
        else if (dir.z > 0)
        {
            //forward
            this.anim.CrossFade(eAnimState.RunF.ToString(), 0.25f);
        }
        else if (dir.z < 0)
        {
            //back
            this.anim.CrossFade(eAnimState.RunB.ToString(), 0.25f);
        }
        else
        {
            //idle 
            this.anim.CrossFade(eAnimState.Idle.ToString(), 0.25f);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FireController : MonoBehaviour
{
    public GameObject bullet;
    public Transform firePos;

    [SerializeField] private float radius = 1f;
    private Collider[] barrels = new Collider[10];
    private Collider nearestBarrel = null;
    public GameObject player;


    void Update()
    {
        FindNearestBarrel();

        if (Input.GetMouseButtonDown(0))
        {
            if (nearestBarrel != null)
            {
                LookAtNearestBarrel();
                Fire();
            }
        }
        
    }

    void FindNearestBarrel()
    {
        int layerMask = 1 << LayerMask.NameToLayer("BARREL");
        int cnt = Physics.OverlapSphereNonAlloc(this.transform.position, this.radius, barrels, layerMask);

        float minDistance = float.MaxValue;

        foreach (Collider col in barrels)
        {
            if (col != null)  // 중요: null 체크
            {
                float distance = Vector3.Distance(this.transform.position, col.transform.position);
                if (distance < minDistance)
                {
                    nearestBarrel = col;
                    minDistance = distance;
                }
            }
        }
    }

    void LookAtNearestBarrel()
    {
        Vector3 directionToBarrel = nearestBarrel.transform.position - transform.position;
        Quaternion lookRotation = Quaternion.LookRotation(directionToBarrel);

        player.transform.rotation = lookRotation;

        firePos.rotation = lookRotation;
    }
    void Fire()
    {
        Instantiate(bullet, firePos.position, firePos.rotation);
    }

    private void OnDrawGizmos()
    {
        Gizmos.color = Color.white;
        Gizmos.DrawWireSphere(this.transform.position, this.radius);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BarrelController : MonoBehaviour
{

    private int hitCount = 0;
    public GameObject expEffect;

    private Transform tr;
    private Rigidbody rb;
    [SerializeField] float radius = 1f;
    public Texture[] textures;

    private new MeshRenderer renderer;
    // Start is called before the first frame update
    void Start()
    {
        tr = GetComponent<Transform>();
        rb = GetComponent<Rigidbody>();

        renderer = GetComponentInChildren<MeshRenderer>();
        int idx = Random.Range(0, textures.Length);

        renderer.material.mainTexture = textures[idx];
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.collider.CompareTag("BULLET"))
        {
            hitCount++;
            if(hitCount == 3)
            {
                GameObject exp = Instantiate(expEffect,tr.position,Quaternion.identity);
                Destroy(exp, 2f);
             
                Destroy(gameObject);
            }
        }
    }
    void ExpBarrel()
    {
       
        GameObject exp = Instantiate(this.expEffect, this.transform.position, Quaternion.identity);
        Destroy(exp, 0.5f);

        this.rb.mass = 1f;
        this.rb.AddForce(Vector2.up * 1500f);

        //간접 폭발력 전달 
        this.IndirectDamage(this.transform.position);

        //3초후 드럼통 제거 
        Destroy(this.gameObject, 3.0f);
    }
    private void IndirectDamage(Vector3 position)
    {
        Collider[] colls = Physics.OverlapSphere(position, this.radius, 1 << 3);
        foreach(Collider col in colls)
        {
            var rb = col.GetComponent<Rigidbody>();
            rb.mass = 1.0f;
            rb.constraints = RigidbodyConstraints.None;
            rb.AddExplosionForce(1500.0f, position, radius, 1200.0f);
        }
    }
  
    private void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(this.transform.position, this.radius);
    }

}

반응형

+ Recent posts