-
20200527 / 캐릭터 마우스 이동 메모C#/수업내용 2020. 5. 27. 18:37
리지드 바디
Use Gravity
콜라이더
https://docs.unity3d.com/kr/530/ScriptReference/Collider.html
이즈트리거
리지드바디
리지드바디, 콜라이더 붙어있어야 충돌 감지 가능
unity paltformer
Mathf.Abs > 절대 값 반환
점프 할때
올라갈때
s: 현재 속도 = (초속 + 가속도 * 시간) * 시간
현재속도 = ( 1 + 9.8f(중력가속도) * Time.deltaTime) * Time.deltaTime;
this.transform.translate(방향 * 속도 * 시간)
>> this.transform.translate(방향 * 현재속도); <<이미 시간 곱했으니 시간 빼고
내려갈때
S = (v -at) t
s: 현재 속도 = (초속 - 가속도 * 시간) * 시간
현재속도 = ( 1 - 9.8f(중력가속도) * Time.deltaTime) * Time.deltaTime;
>> 방향도 포함하고있다
this.transform.translate(속도)
코드>
Test >
더보기1234567891011121314151617181920212223242526272829303132333435363738394041424344using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;public class Test1 : MonoBehaviour{public Button btn;public GameObject prefab;public Hero hero;// Start is called before the first frame updatevoid Start(){this.btn.onClick.AddListener(() => {var go = Instantiate(this.prefab);go.transform.position = new Vector3(0, 3, 0);this.hero = go.GetComponent<Hero>();this.hero.Init();});}// Update is called once per framevoid Update(){if (Input.GetMouseButtonDown(0)){var ray = Camera.main.ScreenPointToRay(Input.mousePosition);RaycastHit hit;if (Physics.Raycast(ray, out hit, 1000)){Debug.LogFormat("Point : {0}", hit.point.x);this.hero.Move(hit.point.x);}}}}cs hero
더보기123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106using System.Collections;using System.Collections.Generic;using System.Runtime.CompilerServices;using UnityEditor.UIElements;using UnityEngine;using UnityEngine.Events;public class Hero : MonoBehaviour{public float g = 9.8f;public bool isGround;private Animator anim;private Coroutine moveRoutine;// Start is called before the first frame updatevoid Start(){}public void Init(){this.anim = this.GetComponent<Animator>();StartCoroutine(this.ActiveGravity());}// Update is called once per framevoid Update(){}public void Move(float tx){if (this.transform.position.x < tx){this.transform.localScale = new Vector3(-7, 7, 7);}else // 후진해야함 / 이미지 반전{this.transform.localScale = new Vector3(7, 7, 7);}if (this.moveRoutine != null){this.StopCoroutine(this.moveRoutine);}this.StartCoroutine(this.MoveImpl(tx));}IEnumerator MoveImpl(float tpos){while(true){var dis = Mathf.Abs(tpos - this.transform.position.x);if (dis <= 0.02f){break;}if (this.transform.position.x < tpos){this.transform.Translate(Vector2.right * 3f * Time.deltaTime, Space.World);}else // 후진해야함 / 이미지 반전{this.transform.Translate(Vector2.left * 3f * Time.deltaTime, Space.World);}// this.anim.Play("LightGuard_Run");yield return null;}Debug.Log("이동완료");}IEnumerator ActiveGravity(){while (true){this.transform.Translate(Vector3.down * g * Time.deltaTime);if (this.isGround){this.transform.position = new Vector3(0, -4.5f, 0);break;}yield return null;}}private void OnTriggerEnter(Collider other){if(other.name == "ground"){this.isGround = true;//var rigidbody = this.GetComponent<Rigidbody>();//rigidbody.constraints = RigidbodyConstraints.FreezePositionY;}}}cs 'C# > 수업내용' 카테고리의 다른 글
20200526 / 코루틴(Coroutine) (0) 2020.05.26 Unity / 금일 메모 내용 정리 (0) 2020.05.22 2020-05-18 [UI구성 / JSON기반 데이터 저장/불러오기] (0) 2020.05.18 2020-05-15 [delgate / 대리자] (0) 2020.05.15 2020-05-14 [unity / 어제에 이어서] (0) 2020.05.14