制作RPG游戏的部分核心代码分析

PlayerMove脚本

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

public enum State
{
    
    
    Idle,
    Walk
}


public class PlayerMove : MonoBehaviour
{
    
    
    public float speed = 1;
    private PlayerDir dir;
    private CharacterController controller;
    public State playerState;
    public bool isMoving = false;
    // Start is called before the first frame update
    void Start()
    {
    
    
        dir = this.transform.GetComponent<PlayerDir>();
        controller = this.GetComponent<CharacterController>();
        playerState = State.Idle;
    }

    // Update is called once per frame
    void Update()
    {
    
    
        float distance = Vector3.Distance(dir.TargetPosition, transform.position);
        if (distance > 0.3f)
        {
    
    
            isMoving = true;
            controller.SimpleMove(transform.forward * speed);
            playerState = State.Walk;
        }
        else
        {
    
    
            isMoving = false;
            playerState = State.Idle;
        }
    }
}

PlayerDir玩家朝向问题

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

public class PlayerDir : MonoBehaviour
{
    
    
    public GameObject effect_Click;
    // Start is called before the first frame update
    private bool isMoving = false;//表示鼠标是否按下

    private Vector3 targetPosition = Vector3.zero;

    private PlayerMove playerMove;
    public Vector3 TargetPosition
    {
    
    
        get
        {
    
    
            return targetPosition;
        }
    }

    private Vector3 targetMousePositon;

    void Start()
    {
    
    
        targetPosition = transform.position;
        playerMove = this.GetComponent<PlayerMove>();
        Debug.Log(UICamera.mainCamera.name);
    }

    // Update is called once per frame
    void Update()
    {
    
    
        if(Input.GetMouseButtonDown(0)&&!UICamera.isOverUI)
        {
    
    
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;
            bool isCollider = Physics.Raycast(ray, out hitInfo);
            if(isCollider&&hitInfo.collider.tag==Tags.Ground)
            {
    
    
                isMoving = true;
                LookAtTarget(hitInfo.point);
                ShowClickEffect(hitInfo.point);
            }
        }

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

        if(isMoving)
        {
    
    
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;
            bool isCollider = Physics.Raycast(ray, out hitInfo);
            if (isCollider && hitInfo.collider.tag == Tags.Ground)
            {
    
    
                LookAtTarget(hitInfo.point);
            }
          
        }
        else
        {
    
    
            if(playerMove.isMoving)
            {
    
    
                LookAtTarget(targetPosition);
            }
        }
    }

    void ShowClickEffect(Vector3 hitPoint)
    {
    
    
        hitPoint = new Vector3(hitPoint.x, hitPoint.y + 0.1f, hitPoint.z);
        GameObject.Instantiate(effect_Click, hitPoint, Quaternion.identity);
    }

    void LookAtTarget(Vector3 hitPoint)
    {
    
    
        targetPosition = hitPoint;
        targetPosition.y = this.transform.position.y;
        targetPosition = new Vector3(TargetPosition.x, TargetPosition.y, TargetPosition.z);

        this.transform.LookAt(targetPosition);

    }
}

PlayerAnimation脚本

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

public class PlayerAnimation : MonoBehaviour
{
    
    
    private PlayerMove move;
    private Animation animations;
    // Start is called before the first frame update
    void Start()
    {
    
    
        move = this.GetComponent<PlayerMove>();
        animations = GetComponent<Animation>();
    }

    // Update is called once per frame
    void LateUpdate()
    {
    
    
        if(move.playerState==State.Idle)
        {
    
    
            PlayAnim("Sword-Idle");
        }
        else if(move.playerState==State.Walk)
        {
    
    
            PlayAnim("Sword-Walk");
        }
    }

    void PlayAnim(string animName)
    {
    
    
        animations.CrossFade(animName);
    }
}

相机跟随的脚本

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

public class FollowPlayer : MonoBehaviour
{
    
    
    private Transform player;
    // Start is called before the first frame update
    private Vector3 offsetPosition;

    public float distance = 0;
    public float scrollSpeed = 10;
    private bool isRotating;
    public float rotateSpeed = 2;

    void Start()
    {
    
    
        player = GameObject.FindGameObjectWithTag(Tags.player).transform;

        transform.LookAt(player);

        offsetPosition = transform.position - player.position;

    }

    // Update is called once per frame
    void Update()
    {
    
    
        transform.position = player.position + offsetPosition;
     
        RotateView();
        ScrollView();
    }

    private void RotateView()
    {
    
    
        Input.GetAxis("Mouse X");
        Input.GetAxis("Mouse Y");
        if(Input.GetMouseButtonDown(1))
        {
    
    
            isRotating = true;
        }
        if(Input.GetMouseButtonUp(1))
        {
    
    
            isRotating = false;
        }

        if(isRotating)
        {
    
    
            transform.RotateAround(player.position, Vector3.up, rotateSpeed * Input.GetAxis("Mouse X"));

            Vector3 originalPos = transform.position;

            Quaternion originalRotation = transform.rotation;

            transform.RotateAround(player.position, transform.right, -rotateSpeed * Input.GetAxis("Mouse Y"));

            float x = transform.eulerAngles.x;

           // x = Mathf.Clamp(x, 10, 80);

           // transform.eulerAngles = new Vector3(x, transform.eulerAngles.y, transform.eulerAngles.z);
           if(x<10||x>80)
            {
    
    
                transform.position = originalPos;
                transform.rotation = originalRotation;
            }
           

        }

        offsetPosition = transform.position - player.position;
    }

    void ScrollView()
    {
    
    
        print(Input.GetAxis("Mouse ScrollWheel"));
        distance = offsetPosition.magnitude;
        distance += Input.GetAxis("Mouse ScrollWheel")*scrollSpeed;
        distance = Mathf.Clamp(distance, 2, 18);
        offsetPosition = offsetPosition.normalized * distance;
    }
}

猜你喜欢

转载自blog.csdn.net/charlsdm/article/details/123917162
今日推荐