c# - 统一更改按钮按下时的游戏对象速度
问题描述
因此,我正在尝试将破折号机制添加到正在构建的游戏角色中。但是由于某种原因,我无法让游戏对象的速度真正改变。我尝试使用有效的 addForce,但我必须添加很多才能获得所需的效果,并且有时表现得很奇怪!
我是否需要对游戏对象的速度做任何比我已经做的事情?
这是我的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float dashSpeed;
private int dashDirection;
private float dashCoolDown;
public float startDashCoolDown;
private float dashTime;
public float startDashTime;
public GameObject dashEffect;
public float speed;
public float jumpForce;
private float moveInput;
private Rigidbody2D rb;
private bool isFacingRight = true;
private Animator anim;
private bool isGrounded;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
private int extraJumps;
public int extraJumpsValue;
public GameObject dustEffect;
public GameObject trailEffect;
private void Awake()
{
// Setting up references.
isGrounded = transform.Find("GroundCheck");
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
}
void Start()
{
extraJumps = extraJumpsValue;
rb = GetComponent<Rigidbody2D>();
dashTime = startDashTime;
dashCoolDown = startDashCoolDown;
}
void FixedUpdate()
{
isGrounded = false;
// Check to see if grounded
Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheck.position, checkRadius, whatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
{
isGrounded = true;
anim.SetBool("Ground", isGrounded);
}
}
// Check if movement is allowed
if (!GameMaster.disableMovement)
{
// Move character
moveInput = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
Instantiate(trailEffect, groundCheck.position, Quaternion.identity);
anim.SetFloat("Speed", Mathf.Abs(moveInput));
}
// Flip character
if (isFacingRight == false && moveInput > 0)
{
Flip();
}
else if (isFacingRight == true && moveInput < 0)
{
Flip();
}
}
private void Update()
{
// Check if the player is grounded
if (isGrounded == true)
{
extraJumps = extraJumpsValue;
}
// Check if movement is allowed
if (!GameMaster.disableMovement)
{
// Check for jump
// If the player has more than one jump available
if (Input.GetKeyDown(KeyCode.Space) && extraJumps > 0)
{
isGrounded = false;
anim.SetBool("Ground", isGrounded);
rb.velocity = Vector2.up * jumpForce;
extraJumps--;
Instantiate(dustEffect, groundCheck.position, Quaternion.identity);
}
// If the player only has one jump available
if (Input.GetKeyDown(KeyCode.Space) && extraJumps == 0 && isGrounded == true)
{
isGrounded = false;
anim.SetBool("Ground", isGrounded);
rb.velocity = Vector2.up * jumpForce;
}
// Check for dash
if (dashCoolDown <= 0)
{
if (Input.GetKeyDown(KeyCode.LeftShift))
{
anim.SetBool("Dash", true);
Dash();
dashTime = startDashTime;
}
}
else
{
dashCoolDown -= Time.deltaTime;
}
}
}
void Dash()
{
if (dashTime <= 0)
{
dashCoolDown = startDashCoolDown;
anim.SetBool("Dash", false);
}
else
{
dashTime -= Time.deltaTime;
if (isFacingRight)
{
rb.velocity = Vector2.right * dashSpeed;
}
else if (!isFacingRight)
{
rb.velocity = Vector2.left * dashSpeed;
}
}
}
void Flip()
{
isFacingRight = !isFacingRight;
// Multiply the player's x local scale by -1.
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
解决方案
如果您没有通过 Rigidbody.velocity 获得所需的破折号效果,您可以尝试使用 Rigidbody.addForce()。我知道您说过您已经使用过它,但是您可以在此处应用多种不同的力模式。我建议使用这个,因为它从一开始就全力以赴,然后将其放下。您可以在此处自行阅读更多相关信息。
所以你可以像这样修改你的代码:
void Dash()
{
if (isFacingRight)
{
rb.AddForce(Vector2.right*dashSpeed, ForceMode.VelocityChange);
}
else if (!isFacingRight)
{
rb.AddForce(Vector2.left*dashSpeed, ForceMode.VelocityChange);
}
}
请注意,您也不需要反复调用 dash 来衰减速度。这个函数现在应该只在按下破折号(shift)按钮时调用。
希望这有帮助!
推荐阅读
- javascript - 是否可以将 html 内容分开以使其更容易附加?
- java - 是什么导致 OpenSplice 中出现此 Java 错误?
- c - 如何在 Visual Studio (Mac) 中运行 C 程序
- node.js - 如何将发送到指定频道的照片复制到另一个频道不和谐
- python - 当我第一次访问该页面时, .is_valid 返回 false
- ios - MKMapView 在交互模式解除时冻结
- kubernetes - 服务器选择在 30000 毫秒后超时 {"name":"MongooseTimeoutError","reason":{"name":"MongoNetworkError"}}
- c++ - 我有一个错误,我不确定它为什么不工作(线程)
- ruby-on-rails - Hyperloop 可以使用最新的 Async/Await 吗?
- c# - 一段时间后,随机数生成器给出相同的数字