简体   繁体   English

如何在我的播放器控制器脚本中添加跑步和动画?

[英]How do I add running and animations to my player controller script?

I am new to Unity and have been having a hard time adding running and animations (I have the animations and they are set up in animator) to my FPS controller script. 我是Unity的新手,一直很难将运行和动画(我有动画,它们是在动画器中设置)添加到FPS控制器脚本中的。 Can someone please help me add running and animations? 有人可以帮我添加跑步和动画吗? I would be extremely grateful. 我将非常感谢。

Here is my code: 这是我的代码:

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

public class PlayerController : MonoBehaviour
{
Animator anim;
float RotateX;
float RotateY;

public static bool GamePaused;
[SerializeField]
[Header("Game Objects")]
public GameObject Camera;
public GameObject PauseMenu;
public GameObject Player;

[Header("Movement Settings")]
public float WalkSpeed = 5.0f;
public float RunSpeed = 10.0f;

[Header("Rotation Settings")]
public float RorationSpeed;
public float MaxYAxis = 60.0f;       // right
public float MinYAxis = -48.0f;     // left

public bool Grounded;

private void Start()
{
    anim = GetComponent<Animator>();
}
void Update()
{

    transform.Translate(Vector3.forward * Input.GetAxis("Vertical") * WalkSpeed * Time.deltaTime);
    transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * WalkSpeed * Time.deltaTime);

    RotateX += Input.GetAxis("Mouse X") * RorationSpeed;
    RotateY += Input.GetAxis("Mouse Y") * RorationSpeed;
    RotateY = Mathf.Clamp(RotateY, MinYAxis, MaxYAxis);
    Camera.transform.localRotation = Quaternion.Euler(-RotateY, 0f, 0f);
    transform.rotation = Quaternion.Euler(0f, RotateX, 0f);

    }
}

Here is what I have to trigger the different animations in the animator, but the animation follows through when I release the key rather than switching immediately upon release. 这是我必须触发动画制作器中的不同动画的原因,但是当我释放键时动画会一直执行,而不是在释放时立即切换。

    if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.A) || 
Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.D) || 
Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.LeftArrow) || 
Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.RightArrow))
    {
        anim.SetBool("IsWalking", true);
    }

    if (Input.GetKeyUp(KeyCode.W) || Input.GetKeyDown(KeyCode.A) || 
Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.D) || 
Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.LeftArrow) || 
Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.RightArrow))
    {
        anim.SetBool("IsWalking", false);
    }

What I can suggest is that you use the horizontal and vertical axes, rather than hard-coded key presses. 我可以建议您使用水平和垂直轴,而不要使用硬编码的按键。

