Unity c# for designer - 6강 (미니게임 만들며 Script 배우기)

 

학습내용

 
  • 과제풀이

  • 방향 버튼에 맞게 캐릭터 회전하기

  • 이동 완료시 까지 다른 버튼 작동안되게 처리하기

  • 충돌 처리





6-1강

6- 1강  코드  공유 드립니다.

아래 코드는 정답이 아니니 심플하게 바꾸셔도 좋습니다.~^^

 

 

using UnityEngine;
using System.Collections;

public class studyCharMove : MonoBehaviour {


    //public
    public GameObject Target;
    public float MovePower = 0.0f;
    public float MoveSpeed = 0.0f;
    public float RotationSpeed = 0.0f;

    //private
    private float TargetPosition;
    private float TargetSetPosition;
    private Quaternion RotationQ;
    private bool OnForward;
    private bool OnLeft;
    private bool OnRight;
    private bool Moving;




    // Use this for initialization
    void Start () {

    }

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

        //forward move
        if(OnForward){
            Moving = true;
            Target.gameObject.transform.position += new Vector3(MovePower,0,0)*(Time.deltaTime * MoveSpeed);
            TargetPosition = Target.gameObject.transform.position.x;

            //Rotation
            RotationQ = Quaternion.Euler(0,0,0);
            Target.transform.rotation = Quaternion.Slerp(Target.transform.rotation, RotationQ , RotationSpeed * Time.deltaTime);

            if(TargetPosition >= TargetSetPosition){
                OnForward = false;
                Moving = false;
            }

        }


        // Left move
        if(OnLeft){
            Moving = true;
            Target.gameObject.transform.position += new Vector3(0,0,MovePower)*(Time.deltaTime * MoveSpeed);
            TargetPosition = Target.gameObject.transform.position.z;

            //Rotation
            RotationQ = Quaternion.Euler(0,-90.0f,0);
            Target.transform.rotation = Quaternion.Slerp(Target.transform.rotation,RotationQ , RotationSpeed * Time.deltaTime);

            if(TargetPosition >= TargetSetPosition){
                OnLeft = false;
                Moving = false;
            }
        }

        // Right move
        if(OnRight){
            Moving = true;
            Target.gameObject.transform.position -= new Vector3(0,0,MovePower)*(Time.deltaTime * MoveSpeed);
            TargetPosition = Target.gameObject.transform.position.z;

            //Rotation
            RotationQ = Quaternion.Euler(0,90.0f,0);
            Target.transform.rotation = Quaternion.Slerp(Target.transform.rotation, RotationQ , RotationSpeed * Time.deltaTime);

            if(TargetPosition <= TargetSetPosition){
                OnRight = false;
                Moving = false;
            }
        }

    }


    //forward move btn
    public void CharForwardMove(){
        if(!Moving){
            TargetSetPosition = Target.gameObject.transform.position.x + MovePower;
            OnForward = true;
        }
    }

    // Left move btn
    public void CharLeftMove(){
        if(!Moving){
            TargetSetPosition = Target.gameObject.transform.position.z + MovePower;
            OnLeft = true;
        }
    }

    // Right move btn
    public void CharRightMove(){
        if (!Moving) {
            TargetSetPosition = Target.gameObject.transform.position.z - MovePower;
            OnRight = true;
        }
    }

} 




6-2강
using UnityEngine;
using System.Collections;

public class CharMove : MonoBehaviour {


    //public
    public GameObject Target;
    public float MovePower = 0.5f;
    public float MoveSpeed = 5.0f;
    public float RotationSpeed = 20.0f;
    public float ReturnPower = 0.5f;


    //private
    private float TargetPosition;
    private float TargetSetPosition;
    private Quaternion RotationQ;
    private bool OnForward;
    private bool OnLeft;
    private bool OnRight;
    private bool Moving;
    private bool StateForward;
    private bool StateLeft;
    private bool StateRight;


    //Collider Controller
    private string OnMoveVecter = "State";
    private string StayVecter = "State";




    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {

        Debug.Log ("OnMoveVecter : "+OnMoveVecter);

        //forward move
        if(OnForward){
            Moving = true;
            Target.gameObject.transform.position += new Vector3(MovePower,0,0)*(Time.deltaTime * MoveSpeed);
            TargetPosition = Target.gameObject.transform.position.x;

            //Rotation
            RotationQ = Quaternion.Euler(0,0,0);
            Target.transform.rotation = Quaternion.Slerp(Target.transform.rotation, RotationQ , RotationSpeed * Time.deltaTime);

            if(TargetPosition >= TargetSetPosition){
                OnForward = false;
                Moving = false;
            }

        }


        // Left move
        if(OnLeft){
            Moving = true;
            Target.gameObject.transform.position += new Vector3(0,0,MovePower)*(Time.deltaTime * MoveSpeed);
            TargetPosition = Target.gameObject.transform.position.z;

            //Rotation
            RotationQ = Quaternion.Euler(0,-90.0f,0);
            Target.transform.rotation = Quaternion.Slerp(Target.transform.rotation, RotationQ , RotationSpeed * Time.deltaTime);

            if(TargetPosition >= TargetSetPosition){
                OnLeft = false;
                Moving = false;
            }

            Moving = false;
        }

        // Right move
        if(OnRight){
            Moving = true;
            Target.gameObject.transform.position -= new Vector3(0,0,MovePower)*(Time.deltaTime * MoveSpeed);
            TargetPosition = Target.gameObject.transform.position.z;

            //Rotation
            RotationQ = Quaternion.Euler(0,90.0f,0);
            Target.transform.rotation = Quaternion.Slerp(Target.transform.rotation, RotationQ , RotationSpeed * Time.deltaTime);

            if(TargetPosition <= TargetSetPosition){
                OnRight = false;
                Moving = false;
            }
        }

    }


    //forward move btn
    public void CharForwardMove(){
        if (Moving == false & StateForward == false) {
            TargetSetPosition = Target.gameObject.transform.position.x + MovePower;
            OnForward = true;
            OnMoveVecter = "ForwardVecter";
        }
    }

    // Left move btn
    public void CharLeftMove(){

        if(Moving == false & StateLeft == false){
            TargetSetPosition = Target.gameObject.transform.position.z + MovePower;
            OnLeft = true;
            OnMoveVecter = "LetfVecter";

        }
    
    }

    // Right move btn
    public void CharRightMove(){
        if (Moving == false & StateRight == false) {
            TargetSetPosition = Target.gameObject.transform.position.z - MovePower;
            OnRight = true;
            OnMoveVecter = "RightVecter";
        }
    }





    //Trigger ON
    void OnTriggerEnter(Collider col){
        StayVecter = OnMoveVecter;
    }

    //collider
    void OnTriggerStay(Collider col){
        
        Debug.Log ("TriggerStay : " + "Trigger Stay!!");
        if(StayVecter == "ForwardVecter"){
            StateForward = true;
        }else if(StayVecter == "LetfVecter"){
            StateLeft = true;
        }else if(StayVecter == "RightVecter"){
            StateRight = true;
        }
    }

    void OnTriggerExit(Collider col){
        StateForward = false;
        StateLeft = false;
        StateRight = false;
        OnMoveVecter = "State";
        StayVecter = "State";
    }


} 




Posted by 프리랜서 디자이너