[Cyphers] 클레어 평타 수정(particleSystem Renderer LengthScale)
2023. 10. 4.

클레어의 평타가 물체에 충돌시, 충돌한 지점까지만 이펙트가 보이도록 수정하였다.

https://docs.unity3d.com/ScriptReference/ParticleSystemRenderer-lengthScale.html

 

Unity - Scripting API: ParticleSystemRenderer.lengthScale

This determines the base length of particles when they don't move. A value of 1 is neutral, causing no stretching or squashing. Use this with a value greater than 1 to make particles always be longer than they are wide.

docs.unity3d.com

파티클 시스템의 Renderer의 lengthScale을 코드로 제어하도록 수정하였다.

particleSystem의 renderer의 lengthScale을 코드로 제어한 결과

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class ClareController : MonoBehaviour
{
    [SerializeField] MouseController mouse;
    [SerializeField] private Transform clareHandTrans;
    [SerializeField] private GameObject beamGo;
    [SerializeField] private GameObject beamImpactGo;
    [SerializeField] private LineRenderer lr;
    [SerializeField] private Transform LCPos;
    [SerializeField] private Transform crossHair;
    private GameObject newBeamGo;
    private Vector3 moveDir;
    private Animator anim;
    private Ray ray2;
    private RaycastHit hit;
    private ParticleSystemRenderer psr;
    // Start is called before the first frame update
    void Start()
    {
       
        this.anim = GetComponent<Animator>();
       
        this.newBeamGo = Instantiate<GameObject>(beamGo);
        this.beamImpactGo = Instantiate<GameObject>(beamImpactGo);
        this.psr = this.newBeamGo.GetComponent<ParticleSystemRenderer>();
        // this.lr = beamGo.GetComponent<LineRenderer>();

        this.newBeamGo.SetActive(false);
        this.beamImpactGo.SetActive(false);
        lr.enabled = false;
       
        //  lr.positionCount = 0;
    }

    // Update is called once per frame
    void Update()
    {
        
        if(moveDir != Vector3.zero)
        {
            if(this.moveDir.z > 0)//forward
            {
                //this.transform.rotation = Quaternion.LookRotation(moveDir); ;//진행방향으로 회전
                this.transform.Translate(Vector3.forward * 4.0f * Time.deltaTime);//이동
                if(this.moveDir.x == 0)//forward
                {
                    this.anim.SetInteger("State", 1);//move forward
                }
                else if (this.moveDir.x > 0)
                {
                    this.anim.SetInteger("State", 2);//move forward rightCross
                }
                else if (this.moveDir.x < 0)
                {
                    this.anim.SetInteger("State", 4);//move forward leftCross
                }
            }
            else if(this.moveDir.z < 0)//backward
            {
                this.transform.Translate(this.moveDir * 4.0f * Time.deltaTime);//이동
                if (this.moveDir.x == 0)//backward
                {
                    this.anim.SetInteger("State", 3);//move backward
                }
                else if (this.moveDir.x > 0)
                {
                    this.anim.SetInteger("State", 7);//move backward rightCross
                }
                else if (this.moveDir.x < 0)
                {
                    this.anim.SetInteger("State", 8);//move backward leftCross
                }
            }
            else
            {//stay but press left or right
                this.transform.Translate(this.moveDir * 4.0f * Time.deltaTime);//이동
                if (this.moveDir.x < 0)
                {
                    this.anim.SetInteger("State", 5);//move left
                }
                else
                {
                    this.anim.SetInteger("State", 6);//move right
                }
            }
        }
        else
        {
            this.anim.SetInteger("State", 0); //idle
        }
    }
    private IEnumerator CoLCBeam()
    {
        this.newBeamGo.SetActive(true);
        // this.newBeamGo.transform.position = this.ray2.origin;
        this.newBeamGo.transform.position = this.clareHandTrans.position;
        
        if (mouse.hit.point != Vector3.zero && mouse.hit.collider.tag != "Player")
        {
            this.newBeamGo.transform.LookAt(mouse.hit.point);
        }
        else
        {
            this.newBeamGo.transform.LookAt(this.LCPos);
        }
        yield return new WaitForSeconds(0.3f);
        this.newBeamGo.SetActive(false);
    }

    private IEnumerator CoLCBeamImpact(float distance)
    {
        this.newBeamGo.SetActive(true);
        this.psr.lengthScale = distance;
        this.newBeamGo.transform.position = this.clareHandTrans.position;

        if (mouse.hit.point != Vector3.zero && mouse.hit.collider.tag != "Player")
        {
            this.newBeamGo.transform.LookAt(mouse.hit.point);
        }
        else
        {
            this.newBeamGo.transform.LookAt(this.LCPos);
        }    
        yield return new WaitForSeconds(0.3f);
        this.newBeamGo.SetActive(false);
    }
    private IEnumerator CoBeamImpact()
    {
        this.beamImpactGo.SetActive(true);
        this.beamImpactGo.transform.position = this.hit.point;
        yield return new WaitForSeconds(0.3f);
        this.beamImpactGo.SetActive(false);
    }
    private IEnumerator CoLCLineRenderer()
    {
        
        lr.enabled = true;
        lr.SetPosition(0, clareHandTrans.position);
        Vector3 LCPos = new Vector3(this.LCPos.position.x, this.LCPos.position.y, this.LCPos.position.z);
        Debug.Log(this.transform.forward);       
        lr.SetPosition(1, LCPos);
        yield return new WaitForSeconds(0.3f);
        lr.enabled = false;

    }
    public void OnMove(InputAction.CallbackContext ctx)
    {
        Vector2 dir = ctx.ReadValue<Vector2>();
        this.moveDir = new Vector3(dir.x, 0,dir.y);//2차원 좌표를 3차원 좌표로 변환
    }

    public void OnLC(InputAction.CallbackContext ctx)
    {
      
        Vector3 ray2Direction;
      
        if (mouse.hit.point != Vector3.zero )
        {
            ray2Direction = mouse.hit.point- this.clareHandTrans.position;
        }
        else
        {
            ray2Direction = LCPos.position - this.clareHandTrans.position;
        }
        this.ray2 = new Ray(this.clareHandTrans.position, ray2Direction);       
        float lcDistance = 5f;

        if (Physics.Raycast(this.ray2.origin, this.ray2.direction, out hit, lcDistance) && !hit.collider.CompareTag("Player"))
        {
            // Debug.LogFormat("LC Hit! Distance : {0}", hit.distance);
            Debug.DrawRay(this.ray2.origin, this.ray2.direction * lcDistance, Color.yellow, 0.5f);
            StartCoroutine(this.CoLCBeamImpact(hit.distance+2f));
            StartCoroutine(this.CoBeamImpact());
        }
        else
        {
            StartCoroutine(this.CoLCBeam());
        }
        
        // StartCoroutine(this.CoLCLineRenderer());
    }
}
myoskin