using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private Rigidbody2D playerRB;
// Movement Speed and Input Variable
public float speed = 10f;
private float horizontalInput;
// Jump Strength and ground check
public Vector2 jumpHeight;
public bool isOnGround = true;
// Max numbers of jumps
public int maxJumps = 2;
public int Jumps;
// Start is called before the first frame update
void Start()
{
playerRB = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
horizontalInput = Input.GetAxis("Horizontal");
transform.Translate(Vector2.right * Time.deltaTime * speed * horizontalInput);
if(Input.GetKeyDown(KeyCode.Space) && isOnGround)
{
playerRB.AddForce(jumpHeight, ForceMode2D.Impulse);
isOnGround= false;
this.Jump();
}
}
private void Jump()
{
if (Jumps > 0)
{
playerRB.AddForce(new Vector2(0f, jumpHeight), ForceMode2D.Impulse);
isOnGround = false;
Jumps = Jumps - 1;
}
if (Jumps == 0)
{
return;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
Jumps = maxJumps;
isOnGround = true;
}
}
}
我还是 Unity 和 C# 的新手。我想让我的角色在 2d 环境中使用这个脚本进行双跳。但是我得到的是“错误 CS1503 无法将 UnityEngine.Vector2 形式转换为浮点数?”我将如何解决这个问题?并防止它在未来发生。