我想添加一个c#脚本,它将帮助我向我移动的方向冲刺,只使用'空格'键。以下是我的角色的脚本
你的脚本应该是这样的,试试这个。我加了一个bool,当你按空格键时,这个bool就会变成true... ...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PinkCharacter : MonoBehaviour
{
Vector3 MoveDirection;
float moveSpeed = 5;
float dashSpeed = 10;
bool canDash;
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.A))
{
MoveDirection = Vector3.left;
}
if (Input.GetKey(KeyCode.D))
{
MoveDirection = Vector3.right;
}
if (Input.GetKey(KeyCode.W))
{
MoveDirection = Vector3.up;
}
if (Input.GetKey(KeyCode.S))
{
MoveDirection = Vector3.down;
}
canDash = Input.GetKey(KeyCode.Space);
if (canDash)
transform.position += MoveDirection * dashSpeed * Time.deltaTime;
else
transform.position += MoveDirection * moveSpeed * Time.deltaTime;
}
}
或者... [这一项有一个简化的 Update
方法]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PinkCharacter : MonoBehaviour
{
Vector3 MoveDirection;
float moveSpeed = 5;
bool canDash;
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.A))
{
MoveDirection = Vector3.left;
}
if (Input.GetKey(KeyCode.D))
{
MoveDirection = Vector3.right;
}
if (Input.GetKey(KeyCode.W))
{
MoveDirection = Vector3.up;
}
if (Input.GetKey(KeyCode.S))
{
MoveDirection = Vector3.down;
}
canDash = Input.GetKey(KeyCode.Space);
moveSpeed = canDash ? 10 : 5;
transform.position += MoveDirection * moveSpeed * Time.deltaTime;
}
}