[TIL] 50 AddForce player.SetParent โญโญ
์นดํ ๊ณ ๋ฆฌ: Til
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์ถ์ด ์ด์ํ๊ฒ ๋๋ค. ๋ด์ผ ์๊ฐํด๋ณด๊ธฐ.
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๊ฐ ๋ฐ๋๊ฒ ํ ๋ ค๊ณ ํ๋ค, ํ๋ ์ด์ด์ ์คํฌ๋ฆฝํธ๋ฅผ ๋ฌ์๋๋ฐ ๊ทธ๋ผ ์ฌ์ ์ด๋ํ ๋ ์ด ์คํฌ๋ฆฝํธ๋ ํ๊ดด๋๋ฉด ์๋๋ค๊ณ ์๊ฐ์ด ๋๊ณ , ๋ค๋ฅธ ๋ฐฉ๋ฒ์ผ๋ก ์ฌ๋ณ ์คํํธ ํฌ์ง์ ์ ๋ฐ๊พธ๊ธฐ๋ก ํ๋ค.
๋๊ธ๋จ๊ธฐ๊ธฐ