有 hotcontrol 时不应捕获 - Unity
Should not be capturing when there is a hotcontrol - Unity
我对 Unity 和 C# 还很陌生。我已经知道 python,所以 C# 很容易掌握,但现在我遇到了问题。我无法 运行 此代码。它导致出现错误。我多次检查代码,但找不到问题所在。这是代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private Rigidbody2D rb2D;
private float moveSpeed;
private float moveHorizontal;
private float moveVertical;
// Start is called before the first frame update
void Start()
{
moveSpeed = 3f;
rb2D = gameObject.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
}
void FixedUpdate()
{
moveHorizontal = Input.GetAxisRaw("Horizontal");
if (moveHorizontal > 0.1f || moveHorizontal < -0.1f)
{
rb2D.AddForce(new Vector2(moveHorizontal * moveSpeed), ForceMode2D.Impulse);
}
}
}
地板(白色矩形)是一个刚体和盒子对撞机。玩家(红色方块)也是如此。我想用键盘移动方块。
任何帮助将不胜感激
问题在这里:
rb2D.AddForce(new Vector2(moveHorizontal * moveSpeed), ForceMode2D.Impulse);
Vector2 将 2 个浮点数作为输入。要修复它,您必须这样做
new Vector2(moveHorizontal * moveSpeed, moveVertical * moveSpeed)
您忘记定义 vector2 中的垂直移动
我对 Unity 和 C# 还很陌生。我已经知道 python,所以 C# 很容易掌握,但现在我遇到了问题。我无法 运行 此代码。它导致出现错误。我多次检查代码,但找不到问题所在。这是代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private Rigidbody2D rb2D;
private float moveSpeed;
private float moveHorizontal;
private float moveVertical;
// Start is called before the first frame update
void Start()
{
moveSpeed = 3f;
rb2D = gameObject.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
}
void FixedUpdate()
{
moveHorizontal = Input.GetAxisRaw("Horizontal");
if (moveHorizontal > 0.1f || moveHorizontal < -0.1f)
{
rb2D.AddForce(new Vector2(moveHorizontal * moveSpeed), ForceMode2D.Impulse);
}
}
}
地板(白色矩形)是一个刚体和盒子对撞机。玩家(红色方块)也是如此。我想用键盘移动方块。
任何帮助将不胜感激
问题在这里:
rb2D.AddForce(new Vector2(moveHorizontal * moveSpeed), ForceMode2D.Impulse);
Vector2 将 2 个浮点数作为输入。要修复它,您必须这样做
new Vector2(moveHorizontal * moveSpeed, moveVertical * moveSpeed)
您忘记定义 vector2 中的垂直移动