본문 바로가기
VR 팀프로젝트/[GroundZero] 제작 일지

GroundZero 제작일지 - 바주카포 총 수정

by 잰쟁 2024. 1. 3.
728x90

 

▼ 수정 목표

- 바주카포 총알이 휘지 않고 더 자연스럽게 나가도록 수정하기!!!


 

바주카포 총을 이동하는 맵에 적용시켜보니 아주 이상하게 총알이 나가는 것을 확인했다..

총알이 기존에 처럼 직선으로 바로 나가지 않고 휘어서(?) 나갔다..

 

▼ 이동맵에 적용시킨 바주카포 총 (이상하게 나가는 장면) 

 

 

맵이 움직여서 공기 등의 저항을 받는 것이 원인이라는 것 까지는 파악을 하였으나,

Rigidbody의  Mass, Drag, Angular Drag 값들을 아무리 만져봐도 별로 상황이 나아지지 않았다ㅜㅡㅜ


 

고민한 끝에 기존 'MissileBullet' 스크립트에 총알을 나가게 했던 AddForce 부분을 수정해보기로 하였다.

여러 ForceMode 중에 질량을 무시하고 힘을 가하는 VelocityChange를 적용시켜 보았다.

 

 

 

**변경부분 : ForceMode.Impulse(기존) -->  ForceMode.VelocityChange(수정)

void FixedUpdate()
{
    this.rb.AddForce(this.transform.forward * this.moveSpeed, ForceMode.VelocityChange);
}

 

 

그리고 총이 일정 거리 이상 날아가면 자동으로 없어지게 하기 위해 아래와 같이 PlayerCar에 'DestoryTrans' 트랜스폼을 붙여주고 스크립트 내용을 추가하였다.

 

 

이렇게만 수정을 하였더니 이상하게 휘어서 날아가는 현상은 어느정도 해결이 되었으나 총알의 속도가 너무 빨라져 몬스터도 빠르게 튕겨나가 졌다..

힘없이 튕겨나가지는 적들,..

 

 

따라서 아래와 같이 총알의 설정 값들을 바꿔주고

(수정전) 총알 설정                                                                                  (수정후) 총알 설정

 

 

몬스터의 Mass 값도 올려주었다.

Enemy의 Mass 늘려주기

 

 


▼ 수정한 MissileBullet 스크립트

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

namespace LJEMain 
{
    public class MissileBullet : MonoBehaviour
    {
        private Transform shootDistance;
        private Rigidbody rb;
        public float moveSpeed = 5f;

        // public System.Action OnHitEnemy;

        void Start()
        {
            this.rb = this.GetComponent<Rigidbody>();
            this.shootDistance = GameObject.Find("ShootDistance").transform;
            this.transform.LookAt(this.shootDistance.position);
        }

        void FixedUpdate()
        {
            this.rb.AddForce(this.transform.forward * this.moveSpeed, ForceMode.VelocityChange);
            if(this.transform.position.z > GameObject.Find("DestroyTrans").transform.position.z) 
            {
                Destroy(this.gameObject);
            }
        }



        private void OnCollisionEnter(Collision collision)
        {
            Debug.LogFormat("<color=red>collision: {0}</color>", collision.gameObject.name);
            if (collision.collider.CompareTag("Gun") && collision.collider.CompareTag("MissileBullet")) return;
            if (collision.collider.CompareTag("Enemy1") || collision.collider.CompareTag("Enemy2") || collision.collider.CompareTag("Enemy3"))
            {
                Debug.Log("<color=lime>onHitEnemy</color>");
                Destroy(this.gameObject);
                var rightHand = this.gameObject.GetComponentInParent<MissileBulletGenerator>().missileGun;
                rightHand.OnHitEnemy(collision.contacts[0].point, collision.gameObject);
            }
        }
    }
}

▼ 수정 후 바주카포 총

 

 

맞아도 Enemy가 튕겨나가지 않고, 총알도 기존보다는 총구 방향대로 총알이 잘 날아간다!!

(하지만 아직까지 어색하긴 함..)


 ** 빌드한 화면에 log 보여주기

 

- 작업 도중에 Quest Link 오류가 생겨 번거롭지만 일일히 Build를 해가면서 작업을 했다.

- 하지만 log를 볼 수가 없어서 수업 시간에 배운 내용을 활용하여 Canvas를 Worldspace로 놓고 스크롤하여 log를 볼 수 있게 해주었다. 

 

 

 

using System.Collections;
using System.Collections.Generic;
using System.Text;
using TMPro;
using UnityEngine;
using UnityEngine.UI;


public class scroll : MonoBehaviour
{
    [SerializeField] private TMP_Text debugText;
    [SerializeField] private ScrollRect scrollRect;

    private float normalizedPositionY = 1;

    public StringBuilder sb = new StringBuilder();
    void OnEnable()
    {
        Debug.Log("OnEnable");
        Application.logMessageReceived += HandleLog;
    }

    private void Start()
    {
        this.scrollRect.normalizedPosition = new Vector2(0, 1);
    }

    void OnDisable()
    {
        Debug.Log("OnDisable");
        Application.logMessageReceived -= HandleLog;
    }

    void HandleLog(string logString, string stackTrace, LogType type)
    {
        sb.Append(logString);
        sb.AppendLine();
        debugText.text = sb.ToString();
    }

    void Update()
    {
        Vector2 thumbstickValue =
            OVRInput.Get(OVRInput.Axis2D.PrimaryThumbstick, OVRInput.Controller.RTouch);

        //Debug.Log(thumbstickValue.y);   //-1 ~ 1

        if (thumbstickValue.y > 0)  //up
        {
            this.MoveUp();
        }
        else if (thumbstickValue.y < 0)    //down
        {
            this.MoveDown();
        }

        //test
#if UNITY_EDITOR
        if (Input.mouseScrollDelta.y > 0)  //up
        {
            this.MoveUp();
        }
        else if (Input.mouseScrollDelta.y < 0)    //down
        {
            this.MoveDown();
        }
#endif
    }
    private void MoveUp()
    {
        //전체 중에 1%씩 움직이고 싶음 
        normalizedPositionY += 0.01f;
        if (normalizedPositionY >= 1) normalizedPositionY = 1;
        //Debug.Log("up");
        this.scrollRect.normalizedPosition = new Vector2(0, normalizedPositionY); //y 값이 1이면 젤 위, 0이면 젤아래
    }

    private void MoveDown()
    {
        normalizedPositionY -= 0.01f;
        if (normalizedPositionY <= 0) normalizedPositionY = 0;
        //Debug.Log("down");
        this.scrollRect.normalizedPosition = new Vector2(0, normalizedPositionY); //y 값이 1이면 젤 위, 0이면 젤아래
    }
}

 

log를 볼 수 있다!!