c# - 团结 : player moves up without any such code

标签 c# animation unity-game-engine scripting

我正在为我的角色创建一个移动函数(我已经做过很多次了,但这次无法正常工作),这是我的代码:

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

[RequireComponent(typeof(CharacterController))]
[RequireComponent(typeof(Animator))]
public class Movement : MonoBehaviour {

    public CharacterController controller;
    public float moveSpeed;
    public float jumpForce;
    public float gravityScale;
    public Animator animator;

    private bool shiftPressed = false;
    private void Update()
    {
        if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
            shiftPressed = true;
        else
            shiftPressed = false;

        if(shiftPressed)
        {
            moveSpeed = 20f;
        } else
        {
            moveSpeed = 10f;
        }

        Vector3 moveDirection = new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed, 0.0f, Input.GetAxis("Vertical") * moveSpeed);

        if(Input.GetKey(KeyCode.Space) && controller.isGrounded)
        {
            moveDirection.y = jumpForce;
        }

        if (moveDirection != Vector3.zero)
            animator.SetFloat("Speed", moveSpeed);
        else if (moveDirection == Vector3.zero)
            animator.SetFloat("Speed", 0f);
        else if (moveDirection != Vector3.zero)
            animator.SetFloat("Speed", moveSpeed);

        if(moveDirection != Vector3.zero)
            transform.rotation = Quaternion.LookRotation(moveDirection);

        moveDirection = Camera.main.transform.TransformDirection(moveDirection);
        moveDirection.y =  moveDirection.y + (Physics.gravity.y * Time.deltaTime * gravityScale);
        controller.Move(moveDirection * Time.deltaTime);
    }
}

可以看到,除了跳转功能之外,没有任何可以在y轴上向上的功能。神秘的是,当我按下“S”键或“向下箭头”时,玩家会按照他应该的方式移动 -z 轴,但讽刺的是,他也在 +y 轴上移动。为了确保没有 y 轴函数,我尝试将跳转函数作为注释,但仍然采用相同的方式。我认为这可能是一些特定于角色的问题,所以我尝试将代码添加到立方体(认为这是我的动画错误),但它在任何时候都没有帮助。我确保角色 Controller 设置良好(碰撞器和其他东西);我已附上屏幕截图。 请帮忙! 提前致谢。

Screenshot

最佳答案

这个问题已经有几个人指出了,但从你的 react 来看,你似乎仍然不明白这个问题。

这句话总是会让你的角色向上移动。

moveDirection.y =  moveDirection.y + (Physics.gravity.y * Time.deltaTime * gravityScale);

您正在尝试将重力(一种力)与操纵变换结合使用。在 Unity 中,您要么执行两者之一,而不是两者都执行。这将导致不期望的且难以修复的结果。

如果您想在代码中使用强制,那么我建议如下:

  1. 为您的角色添加一个 RigidBody。选中“使用重力”复选框。

  2. 通过调用获取 Controller 脚本中的 RigidBody,并向其施加力以移动它。

    var rb = getComponent<RigidBody>();
    rb.AddForce(10f);
    

请注意,如果添加强制,可以在 Update 方法中连续添加,或者通过传递第二个参数“forcemode”仅添加一次。

rb.AddForce(10, ForceMode.Impulse); // add instant force, using mass
// Or
rb.AddForce(10, ForceMode.VelocityChange); // add instant force, ignoring mass
// Or
rb.AddForce(10, ForceMode.Force); // add continuous force, using mass
// Or
rb.AddForce(10, ForceMode.Acceleration); // add continous force, ignoring mass

关于c# - 团结 : player moves up without any such code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50929766/

相关文章:

css - 如何在 unity3d 上加载 png 图像并使用加载场景

unity-game-engine - 忽略特定情况下的碰撞,同时仍将其用作触发器

C# WPF MVVM ItemSource 未附加列表

javascript - 如何平滑setInterval动画?

c# - Android System.DateTime.now 的 Unity 系统时间不起作用?

Javascript - Buggy SlideToggle 效果(无 jQuery)

ios - 为什么这个简单的不透明动画在 SwiftUI 中不起作用?

c# - 自修改 C# (MSIL) 代码?

javascript - 调用脚本后如何获取页面的 html 源代码?

c# - 将标签内容绑定(bind)到嵌套类中的值而不是 Datacontext