ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 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 >

    더보기
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    using 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 update
        void Start()
        {
            this.btn.onClick.AddListener(() => {
                var go = Instantiate(this.prefab);
                go.transform.position = new Vector3(030);
                this.hero = go.GetComponent<Hero>();
                this.hero.Init();
            });
     
     
        }
     
        // Update is called once per frame
        void 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

     

    더보기
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    using 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 update
        void Start()
        {
        }
     
        public void Init()
        {
            this.anim = this.GetComponent<Animator>();
            StartCoroutine(this.ActiveGravity());
                
        }
     
        // Update is called once per frame
        void Update()
        {
     
            
        }
     
        public void Move(float tx)
        {
            if (this.transform.position.x < tx)
            {
                this.transform.localScale = new Vector3(-777);
     
            }
            else // 후진해야함 / 이미지 반전 
            {
                this.transform.localScale = new Vector3(777);
            }
     
            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

     

Designed by Tistory.