C#/수업내용

2020.05.06. 수업내용 - Unity 캐릭터 이동시키기

dev_sr 2020. 5. 6. 22:21

 

1. 코드

 

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Hero : MonoBehaviour
{
 
    
    public GameObject model;
    public int hp;
    public string heroName;
    public bool isImmortal;
    public float speed;
 
 
    // Start is called before the first frame update
    void Start()
    {
        //Debug.Log("Hello world");
        Debug.LogFormat("{0}, {1}, {2}, {3}"this.hp, this.heroName, this.isImmortal, this.speed);
 
        //애니메이션 구성(component)가져오기 
         Animation anim = this.model.GetComponent<Animation>();
        
        //idle 상태
        //Debug.Log(anim);
 
        //달리기 모션 실행
        anim.Play("run@loop");
        Debug.Log(anim);
 
    }
 
    // Update is called once per frame
    void Update()
    {
        //캐릭터 이동시키기 
        //속도, 방향, 시간이 필요
        Vector3 dir = Vector3.forward;
        Vector3 displacement = this.speed * dir * Time.deltaTime;
 
        this.gameObject.transform.Translate(displacement);
    }
}
 
 

 

 

2. 실행 결과

-무한정 뛰어감