Post

반응형

using System.Collections;

using System.Collections.Generic;

using UnityEngine;


//public class NewBehaviourScript : MonoBehaviour {


    public class MoveScript : MonoBehaviour

    {

        public float moveSpeed = 2.0f;


        public float rotSpeed = 2.0f;


        Vector3 posTarget;




        void Awake()


        {


            //moveSpeed = 10.0f;


            posTarget = new Vector3();


        }




        // Use this for initialization


        void Start()


        {


            posTarget = transform.position;


        }




        // Update is called once per frame


        void Update()


        {


            // 느린컴퓨터는 Update에 느리게 들어오고 빠른컴퓨터는 빠르다...속도를 동일하게 하기위해


            // 시간계념이 필요함 (deltaTime)




            bool trans = false;


            if (trans)


            {


                float mx = Input.GetAxis("Horizontal"); // -1 ~ 1;


                transform.Rotate(Vector3.up * mx * Time.deltaTime * rotSpeed);


            }


            else


            {




                float mz = Input.GetAxis("Vertical"); // -1 ~ 1;


                float mx = Input.GetAxis("Horizontal"); // -1 ~ 1;




                Vector3 v = new Vector3(mx, 0, mz);


                v.Normalize(); // 원본 변경


                //v.Normalized(); // 원본 변경x


                transform.Translate(v * Time.deltaTime * moveSpeed);




                transform.Rotate(Vector3.up * mx * Time.deltaTime * rotSpeed);


                //transform.Translate(Vector3.forward * mz * Time.deltaTime * moveSpeed);


                //transform.Translate(Vector3.right * mx * Time.deltaTime * moveSpeed);




                // 마우스로 좌표찍어서 이동


                if (Input.GetButtonDown("Fire1"))


                {


                    Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);


                    RaycastHit hit;


                    if (Physics.Raycast(r, out hit))


                    {


                        posTarget = hit.point;


                        Debug.Log(hit.transform.gameObject);


                        // 방향을 돌린다


                        //transform.LookAt (new Vector3(posTarget.x, 0, 0));


                        //transform.LookAt (posTarget);


                    }


                }




            }


            // 선형보간 이동


            //transform.position = Vector3.Lerp (transform.position, posTarget, Time.deltaTime * moveSpeed);


            // 정규속도로 가기


            if (Vector3.Distance(transform.position, posTarget) >= 0.1f)


            {


                Vector3 dir = posTarget - transform.position;


                // 부드럽게 방향 돌림


                // 나의 앞 방향과 목표 방향과 normallize해서 어느정도 이동해야하는지 결과값이 나옴


                Vector3 look = Vector3.Slerp(transform.forward, dir.normalized, Time.deltaTime * rotSpeed);


                // up y축 회전


                transform.rotation = Quaternion.LookRotation(look, Vector3.up);


                transform.position = Vector3.MoveTowards(transform.position, posTarget, Time.deltaTime * moveSpeed);

            }


        }




        void OnGUI()

        {


            // 방향키 앞뒤 누를 시 -1 ~ 1의 을 출력


            GUI.Label(new Rect(0, 0, 150, 40), Input.GetAxis("Vertical").ToString());


        }


    }

//}



반응형

'이전게시판 > Unity' 카테고리의 다른 글

유니티 2D충돌체크, 코루틴  (0) 2018.04.29
유니티 스프라이트2D  (0) 2018.04.28
유니티 네비게이션, 파티클  (0) 2018.04.22
총알 만들고 쏘기(Prepfeb)  (0) 2018.04.21
Unity 기초 정리  (0) 2018.04.14
유니티 C# Scene 변경  (0) 2018.04.14
UGUI 캔버스, UI 정리  (0) 2017.10.11
유니티 UGUI 버튼 예제  (0) 2017.10.08
▲ top