Flutter (플루터) 1일차

Project 2021. 7. 12. 18:12
반응형

 

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

public class CameraFollow : MonoBehaviour
{
    public float cameraMoveSpeed = 120.0f;
    public GameObject cameraFollowGo;
    public VariableJoystick joystic;
    public Transform playerDir;
    Vector3 followPos;
    public float clampAngle = 80.0f;
    public float inputSensitivity = 150.0f;
    public GameObject cameraGo;
    public GameObject playerGo;
    public float camDistanceXToPlayer;
    public float camDistanceYToPlayer;
    public float camDistanceZToPlayer;
    public float mouseX;
    public float mouseY;
    public float finalInputX;
    public float finalInputZ;
    public float smoothX;
    public float smoothY;
    private float rotY = 0.0f;
    private float rotX = 0.0f;

    private bool isDrag = false;

    // Start is called before the first frame update
    void Start()
    {
        Vector3 rot = this.transform.localRotation.eulerAngles;
        rotY = rot.y;
        rotX = rot.x;
        //Cursor.lockState = CursorLockMode.Locked;
        //Cursor.visible = false;
    }

    // Update is called once per frame
    void Update()
    {
        this.transform.position = this.playerGo.transform.position;

        if (joystic.Vertical != 0 && joystic.Horizontal != 0) return;

        if (Input.GetMouseButtonDown(0)) {
            this.isDrag = true;
        }

        if (Input.GetMouseButtonUp(0)) {
            this.isDrag = false;
        }

        if (this.isDrag) {
            float inputX = Input.GetAxis("RightStickHorizontal");
            float inputZ = Input.GetAxis("RightStickVertical");
            mouseX = Input.GetAxis("Mouse X");
            mouseY = Input.GetAxis("Mouse Y");
            finalInputX = inputX + mouseX;
            finalInputZ = inputZ + mouseY;

            rotY += finalInputX * inputSensitivity * Time.deltaTime;
            rotX += finalInputZ * inputSensitivity * Time.deltaTime;

            rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);

            Quaternion localRotation = Quaternion.Euler(rotX, rotY, 0.0f);
            transform.rotation = localRotation;

            this.playerDir.rotation = Quaternion.Euler(0.0f, rotY, 0.0f);
        }
        
    }

    private void LateUpdate()
    {
        if (isDrag) {
            this.CameraUpdater();
        }
        
    }

    private void CameraUpdater() {
        Transform target = cameraFollowGo.transform;
        float step = cameraMoveSpeed * Time.deltaTime;
        this.transform.position = Vector3.MoveTowards(this.transform.position, target.position, step);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterController : MonoBehaviour
{
    public VariableJoystick joystic;
    private Animator anim;
    public float speed = 3.0f;
    public Transform cameraBase;
    public Transform playerDir;

    // Start is called before the first frame update
    void Start()
    {
        this.anim = this.GetComponent<Animator>();    
    }

    // Update is called once per frame
    void Update()
    {
        if (joystic.Horizontal != 0 && joystic.Vertical != 0)
        {
            var eulerAngles = new Vector3(0, Mathf.Atan2(joystic.Horizontal, joystic.Vertical) * 180 / Mathf.PI, 0);
            transform.eulerAngles = eulerAngles + this.playerDir.localRotation.eulerAngles;
            this.transform.Translate(Vector3.forward * speed * Time.deltaTime);
            this.anim.Play("run");
        }
        else
        {
            this.anim.Play("look_around");
        }
    }
}
반응형

'Project' 카테고리의 다른 글

Dungeon Raiders Proto 1 (match 3 puzzle unity)  (0) 2023.03.17
[게임출시] 배틀워즈 - 게임으로 즐기는 영단어  (0) 2022.10.21
마법사 스킬  (0) 2019.06.13
시스템 재구성  (0) 2019.06.06
공주를 구해줘 시스템 개선  (0) 2019.06.05
: