当NPC或物品点击时对角色进行动画处理

时间:2018-12-07 10:09:02

标签: c# unity3d

我在youtube上找到了与NPC和物品互动的教程。我的问题是,一旦单击NPC或物品,如何为我的角色设置动画。我希望角色前进到NPC或物品。我只是感到困惑,应该在哪里放置脚本以动画角色的行走或奔跑。

如您所见,有两个盒子,分别是NPC和一个物品。因此,当我右键单击框时,它应该是步行的,它将在npc和item旁边停止。我该怎么办?

enter image description here

WorldInteraction

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

public class WorldInteraction : MonoBehaviour {
    NavMeshAgent playerAgent;

    void Start() {
        playerAgent = GetComponent<NavMeshAgent>();
    }

    void Update() {
        if (Input.GetMouseButtonDown(0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()) {
            GetInteraction();
        }
    }

    void GetInteraction() {
        Ray interactionRay = Camera.main.ScreenPointToRay (Input.mousePosition);
        RaycastHit interactionInfo;
        if(Physics.Raycast(interactionRay, out interactionInfo, Mathf.Infinity)) {
            GameObject interactedObject = interactionInfo.collider.gameObject;
            if (interactedObject.tag == "Interactable Object") {
                interactedObject.GetComponent<Interactable>().MoveToInteraction(playerAgent);
            } else {
                playerAgent.destination = interactionInfo.point;
            }
        }
    }
}

可交互

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

public class Interactable : MonoBehaviour {
    public NavMeshAgent playerAgent;

    public virtual void MoveToInteraction(NavMeshAgent playerAgent) {
        this.playerAgent = playerAgent;
        playerAgent.destination = this.transform.position;

        Interact();
    }

    public virtual void Interact() {
        Debug.Log ("Interacting with base class.");
    }
}

NPC

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

public class NPC : Interactable {

    public override void Interact() {
        Debug.Log ("Interacting with NPC.");
    }
}

PlayerController

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

public class PlayerController : MonoBehaviour {

        public bool isGrounded;

    private float speed;
        private float rotationSpeed = 75.0f;
    private float moveSpeed = 6f;
    private float moveFB, moveLR;
        public float jumpHeight;
    Rigidbody rb;
        Animator animator;
        CapsuleCollider capcollider;

    public Transform playerCamera, character, centerPoint;
    private float mouseX, mouseY, zoomSpeed = 2, mouseYPosition = 1f;
    public float zoom, zoomMin = -2f, zoomMax = -10f;

    void Start() {
        rb = GetComponent<Rigidbody>();
        animator = GetComponent<Animator>();
            capcollider = GetComponent<CapsuleCollider>();
            isGrounded = true;

            zoom = -5;
    }

    void Update() {
        zoom += Input.GetAxis ("Mouse ScrollWheel") * zoomSpeed;

            if (zoom > zoomMin)
            {
                    zoom = zoomMin;
            }
            if (zoom < zoomMax)
            {
                zoom = zoomMax;
            }
        playerCamera.transform.localPosition = new Vector3 (0, 0, zoom);

        mouseX += Input.GetAxis ("Mouse X");
            mouseY -= Input.GetAxis ("Mouse Y");
            playerCamera.rotation = Quaternion.Euler(0, mouseX, 0);

        mouseY = Mathf.Clamp (mouseY, 2, 60f);
            playerCamera.LookAt(centerPoint);
            centerPoint.localRotation = Quaternion.Euler (mouseY, mouseX, 0);

        moveFB = Input.GetAxis ("Vertical") * moveSpeed;
            moveLR = Input.GetAxis ("Horizontal") * moveSpeed;

            //transform.Translate(0, 0, moveFB);
            //transform.Rotate(0, moveLR, 0);

        Vector3 movement = new Vector3 (moveLR, 0, moveFB);
            movement = character.rotation * movement;
            character.GetComponent<CharacterController>().Move(movement * Time.deltaTime);
            centerPoint.position = new Vector3(character.position.x, character.position.y + mouseYPosition, character.position.z);

            /*if(Input.GetKey(KeyCode.Space) && isGrounded == true)
            {
                    rb.AddForce(0, jumpHeight, 0);
                    animator.SetTrigger("isJumping");
                    isGrounded = false;
            }*/

            if (Input.GetKey(KeyCode.LeftShift))
            {
                    if (Input.GetAxis("Vertical") > 0)
                    {
                        moveSpeed = 8f;
                        Quaternion turnAngle = Quaternion.Euler(0, centerPoint.eulerAngles.y, 0);
                        character.rotation = Quaternion.Slerp(character.rotation, turnAngle, Time.deltaTime * rotationSpeed);
                        animator.SetBool("isRunning", true);
                    }
                    else if (Input.GetAxis("Vertical") < 0) 
                    {
                        moveSpeed = 4f;
                        Quaternion turnAngle = Quaternion.Euler(0, centerPoint.eulerAngles.y, 0);
                        character.rotation = Quaternion.Slerp(character.rotation, turnAngle, Time.deltaTime * rotationSpeed);
                        animator.SetBool("isBack", true);
                    }
                    else if (Input.GetAxis("Vertical") == 0 && Input.GetAxis("Horizontal") == 0)
                    {
                        animator.SetBool("isWalking", false);
                        animator.SetBool("isRunning", false);
                        animator.SetBool("isBack", false);
                    }
            }
            else
            {
                    if (Input.GetAxis("Vertical") > 0)
                    {
                        moveSpeed = 4f;
                        Quaternion turnAngle = Quaternion.Euler(0, centerPoint.eulerAngles.y, 0);
                        character.rotation = Quaternion.Slerp(character.rotation, turnAngle, Time.deltaTime * rotationSpeed);
                        animator.SetBool("isWalking", true);
                    }
                    else if (Input.GetAxis("Vertical") < 0) 
                    {
                        moveSpeed = 4f;
                        Quaternion turnAngle = Quaternion.Euler(0, centerPoint.eulerAngles.y, 0);
                        character.rotation = Quaternion.Slerp(character.rotation, turnAngle, Time.deltaTime * rotationSpeed);
                        animator.SetBool("isBack", true);
                    }
                    else if (Input.GetAxis("Vertical") == 0 && Input.GetAxis("Horizontal") == 0)
                    {
                        animator.SetBool("isWalking", false);
                        animator.SetBool("isRunning", false);
                        animator.SetBool("isBack", false);
                    }
            }
    }
}

0 个答案:

没有答案