한 걸음 두 걸음

가상증강현실및실습 0329 ] 키보드 Input 받아 이동 / 마우스 입력 추적하기 본문

Unity

가상증강현실및실습 0329 ] 키보드 Input 받아 이동 / 마우스 입력 추적하기

언제나 변함없이 2019. 3. 29. 09:29
반응형

키보드 Input 받아 이동

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CsMove : MonoBehaviour
{
    int speed = 5;


    void Update()
    {
        float amtMove = speed * Time.deltaTime;

        float keyForward = Input.GetAxis("Vertical");
        //이외에 getKeyDown으로 받을 수도 있다.
        float keySide = Input.GetAxis("Horizontal");

        transform.Translate(Vector3.forward * amtMove * keyForward);
        transform.Translate(Vector3.right * amtMove * keySide);   

    }
}

keyboard받아 이동 및 회전

오른쪽 / 왼쪽 버튼 클릭할 때 회전하는 기능을 넣었ㅆ음! (간단)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CsMove : MonoBehaviour
{
    private int speed = 5;
    private int rotSpeed = 120;

    void Update()
    {
        float amtMove = speed * Time.smoothDeltaTime;
        float amtRot = rotSpeed /* * Time.smoothDeltaTime*/; 

        float keyForward = Input.GetAxis("Vertical");
        float keyRot = Input.GetAxis("Horizontal");

        transform.Translate(Vector3.forward * amtMove * keyForward);
        transform.Rotate(Vector3.up * amtMove * keyRot);   

    }
}

그런데 여기서 추가로,


위의 상속관계를 만들어주면
이동 및 회전할 때 구나 카메라가 같은 vector로 움직여서
따라다니게된다.

다중 오프젝트 제어 _ 새로운 축 추가하기

이제 q, e누를 때마다 getAxis("turret"); Input으로 값을 받아옵니다ㅎㅎ

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CsMove : MonoBehaviour
{
    public GameObject turret; //여기서 GameObject를 공용변수로 만들어놓았기 때문에 Cube나 Sphere를 넣어서 설정해주면 됩니다.
    private int moveSpeed = 10;
    private int rotSpeed = 120;

    void Update()
    {
        float amtMove = moveSpeed * Time.smoothDeltaTime;
        float amtRot = rotSpeed  * Time.smoothDeltaTime; 

        float keyForward = Input.GetAxis("Vertical");
        float keyRot = Input.GetAxis("Horizontal");

        float keyTurret = Input.GetAxis("Turret"); //새로운 축이에요~


        transform.Translate(Vector3.forward * amtMove * keyForward);
        transform.Rotate(Vector3.up * amtMove * keyRot);

        turret.transform.Rotate(Vector3.up * amtRot * keyTurret);

    }
}

마우스 추적하기

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class control : MonoBehaviour
{
    private Vector3 lookDir;
    bool moving;
    int speed;

    // Start is called before the first frame update
    void Start()
    {
        moving = false;
        speed = 5;


    }

    // Update is called once per frame
    void Update()
    {
        MovePacman();

        if (Input.GetKeyDown(KeyCode.Escape)) {
            Application.Quit();
        }


    }

    void MovePacman()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(ray, out hit, Mathf.Infinity)) {
                lookDir = hit.point; lookDir.y = 0;
            }
            moving = true;
        }
        if (moving) {
            float amtMove = speed * Time.smoothDeltaTime;
            transform.LookAt(lookDir);
            transform.Translate(Vector3.forward * amtMove);
            float dist = Vector3.Distance(transform.position, lookDir);
            if (dist < 0.1f) moving = false;
        }
    }


}
반응형