csharp JumpGame
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp JumpGame相关的知识,希望对你有一定的参考价值。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ActionController : StateMachineBehaviour
{
GameObject player;
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
if (stateInfo.IsName("Walk"))
{
player = GameObject.Find("Player");
player.GetComponent<PlayerController>().speed = 2.0f;
}
if(stateInfo.IsName("Jump"))
{
player = GameObject.Find("Player");
player.GetComponent<PlayerController>().jumpEnd = false;
}
}
override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
if (stateInfo.IsName("Jump"))
{
player.GetComponent<PlayerController>().jumpEnd = true;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameDirector : MonoBehaviour {
GameObject player;
GameObject mainCamera;
GameObject gameOver;
GameObject RightJumpButton;
GameObject LeftJumpButton;
GameObject hp1;
GameObject hp2;
GameObject hp3;
GameObject jumpButton;
private Color Gray = new Color(152.0f / 255.0f, 152.0f / 255.0f, 152.0f / 255.0f, 255.0f / 255.0f);
void Start () {
player = GameObject.Find("Player");
mainCamera = Camera.main.gameObject;
gameOver = GameObject.Find("GameOver");
RightJumpButton = GameObject.Find("RightJumpButton");
LeftJumpButton = GameObject.Find("LeftJumpButton");
hp1 = GameObject.Find("HP1");
hp2 = GameObject.Find("HP2");
hp3 = GameObject.Find("HP3");
jumpButton = GameObject.Find("JumpButton");
}
void Update () {
PlayerController playerCont = player.GetComponent<PlayerController>();
//playerが落下してカメラから見切れると全HPをグレーにする(GAME OVER)
if(player.activeSelf == false)
{
gameOver.GetComponent<Text>().enabled = true;
hp1.GetComponent<Image>().color = Gray;
hp2.GetComponent<Image>().color = Gray;
hp3.GetComponent<Image>().color = Gray;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FrontLiftController : MonoBehaviour {
BoxCollider LiftCollider;
GameObject player;
void Start () {
LiftCollider = GetComponent<BoxCollider>();
player = GameObject.Find("Player");
}
void Update () {
PlayerController playerCont = player.GetComponent<PlayerController>();
//ジャンプ時はリフトのcolliderをOFFして下から貫通可能にして、着地時にcolliderをONにする
if (playerCont.jumpEnd == true)
{
LiftCollider.enabled = true;
}
if (playerCont.jumpEnd == false)
{
LiftCollider.enabled = false;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LiftController : MonoBehaviour
{
BoxCollider LiftCollider;
GameObject player;
GameObject mainCamera;
GameObject score;
public int lift_count = 0;
void Start()
{
LiftCollider = GetComponent<BoxCollider>();
player = GameObject.Find("Player");
mainCamera = Camera.main.gameObject;
score = GameObject.Find("Score2");
}
void Update()
{
float liftHeight = transform.position.y;
float cameraHeight = mainCamera.transform.position.y;
PlayerController playerCont = player.GetComponent<PlayerController>();
//ジャンプ時はリフトのcolliderをOFFして下から貫通可能にして、着地時にcolliderをONにする
if (playerCont.jumpEnd == true)
{
LiftCollider.enabled = true;
}
if (playerCont.jumpEnd == false)
{
LiftCollider.enabled = false;
}
//リフトがカメラから見切れたら上部に移動し、Ⅹ軸はランダムで配置
if (liftHeight < cameraHeight - 1.7f)
{
float liftPosX = transform.position.x;
int number1 = Random.Range(0, 7);
int number2 = Random.Range(0, 2);
switch (number1)
{
case 0:
liftPosX = -2.75f;
break;
case 1:
liftPosX = -2;
break;
case 2:
liftPosX = -1;
break;
case 3:
liftPosX = 0f;
break;
case 4:
liftPosX = 1f;
break;
case 5:
liftPosX = 2f;
break;
case 6:
liftPosX = 2.75f;
break;
}
float liftPosY = cameraHeight + 4.5f;
transform.position = new Vector3(liftPosX, liftPosY, transform.position.z);
}
if(playerCont.OnLift == true)
{
lift_count++;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
GameObject player;
float cameraHeight;
void Start()
{
player = GameObject.Find("Player");
}
void Update()
{
float playerHeight = player.transform.position.y + 1.5f;
float cameraHeight = transform.position.y;
float newHeight = Mathf.Lerp(cameraHeight, playerHeight, 0.05f);
PlayerController playerCont = player.GetComponent<PlayerController>();
//playerがカメラの高さを超えて、リフトに着地したらカメラ移動
if(playerHeight > cameraHeight && playerCont.OnLift == true)
{
transform.position = new Vector3(transform.position.x, newHeight, transform.position.z);
}
//playerがカメラから見切れたら消す
if(playerHeight + 1.0f < cameraHeight)
{
player.SetActive(false);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class JumpButtonController : MonoBehaviour {
GameObject player;
Button jumpButton;
void Start () {
player = GameObject.Find("Player");
jumpButton = GameObject.Find("Canvas/JumpButton").GetComponent<Button>();
}
void Update () {
PlayerController playerCont = player.GetComponent<PlayerController>();
//空中でのジャンプを禁止(ジャンプボタンの非アクティブ化)
if (playerCont.isGrounded == false && playerCont.OnLift == false)
{
jumpButton.interactable = false;
}
else
{
jumpButton.interactable = true;
}
if (playerCont.OnLift == true)
{
jumpButton.interactable = true;
}
}
public void OnClick()
{
player.GetComponent<PlayerController>().Jump();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour {
Animator animator;
Rigidbody rb;
public float speed = 2.0f;
public bool isGrounded = true;
public bool OnLift = false;
public bool jumpEnd = false;
public int liftCount = 0;
public bool cameraMove = false;
public bool RMove_flag = false;
public bool LMove_flag = false;
Button JumpButton;
Button RightButton;
Button LeftButton;
GameObject director;
void Start () {
animator = GetComponent<Animator>();
rb = GetComponent<Rigidbody>();
JumpButton = GameObject.Find("Canvas/JumpButton").GetComponent<Button>();
RightButton = GameObject.Find("Canvas/RightButton").GetComponent<Button>();
LeftButton = GameObject.Find("Canvas/LeftButton").GetComponent<Button>();
director = GameObject.Find("GameDirector");
}
void Update () {
if(RMove_flag == true)
{
RightMove();
}
if(LMove_flag == true)
{
LeftMove();
}
}
public void Jump()
{
rb.AddForce(Vector3.up * 350);
animator.SetTrigger("IsJump");
isGrounded = false;
OnLift = false;
cameraMove = true;
}
public void RightPushDown()
{
RMove_flag = true;
animator.SetBool("IsWalk", true);
LeftButton.interactable = false; //右に移動中は左ボタン非アクティブ
}
public void RightPushUp()
{
RMove_flag = false;
animator.SetBool("IsWalk", false);
LeftButton.interactable = true;
}
public void LeftPushDown()
{
LMove_flag = true;
animator.SetBool("IsWalk", true);
RightButton.interactable = false; //左に移動中は右ボタン非アクティブ
}
public void LeftPushUp()
{
LMove_flag = false;
animator.SetBool("IsWalk", false);
RightButton.interactable = true;
}
public void RightMove()
{
transform.position += transform.forward * Time.deltaTime * speed;
transform.rotation = Quaternion.Euler(0, 90, 0);
animator.SetBool("IsWalk", true);
}
public void LeftMove()
{
transform.position += transform.forward * Time.deltaTime * speed;
transform.rotation = Quaternion.Euler(0, 270, 0);
animator.SetTrigger("IsWalk");
}
private void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Ground")
{
isGrounded = true;
}
if(col.gameObject.tag == "OnLift")
{
OnLift = true;
liftCount++;
cameraMove = false;
}
}
private void OnCollisionExit(Collision col)
{
if(col.gameObject.tag == "Ground")
{
isGrounded = false;
}
if(col.gameObject.tag == "OnLift")
{
OnLift = false;
}
}
}
以上是关于csharp JumpGame的主要内容,如果未能解决你的问题,请参考以下文章