unity2D中c#脚本调用rigidbody2d时报错,求大神告知!!!
如图,每次调用都会报如下错误代码如下;voidFixedUpdate(){//Cachethehorizontalinput.floath=Input.GetAxis("...
如图,每次调用都会报如下错误
代码如下;
void FixedUpdate ()
{
// Cache the horizontal input.
float h = Input.GetAxis("Horizontal");
if(h * Rigidbody2D.velocity.x < maxSpeed)
Rigidbody2D.AddForce(Vector2.right * h * moveForce);
if(Mathf.Abs(Rigidbody2D.velocity.x) > maxSpeed)
Rigidbody2D.velocity = new Vector2(Mathf.Sign(Rigidbody2D.velocity.x) * maxSpeed, Rigidbody2D.velocity.y);
// If the player should jump...
if(jump)
{
Rigidbody2D.AddForce(new Vector2(0f, jumpForce));
jump = false;
}
} 展开
代码如下;
void FixedUpdate ()
{
// Cache the horizontal input.
float h = Input.GetAxis("Horizontal");
if(h * Rigidbody2D.velocity.x < maxSpeed)
Rigidbody2D.AddForce(Vector2.right * h * moveForce);
if(Mathf.Abs(Rigidbody2D.velocity.x) > maxSpeed)
Rigidbody2D.velocity = new Vector2(Mathf.Sign(Rigidbody2D.velocity.x) * maxSpeed, Rigidbody2D.velocity.y);
// If the player should jump...
if(jump)
{
Rigidbody2D.AddForce(new Vector2(0f, jumpForce));
jump = false;
}
} 展开
1个回答
展开全部
楼楼我会了,是unity版本的问题,你把其中两句改成
if(Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x) > maxSpeed)
GetComponent<Rigidbody2D>().velocity = new Vector2(Mathf.Sign(GetComponent<Rigidbody2D>().velocity.x) * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
就可以了
if(Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x) > maxSpeed)
GetComponent<Rigidbody2D>().velocity = new Vector2(Mathf.Sign(GetComponent<Rigidbody2D>().velocity.x) * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
就可以了
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询