More specifically, first of all (if you have not already) in the animator window create an idle state and a walking animation state. 更具体地说,首先(如果尚未创建)在动画器窗口中创建一个空闲状态和一个行走动画状态。 You will need somehow to transition between the two states. 您将需要某种方式在这两种状态之间转换。 To do that you will need to create a new bool parameter (let's name it "isWalking" as you did) and create a new condition between the idle and the walking state. 为此,您将需要创建一个新的bool参数(就像您一样将其命名为“ isWalking”),并在空闲状态和步行状态之间创建一个新条件。 For example, set to transition between "idle" to "walking" when isWalking is true and transition between "walking" to "idle" when isWalking is false. 例如,当isWalking为true时,设置为从“ idle”到“ walking”之间的过渡;当isWalking为false时, 设置为从“ walking”到“ idle”之间的过渡。

Now in your PlayerController script in the update or fixed update you can add the following code 现在,在更新或固定更新的PlayerController脚本中,您可以添加以下代码

horizontalMovement = Input.GetAxis("Horizontal");
verticalMovement = Input.GetAxis("Vertical");

//normalize vector so movement in two axis simultanesly is balanced.
moveDirection = (horizontalMovement * transform.right + verticalMovement * transform.forward).normalized;

/* based on your code although a rigid body solution or character controller would have been more robust */
transform.Translate(moveDirection * WalkSpeed * Time.deltaTime);

if (horizontal != 0 || vertical != 0)
{
    animator.setFloat("isWalking",true);
}
else
{
    animator.setFloat("isWalking",false);
}

However this solution is working is implemented based on the code you provided. 但是,此解决方案正在运行,是根据您提供的代码实现的。 If you want to switch in a more robust and easy to maintain script you can use this free controller that uses a rigidbody and has animations already installed and working. 如果要切换到更健壮和易于维护的脚本,可以使用此免费控制器,该控制器使用刚体并已安装动画并且可以正常工作。

https://github.com/PanMig/First-Person-Unity-Camera https://github.com/PanMig/First-Person-Unity-Camera

Here is my working script with walking, running, jumping and animation triggers. 这是我的步行,跑步,跳跃和动画触发器的工作脚本。

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

public class PlayerController : MonoBehaviour
{
Animator anim;
float RotateX;
float RotateY;
Rigidbody rb;
public static bool GamePaused;

public bool isGrounded;

[SerializeField]
[Header("Game Objects")]
public GameObject Camera;
public GameObject PauseMenu;
public GameObject Player;

[Header("Movement Settings")]
public float DefaultSpeed = 5.0f;
public float WalkSpeed = 5.0f;
public float RunSpeed = 10.0f;
public float jumpForce = 2.5f;
public Vector3 jump;

[Header("Rotation Settings")]
public float RorationSpeed = 3.0f;
public float MaxYAxis = 60.0f;
public float MinYAxis = -48.0f;

private void Start()
{
    anim = GetComponent<Animator>();
    rb = GetComponent<Rigidbody>();
    jump = new Vector3(0.0f, 1.0f, 0.0f);
}

void Update()
{
    Rotation();
    Movement();
    Bool();

    if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
    {

        rb.AddForce(jump * jumpForce, ForceMode.Impulse);
        isGrounded = false;
    }
}

void Rotation()
{
    RotateX += Input.GetAxis("Mouse X") * RorationSpeed;
    RotateY += Input.GetAxis("Mouse Y") * RorationSpeed;
    RotateY = Mathf.Clamp(RotateY, MinYAxis, MaxYAxis);
    Camera.transform.localRotation = Quaternion.Euler(-RotateY, 0f, 0f);
    transform.rotation = Quaternion.Euler(0f, RotateX, 0f);
}

void Movement()
{
    transform.Translate(Vector3.forward * Input.GetAxis("Vertical") * WalkSpeed * Time.deltaTime);
    transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * WalkSpeed * Time.deltaTime);

    if (Input.GetKeyDown(KeyCode.LeftShift))
    {
        WalkSpeed = RunSpeed;
    }
    if (Input.GetKeyUp(KeyCode.LeftShift))
    {
        WalkSpeed = DefaultSpeed;
    }
}

void Bool()
{
    if (Input.GetKeyDown(KeyCode.W))
    {
        anim.SetBool("IsWalkingForward", true);
    }
    if (Input.GetKeyDown(KeyCode.A))
    {
        anim.SetBool("IsWalkingLeft", true);
    }
    if (Input.GetKeyDown(KeyCode.S))
    {
        anim.SetBool("IsWalkingBack", true);
    }
    if (Input.GetKeyDown(KeyCode.D))
    {
        anim.SetBool("IsWalkingRight", true);
    }
    if (Input.GetKeyDown(KeyCode.UpArrow))
    {
        anim.SetBool("IsWalkingForward", true);
    }
    if (Input.GetKeyDown(KeyCode.LeftArrow))
    {
        anim.SetBool("IsWalkingLeft", true);
    }
    if (Input.GetKeyDown(KeyCode.DownArrow))
    {
        anim.SetBool("IsWalkingBack", true);
    }
    if (Input.GetKeyDown(KeyCode.RightArrow))
    {
        anim.SetBool("IsWalkingRight", true);
    }


    if (Input.GetKeyUp(KeyCode.W))
    {
        anim.SetBool("IsWalkingForward", false);
    }
    if (Input.GetKeyUp(KeyCode.A))
    {
        anim.SetBool("IsWalkingLeft", false);
    }
    if (Input.GetKeyUp(KeyCode.S))
    {
        anim.SetBool("IsWalkingBack", false);
    }
    if (Input.GetKeyUp(KeyCode.D))
    {
        anim.SetBool("IsWalkingRight", false);
    }
    if (Input.GetKeyUp(KeyCode.UpArrow))
    {
        anim.SetBool("IsWalkingForward", false);
    }
    if (Input.GetKeyUp(KeyCode.LeftArrow))
    {
        anim.SetBool("IsWalkingLeft", false);
    }
    if (Input.GetKeyUp(KeyCode.DownArrow))
    {
        anim.SetBool("IsWalkingBack", false);
    }
    if (Input.GetKeyUp(KeyCode.RightArrow))
    {
        anim.SetBool("IsWalkingRight", false);
    }
}

void OnCollisionStay()
{
    isGrounded = true;
}
void OnCollisionExit()
{
    isGrounded = false;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM