2,535
社区成员
发帖
与我相关
我的任务
分享
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;
}
}