[TIL] 50 AddForce player.SetParent โญโญ

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , ,


Addforce, player.SetParent



2์ผ์ฐจ

ย ย  [o] ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ - 51
ย ย  [o] ๋‹ค๋ฅธ๋ฐ˜ ๊ฐ•์˜ ๋“ฃ๊ธฐ
ย ย  [x] ์‹ฌํ™”์ฃผ์ฐจ ๊ฐ•์˜ ๋“ฃ๊ธฐ.







์ ํ”„๋Œ€ ๋งŒ๋“ค๊ธฐ

2. Rotation,Addforce

    [SerializeField]
    private float _jumpforce = 30f;
    private float _rotateSpeed = 0.5f;
    private Vector3 _stopRotate = new Vector3(60, 0, 0);
    private Vector3 _startRotate = new Vector3(0, 0, 0);
    private bool _checkRotate = true;
    private bool _collidertime = true;
    private bool _onPad = false;

    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Player" && _checkRotate && _collidertime)
        {
            StartCoroutine(RotatePad(collision));
            _collidertime = false;
            _onPad = true;
        }
    }
    private void OnTriggerExit(Collider other)
    {
        _onPad = false;
    }

    IEnumerator RotatePad(Collision collision)
    {
        PlayerJump(collision, Vector3.left);

        yield return new WaitForSeconds(0.1f);
        while (_checkRotate)
        {
            _rotateSpeed += 0.01f;
            transform.Rotate(Vector2.right * _rotateSpeed);
            yield return null;
            if (_stopRotate.x - transform.eulerAngles.x <= 10f)
            {

                Vector3 forwardDirection = transform.up;
                
                //Rigidbody Addforce ์—์„œ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ณ€๊ฒฝ (y๋Š” ํž˜์„ ๋ฐ›๋Š”๋ฐ z ํž˜์„ ๋ชป๋ฐ›์Œ)
                Rigidbody otherRigidbody = collision.gameObject.GetComponent<Rigidbody>();
                if (otherRigidbody != null&& _onPad)
                {
                    Debug.Log("addforce ๋ฐฉํ–ฅ : " + forwardDirection * _jumpforce);
                    otherRigidbody.AddForce(forwardDirection * _jumpforce, ForceMode.Impulse);
                }

                transform.rotation = Quaternion.Euler(_stopRotate);
                _checkRotate = false;
            }
        }

        yield return new WaitForSeconds(2f);

        while (!_checkRotate)
        {
            transform.Rotate(Vector2.left);
            yield return null;

            if (transform.eulerAngles.x - _startRotate.x <= 1.1f)
            {
                transform.rotation = Quaternion.Euler(_startRotate);
                _checkRotate = true;
            }
        }
        _collidertime = true;
        _rotateSpeed = 1f;
    }
}



addforce ๊ฐ€ y์ถ•์€ ๋˜๋Š”๋ฐ z์ถ•์ด ์ด์ƒํ•˜๊ฒŒ ๋œ๋‹ค. ๋‚ด์ผ ์ƒ๊ฐํ•ด๋ณด๊ธฐ.

image







setParent

ํ”Œ๋ ˆ์ด์–ด ๋ถ€๋ชจset,null ํŒ€์›๋ถ„ ์ฝ”๋“œ ๋‹ค์Œ์— ์ฐธ๊ณ ํ•  ๋•Œ ๋ณด๊ธฐ.

SavePoint


using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SavePoint : MonoBehaviour
{
    private Vector3 _startPoint; // ์‹œ์ž‘์œ„์น˜ ์„ค์ •.
    private Vector3 _firstStartPoint = new Vector3(0,50,50); // 1์Šคํ…Œ์ด์ง€ ์‹œ์ž‘์œ„์น˜ ์„ค์ •.
    private Vector3 _SecondStartPoint = new Vector3(94, 0, 15); // 2์Šคํ…Œ์ด์ง€ ์‹œ์ž‘์œ„์น˜ ์„ค์ •.

    private Vector3 _savePoint = Vector3.zero;  // ์ €์žฅ์œ„์น˜ ์„ค์ •.

    private HealthSystem HealthSystem;
    private PlayerRagdollController _playerRagdollController;

    private void Awake()
    {
        _playerRagdollController = GetComponent<PlayerRagdollController>();
        HealthSystem = GetComponent<HealthSystem>();
    }
    private void Start()
    {
        Scene scene = SceneManager.GetActiveScene();
        sceneCheck(scene);
        _savePoint = _startPoint;
        HealthSystem.OnDied += revive;
        //SceneManager.sceneLoaded += LoadedsceneEvent;
    }

    public void revive() 
    {
        StartCoroutine(ReStartCo());
    }
    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.R)) 
        {
            StartCoroutine(ReStartCo());
        }
        checkSaveBoard();
        Debug.DrawRay(transform.position, Vector3.down, Color.red, 0.3f);
    }

    /*private void LoadedsceneEvent(Scene scene, LoadSceneMode arg1)
    {
        _player = GameObject.FindGameObjectWithTag("Player");
        sceneCheck(scene);
        _savePoint = _startPoint;
    }*/

    private void checkSaveBoard() 
    {
        RaycastHit _hit;

        if (Physics.Raycast(transform.position, Vector3.down, out _hit, 1,4))
        {
            if (_hit.transform.CompareTag("SaveBoard"))
            {
                Debug.Log("์„ธ์ด๋ธŒ๋ณด๋“œ");
                _savePoint = _hit.transform.position + Vector3.up;
                //์„ธ์ด๋ธŒ ์ดํŽ™ํŠธ
                Destroy(_hit.collider.gameObject, 1f);
            }
        }
    }
    
    public IEnumerator ReStartCo()
    {
        _playerRagdollController.SetRagdollState(true);
        yield return new WaitForSeconds(5.1f);
        gameObject.transform.position = _savePoint;
        _savePoint = _startPoint;
    }

    private void sceneCheck(Scene scene) 
    {
        if (scene.name == "KDH_Obstacle")
        {
            _startPoint = _firstStartPoint;
        }
        else if (scene.name == "99.BJH")
        {
            _startPoint = _SecondStartPoint;
        }
        else
        {
            _startPoint = Vector3.zero;
        }
    }
}


SceneManager.sceneLoaded += LoadedsceneEvent;

private void LoadedsceneEvent(Scene scene, LoadSceneMode arg1)
{
    _player = GameObject.FindGameObjectWithTag("Player");
    sceneCheck(scene);
    _savePoint = _startPoint;
}

์”ฌ ์ด๋™์‹œ startpoint๊ฐ€ ๋ฐ”๋€Œ๊ฒŒ ํ• ๋ ค๊ณ  ํ–ˆ๋‹ค, ํ”Œ๋ ˆ์ด์–ด์— ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋‹ฌ์•˜๋Š”๋ฐ ๊ทธ๋Ÿผ ์”ฌ์„ ์ด๋™ํ•  ๋•Œ ์ด ์Šคํฌ๋ฆฝํŠธ๋Š” ํŒŒ๊ดด๋˜๋ฉด ์•ˆ๋œ๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋๊ณ , ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ์”ฌ๋ณ„ ์Šคํƒ€ํŠธ ํฌ์ง€์…˜์„ ๋ฐ”๊พธ๊ธฐ๋กœ ํ–ˆ๋‹ค.







์žก๋‹ด,์ •๋ฆฌ




์ฐธ๊ณ  : ์œ ๋‹ˆํ‹ฐ TOP


๐Ÿ“”

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