C# Can';t在Unity中向播放器对象添加脚本
嘿,伙计们,我的剧本有点问题。我无法将2D游戏中的跳跃脚本添加到我的玩家。它总是说“无法添加脚本组件‘Player’,因为找不到脚本类。请确保没有编译错误,并且文件名和类名匹配。”。我找不到问题所在。Unity中的MonoBehavior类名和脚本名匹配,因此我需要帮助。 这是你感兴趣的剧本C# Can';t在Unity中向播放器对象添加脚本,c#,unity3d,C#,Unity3d,嘿,伙计们,我的剧本有点问题。我无法将2D游戏中的跳跃脚本添加到我的玩家。它总是说“无法添加脚本组件‘Player’,因为找不到脚本类。请确保没有编译错误,并且文件名和类名匹配。”。我找不到问题所在。Unity中的MonoBehavior类名和脚本名匹配,因此我需要帮助。 这是你感兴趣的剧本 using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : M
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public float gravity;
public Vector2 velocity;
public float maxXVelocity = 100;
public float maxAcceleration = 10;
public float acceleration = 10;
public float distance = 0;
public float jumpVelocity = 20;
public float groundHeight = -3;
public bool isGrounded = false;
public bool isHoldingJump = false;
public float maxHoldJumpTime = 0.4f;
public float maxMaxHoldJumpTime = 0.4f;
public float holdJumpTimer = 0.0f;
public float jumpGroundThreshold = 1;
public bool isDead = false;
void Start()
{
}
void Update()
{
Vector2 pos = transform.position;
float groundDistance = Mathf.Abs(pos.y - groundHeight);
if (isGrounded || groundDistance <= jumpGroundThreshold)
{
if (Input.GetKeyDown(KeyCode.Space))
{
isGrounded = false;
velocity.y = jumpVelocity;
isHoldingJump = true;
holdJumpTimer = 0;
}
}
if (Input.GetKeyUp(KeyCode.Space))
{
isHoldingJump = false;
}
}
private void FixedUpdate()
{
if (isDead)
{
return;
}
Vector2 pos = transform.position;
if (pos.y < -20)
{
isDead = true;
}
if (!isGrounded)
{
if (isHoldingJump)
{
holdJumpTimer += Time.fixedDeltaTime;
if (holdJumpTimer >= maxHoldJumpTime)
{
isHoldingJump = false;
}
}
pos.y += velocity.y * Time.fixedDeltaTime;
if (!isHoldingJump)
{
velocity.y += gravity * Time.fixedDeltaTime;
}
Vector2 rayOrigin = new Vector2(pos.x + 0.7f, pos.y);
Vector2 rayDirection = Vector2.up;
float rayDistance = velocity.y * Time.fixedDeltaTime;
RaycastHit2D hit2D = Physics2D.Raycast(rayOrigin, rayDirection, rayDistance);
if (hit2D.collider != null)
{
Ground ground = hit2D.collider.GetComponent<Ground>();
if (ground != null)
{
if (pos.y >= ground.groundHeight)
{
groundHeight = ground.groundHeight;
pos.y = groundHeight;
velocity.y = 0;
isGrounded = true;
}
}
}
Debug.DrawRay(rayOrigin, rayDirection * rayDistance, Color.red);
Vector2 wallOrigin = new Vector2(pos.x, pos.y);
Vector2 wallDir = Vector2.right;
RaycastHit2D wallHit = Physics2D.Raycast(wallOrigin, wallDir, velocity.x * Time.fixedDeltaTime);
if (wallHit.collider != null)
{
Ground ground = wallHit.collider.GetComponent<Ground>();
if (ground != null)
{
if (pos.y < ground.groundHeight)
{
velocity.x = 0;
}
}
}
}
distance += velocity.x * Time.fixedDeltaTime;
if (isGrounded)
{
float velocityRatio = velocity.x / maxXVelocity;
acceleration = maxAcceleration * (1 - velocityRatio);
maxHoldJumpTime = maxMaxHoldJumpTime * velocityRatio;
velocity.x += acceleration * Time.fixedDeltaTime;
if (velocity.x >= maxXVelocity)
{
velocity.x = maxXVelocity;
}
Vector2 rayOrigin = new Vector2(pos.x - 0.7f, pos.y);
Vector2 rayDirection = Vector2.up;
float rayDistance = velocity.y * Time.fixedDeltaTime;
RaycastHit2D hit2D = Physics2D.Raycast(rayOrigin, rayDirection, rayDistance);
if (hit2D.collider == null)
{
isGrounded = false;
}
Debug.DrawRay(rayOrigin, rayDirection * rayDistance, Color.yellow);
}
Vector2 obstOrigin = new Vector2(pos.x, pos.y);
RaycastHit2D obstHitX = Physics2D.Raycast(obstOrigin, Vector2.right, velocity.x * Time.fixedDeltaTime);
if (obstHitX.collider != null)
{
Obstacle obstacle = obstHitX.collider.GetComponent<Obstacle>();
if (obstacle != null)
{
hitObstacle(obstacle);
}
}
RaycastHit2D obstHitY = Physics2D.Raycast(obstOrigin, Vector2.up, velocity.y * Time.fixedDeltaTime);
if (obstHitY.collider != null)
{
Obstacle obstacle = obstHitY.collider.GetComponent<Obstacle>();
if (obstacle != null)
{
hitObstacle(obstacle);
}
}
transform.position = pos;
}
void hitObstacle(Obstacle obstacle)
{
Destroy(obstacle.gameObject);
velocity.x *= 0.7f;
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类玩家:单一行为
{
公众浮力;
公共矢量2速度;
公共浮点数maxXVelocity=100;
公共浮点数最大加速度=10;
公众浮子加速度=10;
公共浮动距离=0;
公共交通速度=20;
公众浮标地面高度=-3;
公共bool isGrounded=false;
公共bool isHoldingJump=false;
公共浮点maxHoldJumpTime=0.4f;
公共浮点MaxHoldJumpTime=0.4f;
公共浮点数保持跳跃计时器=0.0f;
公共浮点数阈值=1;
公共bool isDead=false;
void Start()
{
}
无效更新()
{
矢量2位置=变换位置;
浮动接地距离=数学绝对值(位置y-接地高度);
如果(Is接地| |接地距离=maxHoldJumpTime)
{
isHoldingJump=false;
}
}
位置y+=速度y*时间固定时间;
如果(!isHoldingJump)
{
速度y+=重力*时间固定时间;
}
Vector2光线原点=新Vector2(位置x+0.7f,位置y);
Vector2光线方向=Vector2.up;
浮动光线距离=速度.y*时间.fixedDeltaTime;
RaycastHit2D hit2D=Physics2D.Raycast(光线原点、光线方向、光线距离);
if(hit2D.collider!=null)
{
Ground=hit2D.collider.GetComponent();
如果(接地!=null)
{
如果(位置y>=地面高度)
{
地面高度=地面高度。地面高度;
位置y=地面高度;
速度y=0;
isGrounded=true;
}
}
}
DrawRay(光线原点,光线方向*光线距离,颜色.红色);
矢量2墙原点=新矢量2(位置x、位置y);
Vector2 wallDir=Vector2.right;
RaycastHit2D wallHit=Physics2D.Raycast(wallOrigin,wallDir,velocity.x*Time.FixedDelatime);
if(wallHit.collider!=null)
{
Ground=wallHit.collider.GetComponent();
如果(接地!=null)
{
如果(位置y<地面高度)
{
速度x=0;
}
}
}
}
距离+=速度x*时间固定时间;
如果(接地)
{
float velocityRatio=速度.x/maxx速度;
加速度=最大加速度*(1-速度比);
maxHoldJumpTime=maxMaxHoldJumpTime*velocityRatio;
速度x+=加速度*时间固定时间;
如果(速度.x>=最大速度)
{
速度x=最大速度x;
}
Vector2光线原点=新Vector2(位置x-0.7f,位置y);
Vector2光线方向=Vector2.up;
浮动光线距离=速度.y*时间.fixedDeltaTime;
RaycastHit2D hit2D=Physics2D.Raycast(光线原点、光线方向、光线距离);
if(hit2D.collider==null)
{
isfounded=false;
}
DrawRay(光线原点,光线方向*光线距离,颜色.黄色);
}
Vector2 obstOrigin=新Vector2(位置x,位置y);
RaycastHit2D obstHitX=Physics2D.Raycast(obstOrigin,Vector2.right,velocity.x*Time.fixedDelatime);
if(obstHitX.collider!=null)
{
障碍物=obstHitX.collider.GetComponent();
如果(障碍物!=null)
{
障碍;
}
}
RaycastHit2D obthity=Physics2D.Raycast(obstOrigin,Vector2.up,velocity.y*Time.fixedDelatime);
if(obthity.collider!=null)
{
障碍物=obthity.collider.GetComponent();
如果(障碍物!=null)
{
障碍;
}
}
transform.position=pos;
}
无效障碍物(障碍物)
{
摧毁(障碍物、游戏对象);
速度x*=0.7f;
}
}
如果您使用的是Windows,请确保它是pascal格式的。Windows有时会因为文件名的大小写而显得很时髦。用一个不同的名字重新命名,然后用pascal的大小写。哦,好吧,但是名字只有一个字:“Player”@Anton courmanschiyiy你需要确保Unity中的脚本与脚本中的类名称完全相同。如果您在创建后的任何时候更改了它,这就是它找不到它的原因。Windows有这样一个功能,浏览器会将名为player
的文件显示为player
,因此无法知道它的实际名称。是的,这对大家都有帮助,但是现在我有一个问题,当我尝试开始游戏时,会出现“在你进入游戏模式之前,所有的编译器错误都必须是错误的!”