Unity3d 角色无缘无故地向后行走(Brackeys Unity中的第一人称角色控制器)
我使用Brackeys中的第一人称角色控制器脚本。但有时,我的角色在没有按下按钮的情况下向后行走。我试图在项目设置中更改角色控制器的值和轴的值,但没有任何实际帮助Unity3d 角色无缘无故地向后行走(Brackeys Unity中的第一人称角色控制器),unity3d,Unity3d,我使用Brackeys中的第一人称角色控制器脚本。但有时,我的角色在没有按下按钮的情况下向后行走。我试图在项目设置中更改角色控制器的值和轴的值,但没有任何实际帮助 using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovementScript : MonoBehaviour { public CharacterController contr
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovementScript : MonoBehaviour
{
public CharacterController controller;
public Transform groundcheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
public float speed = 12f;
public float gravity = -9.81f;
public float jumpheight = 3f;
Vector3 velocity;
bool isGrounded;
void Start()
{
}
void Update()
{
isGrounded = Physics.CheckSphere(groundcheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y += Mathf.Sqrt(jumpheight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类PlayerMovementScript:MonoBehavior
{
公共字符控制器;
公开审查;
公共浮地距离=0.4f;
公共层掩码地面掩码;
公共浮子速度=12f;
公共浮子重力=-9.81f;
公共浮子跳线高度=3f;
矢量3速度;
布尔被禁足;
void Start()
{
}
无效更新()
{
isground=physical.CheckSphere(groundcheck.position、groundDistance、groundMask);
如果(isGrounded&&velocity.y<0)
{
速度y=-2f;
}
float x=Input.GetAxis(“水平”);
float z=Input.GetAxis(“垂直”);
Vector3 move=transform.right*x+transform.forward*z;
控制器。移动(移动*速度*时间。延迟时间);
if(Input.GetButtonDown(“跳转”)和&isground)
{
速度y+=数学Sqrt(跳跃高度*-2f*重力);
}
速度.y+=重力*时间增量;
控制器。移动(速度*时间。增量时间);
}
}
我真的不知道问题出在哪里,但也许你可以帮我我看剧本没有什么问题。某些东西必须在脚本之外提供垂直输入 如果您使用的是控制器,您的左操纵杆可能会出现一些漂移问题。在运动矢量中添加一些可以防止任何非自愿运动的发生。例如,您可以检查您的输入是否低于某个阈值,如果是这样,则将其设置为零。 下面是一个简单的方法(在本例中,阈值为0.125。如果操纵杆漂移仍然提供输入,请尝试更高的值):
float x=Input.GetAxis(“水平”);
float z=Input.GetAxis(“垂直”);
如果(x有点晚,但我也有同样的问题。
导致它的原因是除了鼠标和键盘之外还插入了另一个控制器,在我的例子中是一个方向盘。因此,如果你只想使用它的y
组件,Debug.Log(z),请确保所有额外的控制器都被拔下velocity
向量有什么用
给你?很抱歉回答晚了。Debug.Log(z)给我值-1,如果我向后走,听起来好像有什么东西在触发你的输入.GetAxis(“垂直”)
始终为负值…我检查了所有脚本,但没有发现任何东西可以触发我的垂直轴。是否有一个轴的设置可以触发此设置?或者可能是一个组件调整不正确?
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
if(x <= 0.125f)
x = 0
if(y <= 0.125f)
y = 0