C# Unity 5从Unity 4.6升级为脚本停止工作
当我使用Unity 4.6时,下面的代码工作得非常好C# Unity 5从Unity 4.6升级为脚本停止工作,c#,unity3d,C#,Unity3d,当我使用Unity 4.6时,下面的代码工作得非常好 using UnityEngine; using System.Collections; public class HadesController : MonoBehaviour { public float maxSpeed = 10f; bool facingRight = true; Animator anim; bool grounded = false; public Transform groundCheck; float g
using UnityEngine;
using System.Collections;
public class HadesController : MonoBehaviour {
public float maxSpeed = 10f;
bool facingRight = true;
Animator anim;
bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask WhatIsGround;
public float jumpForce = 700f;
void Start ()
{
anim = GetComponent<Animator>();
}
void FixedUpdate ()
{
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, WhatIsGround);
anim.SetBool ("Ground", grounded);
anim.SetFloat ("vSpeed", GetComponent<Rigidbody2D>().velocity.y);
if (!grounded)
return;
float move = Input.GetAxis ("Horizontal");
anim.SetFloat("Speed", Mathf.Abs (move));
GetComponent<Rigidbody2D>().velocity = new Vector2 (move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
if (move > 0 &&!facingRight)
Flip ();
else if (move <0 && facingRight)
Flip ();
}
void Update()
{
if (grounded && Input.GetKeyDown (KeyCode.Space))
{
anim.SetBool("Ground", false);
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce));
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
使用UnityEngine;
使用系统集合;
公共类HadesController:MonoBehavior{
公共浮点数maxSpeed=10f;
bool facingRight=正确;
动画师;
bool-grounded=false;
公开审查;
浮地半径=0.2f;
公共层码头;
公共浮子力=700f;
无效开始()
{
anim=GetComponent();
}
无效固定更新()
{
接地=物理2D.Overlap圆(接地检查位置、接地半径、WhatIsGround);
动画设置工具(“地面”,接地);
anim.SetFloat(“vSpeed”,GetComponent().velocity.y);
如果(!接地)
回来
浮动移动=Input.GetAxis(“水平”);
动画设置浮动(“速度”,数学Abs(移动));
GetComponent().velocity=新矢量2(移动*maxSpeed,GetComponent().velocity.y);
如果(移动>0&!朝向右侧)
翻转();
else if(move这是一个直接的错误。您只需要看到异常的这一部分:
您可能需要在inspector中指定HadesController脚本的groundCheck变量
也就是说,分配给groundCheck的转换不知何故丢失,现在groundCheck为空。您应该重新分配它。只需将以前分配的转换(或游戏对象)再次拖放到groundCheck in inspector
在错误行之前添加调试检查,您应该查看它是否为null:
Log(“groundCheck为null:+(groundCheck==null))
请先阅读标签说明,然后再将其添加到问题中。!=从例外情况来看,您的地面检查字段的检查器似乎没有设置任何内容。仅凭您发布的内容很难判断。