关于#unity#的问题,请各位专家解答!(语言-c#)

按下ad键胶囊体上下跑,代码如下
public class PlayerMovement : MonoBehaviour
{
[Header("Movement")]
public float moveSpeed;

public float groundDrag;

public float jumpForce;
public float jumpCooldown;
public float airMultiplier;
bool readyToJump;

[Header("Keybinds")]
public KeyCode jumpKey = KeyCode.Space;

[Header("Ground Check")]
public float playerHeight;
public LayerMask whatIsGround;
bool grounded;

public Transform orientation;

float horizontalInput;
float verticalInput;

Vector3 moveDirection;

Rigidbody rb;

private void Start()
{
    rb = GetComponent<Rigidbody>();
    rb.freezeRotation = true;
}

private void Update()
{
    // ground check
    grounded = Physics.Raycast(transform.position,Vector3.down,playerHeight * 0.5f + 0.2f,whatIsGround);

    MyInput();
    SpeedControl();
    if(grounded)
    rb.drag = groundDrag;
    else
    rb.drag = 0;
}

private void FixedUpdate()
{
    MovePlayer();
}

private void MyInput()
{
    horizontalInput = Input.GetAxis("Horizontal");
    verticalInput = Input.GetAxis("Vertical");
    

    //when to jump
    if(Input.GetKey(jumpKey) && readyToJump && grounded)
    {
        readyToJump = false;

        Jump();

        Invoke(nameof(ResetJump), jumpCooldown);
    }
}

private void MovePlayer()
{
    // calculate movement direction
    moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;

    // on grand
    if(grounded)
        rb.AddForce(moveDirection.normalized * moveSpeed * 10f,ForceMode.Force);
    //in air
    else if(!grounded)
        rb.AddForce(moveDirection.normalized * moveSpeed * 10f *airMultiplier, ForceMode.Force);    
}

private void SpeedControl()
{
    Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);

    if(flatVel.magnitude > moveSpeed)
    {
        Vector3 limitedVel = flatVel.normalized * moveSpeed;
        rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z);
    }
}

private void Jump()
{
    //reset y velocity
    rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);

    rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
}
private void ResetJump()
{
    readyToJump = true;
}

}

放了一堆代码,问题是什么
你是想按下AD左右跑是吗
那么先看输入设置,ADWS到底对应的是什么,确定没有设置错了
再看你要控制的对象默认方向是什么
forward 是物体的前,跟你摄像机看到的前不是一个概念

  • 这篇博客: Unity数字孪生开发笔记——轿厢基本运动实现中的 2>普通C#动画类 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
    • 目前遇到问题----如何将同一个动画正播后停止在最后一帧,在过几秒钟后将该动画倒播回去
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    /// <summary>
    /// 动画行为类,提供有关动画的行为
    /// </summary>
    public class Animations 
    {
        //附加在敌人模型上的动画组件引用
        private Animation anim;
    
        /// <summary>
        /// 创建动画行为类
        /// </summary>
        /// <param name="anim">附加在敌人模型上的动画组件引用</param>
        public Animations(Animation anim)
        {
            this.anim = anim;
        }
    
        //播放动画
        public void Play(string animName)
        {
            //anim.CrossFade(animName);
            anim.Play(animName);
        }
    
        /// <summary>
        /// 判断指定动画是否正在播放
        /// </summary>
        /// <param name="animName">动画片段名称</param>
        /// <returns></returns>
        public bool IsPlaying(string animName)
        {
            return anim.IsPlaying(animName);
        }
    }