求教!跳跃代码报错

Ian Tsai 2020-08-28 04:02:26
求助!
原案例是基于Unity4的,我用的是Unity5
#70和#89行报错,原因是jumpMaterial没有定义。说实话这段代码我理解不了为什么要这样设计,求高人指点



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayController : MonoBehaviour {
[HideInInspector]
public bool isFacingRight = true;
[HideInInspector]
public bool isJumping = false;
[HideInInspector]
public bool isDoubleJumping = false;
[HideInInspector]
public bool isGrounded = false;

public float jumpForce = 650.0f;
public float maxSpeed = 7.0f;

public Transform groundCheck;
public LayerMask groundLayers;

private float groundCheckRadius = 0.2f;
private Animator anim;

void awake(){
anim = GetComponent<Animator> ();
}
// // Use this for initialization
// void Start () {
// }

// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Jump"))
{
if (isGrounded == true) {
this.GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, 0);
this.GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0, jumpForce));
this.anim.SetTrigger ("Jump");
} else if (isDoubleJumping == false) {
isDoubleJumping = true;
this.GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, 0);
this.GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0, jumpForce));
}


}
}

private void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle
(groundCheck.position, groundCheckRadius, groundLayers);

PhysicsMaterial2D material = this.gameObject.GetComponent<CircleCollider2D> ().sharedMaterial;

if(isGrounded==true)
{
isDoubleJumping=false;
}
if (isGrounded == true && material == this.jumpMaterial) {
CircleCollider2D collision = this.gameObject.GetComponent<CircleCollider2D> ();
collision.sharedMaterial = null;
collision.enabled = false;
collision.enabled = true;
}
else if(isGrounded ==false&&this.gameObject.GetComponent<CircleCollider2D>().sharedMaterial==null)
{
CircleCollider2D collision = this.gameObject.GetComponent<CircleCollider2D> ();
collision.sharedMaterial = this.jumpMaterial;
collision.enabled = false;
collision.enabled = true;
}


float move = Input.GetAxis ("Horizontal");
try
{
this.GetComponent<Rigidbody2D>().velocity = new Vector2 (move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
this.anim.SetFloat("Speed",Mathf.Abs(move));
if ((move > 0.0f && isFacingRight == false)|| (move < 0.0f && isFacingRight == true))
{
Flip ();
}
}
catch(UnityException error){
Debug.LogError (error.toString ());
}
}
void Flip()
{
isFacingRight = !isFacingRight;
Vector3 playerScale = transform.localScale;
playerScale.x = playerScale.x * -1;
transform.localScale = playerScale;
}
}
...全文
10582 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
带酒书生 2020-09-01
  • 打赏
  • 举报
回复
你就没定义jumpmaterial,肯定报错啊

2,535

社区成员

发帖
与我相关
我的任务
社区描述
Unity3D相关内容讨论专区
游戏unity 技术论坛(原bbs)
社区管理员
  • Unity3D
  • 芝麻粒儿
  • 「已注销」
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

Unity3D社区公告:

  1. 社区致力于解决各种Unity3D相关的“疑难杂症”。
  2. 社区不允许发布与Unity3D或相关技术无关内容。
  3. 社区版主邀请各位一道为打造优秀社区不懈努力。

试试用AI创作助手写篇文章吧