多个错误消息 C# 脚本统一.需要一些帮助(初级脚本编写器)



我从 2 天开始团结工作。我正在为2d精灵编写脚本。当我添加最后一个元素时,脚本不再工作。但我想做这项工作。有人可以查看错误消息,然后查看脚本以查看它出了什么问题。

错误1(25,38(:错误 CS1503:参数#2' cannot convert浮点"表达式键入"UnityEngine.Vector2">

错误 2: (25,38(: 错误 CS1502: 'UnityEngine.Physics2D.OverlapBox(UnityEngine.Vector2, UnityEngine.Vector2, float(' 的最佳重载方法匹配有一些无效参数


脚本:

using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float speed = 5f;
public float jumpSpeed = 8f;
private float movement = 0f;
private Rigidbody2D rigidBody;
public Transform groundCheckPoint;
public float groundCheckRadius;
public LayerMask groundLayer;
private bool isTouchingGround;

void Start()
{
rigidBody = GetComponent<Rigidbody2D> ();
}

void Update()
{
isTouchingGround = Physics2D.OverlapBox (groundCheckPoint.position, groundCheckRadius, groundLayer);
movement = Input.GetAxis ("Horizontal");
if (movement > 0f)
{
rigidBody.velocity = new Vector2 (movement * speed, rigidBody.velocity.y);
}
else if (movement < 0f)
{
rigidBody.velocity = new Vector2 (movement * speed, rigidBody.velocity.y);
}
else
{
rigidBody.velocity = new Vector2(0, rigidBody.velocity.y);
}
if (Input.GetButtonDown("Jump") && isTouchingGround)
{
rigidBody.velocity = new Vector2(rigidBody.velocity.x,jumpSpeed);
}
}
}
isTouchingGround = Physics2D.OverlapCircle(groundCheckPoint.position, groundCheckRadius, groundLayer);

如果你仍然想使用OverlapBox,这里是它的脚本API的链接(每当我遇到困难时,我个人都会来到这里(: Unity - 脚本API:Physics2D.OverlapBox

此外,我会将播放器跳转到 FixedUpdate 函数而不是更新函数的编码。下面看到的是我为游戏开发大赛制作的 2D 平台游戏中的玩家控制器脚本中的代码。希望这有帮助。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed;
public float jumpForce;
private float moveInput;
private Rigidbody2D rb;
private bool facingRight = true;
private bool isGrounded;
public Transform groundCheck;
public float checkedRadius;
public LayerMask whatIsGround;
private int extraJumps;

private void Start()
{
extraJumps = 1;
rb = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkedRadius, whatIsGround);
moveInput = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if (facingRight == false && moveInput > 0)
{
Flip();
}
else if (facingRight == true && moveInput < 0)
{
Flip();
}
}
private void Update()
{

if (isGrounded == true)
{
extraJumps = 2;
}
if (Input.GetKeyDown(KeyCode.W) && extraJumps > 0)
{
rb.velocity = Vector2.up * jumpForce;
extraJumps--;
}
else if (Input.GetKeyDown(KeyCode.W) && extraJumps == 0 && isGrounded == true)
{
rb.velocity = Vector2.up * jumpForce;
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
}

最新更新