C# 玩家在移动unity 2d时口吃
每当玩家使用喷气背包或速度很快时,玩家就会结巴。我试着使用插值和其他类似的东西,但唯一的结果是更加口吃。如果有人知道口吃的原因是什么,请告诉我,如果你想,甚至可以解释你的答案:) 以下是我所说的球员口吃的意思: 这是我的播放器代码:C# 玩家在移动unity 2d时口吃,c#,unity3d,C#,Unity3d,每当玩家使用喷气背包或速度很快时,玩家就会结巴。我试着使用插值和其他类似的东西,但唯一的结果是更加口吃。如果有人知道口吃的原因是什么,请告诉我,如果你想,甚至可以解释你的答案:) 以下是我所说的球员口吃的意思: 这是我的播放器代码: using UnityEngine; using UnityEngine.SceneManagement; public class Player : MonoBehaviour { //basic variables [SerializeFie
using UnityEngine;
using UnityEngine.SceneManagement;
public class Player : MonoBehaviour
{
//basic variables
[SerializeField]
private float speed = 5f;
[SerializeField]
private float Jumpforce = 5f;
[SerializeField]
private float JetPackForce = 5f;
[SerializeField]
public bool canUseJetpack = true;
[SerializeField]
private Rigidbody2D rb;
[SerializeField]
private Animator animator;
private bool isFacingRight = true;
//runs when game starts
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
//runs every frame
void Update()
{
//applies force thus making player to move left or right
var move = Input.GetAxis("Horizontal");
transform.position = transform.position + new Vector3(move * speed * Time.deltaTime, 0, 0);
//changes player animation state
animator.SetFloat("Speed", Mathf.Abs(move));
//flip the player if facing right or left
if (move < 0 && isFacingRight)
{
flip();
}
else if (move > 0 && !isFacingRight)
{
flip();
}
//checks if the space key is pressed and that the player velocity on the y axis is smaller than 0.001
if(Input.GetKeyDown("space") && Mathf.Abs(rb.velocity.y) < 0.001f)
{
//adds force from below the player thus making the player jump
rb.AddForce(new Vector2(0, Jumpforce), ForceMode2D.Impulse);
}
//checks for the key 'Q' to be pressed and that there is enough fuel in the jetpack
if (Input.GetKey(KeyCode.Q) && canUseJetpack == true)
{
//ads force from below the player thus making the player fly using its jetpack
rb.AddForce(Vector2.up * JetPackForce);
//decreases the fuel in the jetpack
JetpackBar.instance.UseFuel(0.1f);
//makes the player run the using jetpack animation
animator.SetBool("UsingJetpack", true);
}
else
{
//if the player isn't using the jetpack, switch to the idle animation or the run animation depending if the player is moving or not
animator.SetBool("UsingJetpack", false);
}
//checks if the player health is less than 0
if (HealthBar.instance.currentHealth <= 0)
{
//if so, restart the game
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
//checks if someting collided with the player
private void OnCollisionEnter2D(Collision2D collision)
{
//if the thing collided with the player has a tag of 'Enemy'
if (collision.gameObject.CompareTag("Enemy"))
{
//then, make the player take damage
HealthBar.instance.takeDamage(25);
}
}
//flip the player
void flip()
{
isFacingRight = !isFacingRight;
transform.Rotate(0f, 180f, 0f);
}
}
使用UnityEngine;
使用UnityEngine.SceneManagement;
公共类玩家:单一行为
{
//基本变量
[序列化字段]
私人浮动速度=5f;
[序列化字段]
专用浮子跳线力=5f;
[序列化字段]
私人浮动JetPackForce=5f;
[序列化字段]
public bool canUseJetpack=true;
[序列化字段]
私有刚体2d rb;
[序列化字段]
私人动画师;
private bool isFacingRight=真;
//比赛开始时跑步
void Start()
{
rb=GetComponent();
}
//每帧运行一次
无效更新()
{
//施力使玩家向左或向右移动
var move=Input.GetAxis(“水平”);
transform.position=transform.position+新矢量3(移动*速度*时间增量,0,0);
//更改播放器动画状态
动画师.SetFloat(“速度”,Mathf.Abs(move));
//如果面向右侧或左侧,则翻转播放器
如果(移动<0&&isFacingRight)
{
翻转();
}
否则如果(移动>0&!isFacingRight)
{
翻转();
}
//检查是否按下空格键,y轴上的播放机速度是否小于0.001
if(输入GetKeyDown(“空格”)和数学Abs(rb.velocity.y)<0.001f)
{
//从球员下方增加力量,从而使球员跳跃
rb.AddForce(新矢量2(0,跳跃力),ForceMode2D.Pulse);
}
//检查是否按下“Q”键,以及jetpack中是否有足够的燃油
if(Input.GetKey(KeyCode.Q)和&canUseJetpack==true)
{
//广告从播放器下方强制播放,从而使播放器使用其喷气背包飞行
rb.AddForce(矢量2.up*JetPackForce);
//减少喷气背包中的燃油
JetpackBar.instance.UseFuel(0.1f);
//使用jetpack动画使播放器运行
animator.SetBool(“使用Jetpack”,真);
}
其他的
{
//如果播放机未使用jetpack,根据播放机是否移动,切换到空闲动画或运行动画
animator.SetBool(“使用Jetpack”,false);
}
//检查播放器的运行状况是否小于0
如果(HealthBar.instance.currentHealth首先,无论何时处理刚体,您都不希望使用Transform
组件移动对象。您只希望通过Rigidbody
/Rigidbody2D
组件移动对象
然后是一个普遍的问题。你可以根据输入或使用物理和力来移动你的对象。两者不可能同时进行,因为两者都变换。位置
或实际的“正确”将否决力,因此这两个系统一直在冲突
因此,我宁愿直接覆盖
void Update()
{
// Store the current velocity
var velocity = rb.velocity;
var move = Input.GetAxis("Horizontal");
// Only overwrite the X component
// We don't need Time.deltaTime since a velocity already
// moves the object over time
velocity.x = move * speed;
animator.SetFloat("Speed", Mathf.Abs(move));
if (move < 0 && isFacingRight)
{
flip();
}
else if (move > 0 && !isFacingRight)
{
flip();
}
if(Input.GetKeyDown("space") && Mathf.Abs(rb.velocity.y) < 0.001f)
{
// simply overwrite the Y velocity
// You'll have to adjust that value of course
// and might want to rename it ;)
velocity.y = Jumpforce;
}
if (Input.GetKey(KeyCode.Q) && canUseJetpack)
{
// Here we want to use Time.deltaTime since the jetpack
// shall increase the Y velocity over time
// again you'll have to adjust/rename that value
velocity.y += JetPackForce * Time.deltaTime;
// Also adjust this value!
// To be frame-rate independent you want to use Time.deltaTime here
JetpackBar.instance.UseFuel(0.1f * Time.deltaTime);
animator.SetBool("UsingJetpack", true);
}
else
{
animator.SetBool("UsingJetpack", false);
}
// After having calculated all changes in the velocity now set it again
rb.velocity = velocity;
if (HealthBar.instance.currentHealth <= 0)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
void Update()
{
//存储当前速度
var速度=rb速度;
var move=Input.GetAxis(“水平”);
//仅覆盖X组件
//我们不需要时间,因为已经有速度了
//随时间移动对象
速度x=移动*速度;
动画师.SetFloat(“速度”,Mathf.Abs(move));
如果(移动<0&&isFacingRight)
{
翻转();
}
否则如果(移动>0&!isFacingRight)
{
翻转();
}
if(输入GetKeyDown(“空格”)和数学Abs(rb.velocity.y)<0.001f)
{
//简单地覆盖Y速度
//当然,您必须调整该值
//并且可能要将其重命名;)
速度y=跳跃力;
}
if(Input.GetKey(KeyCode.Q)和canUseJetpack)
{
//在这里,我们想使用Time.deltaTime,因为喷气背包
//应随时间增加Y速度
//同样,您必须调整/重命名该值
速度.y+=喷气背包力*时间.deltaTime;
//还要调整此值!
//要独立于帧速率,请在此处使用Time.deltaTime
JetpackBar.instance.UseFuel(0.1f*Time.deltaTime);
animator.SetBool(“使用Jetpack”,真);
}
其他的
{
animator.SetBool(“使用Jetpack”,false);
}
//计算完速度的所有变化后,现在再次设置它
rb.速度=速度;
如果(HealthBar.instance.currentHealth)您是否尝试切换到FixedUpdate
而不是Update
?在Update()中执行物理操作
有时可能会有问题。请检查FixedUpdate
与Update
的详细分类。我曾尝试将更新切换到FixedUpdate,但这只会使一切正常。您是否仍在使用时间中的deltaTime
,或者是否已切换到使用更合适的IfJetpackBar
会随着时间的推移再生燃油,尝试使其在加油时不会一直尝试飞行。或者,当JetpackBar
耗尽燃油时,玩家必须释放jetpack键,jetpack才能重新工作。这两种情况之一可能会阻止您不断再生jetpack燃油和燃油使用它会立即产生口吃效果。除了fixedupdate中的第三行之外,我应该在其他任何地方使用Time.fixedDeltaTime吗?也没有什么好结果:(非常感谢你的回答,但是当你说要重命名它时,重命名“it”是什么意思?为什么我要重命名“it”?谢谢