Angelyatou/Endless_Unity_Projects: Unity Projects of Endlessdaydram (github.com)
https://github.com/Angelyatou/Endless_Unity_Projects
如何创建一个光滑的玩家
Physic Material 物理材质(动态摩擦力、静态摩擦力、弹力)
Animation
anim.SetBool("IsGround", isGround);
anim.SetTrigger("Change");
Unity富文本
unity中的富文本使用的是html5的格式,即在一对匹配的字符中包含需要富文本化的文字内容。
变<color=green><b> 色</b></color> 跑 酷
用Button切换场景
StartScence.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class StartScence : MonoBehaviour
{
public void OnBtnStart()
{
SceneManager.LoadScene(1);
}
}
(StartScence.cs挂在Canvas或Button都可以)
创建一个空物体,把StartScene脚本拖进去
把空物体拖到Button的Onclick事件下面,并修改Onclick事件
游戏结束显示结束面板
GameMode.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameMode : MonoBehaviour
{
Transform gameOverPanel;
public static GameMode Instance { get; private set; }
private void Awake()
{
Instance = this;
}
public void Start()
{
GameObject canvas = GameObject.Find("Canvas");
gameOverPanel = canvas.transform.Find("GameOverPanel");
gameOverPanel.gameObject.SetActive(false);
}
public void OnPlayerDie()
{
gameOverPanel.gameObject.SetActive(true);
}
public void OnRestart()
{
SceneManager.LoadScene("Game");
}
}
PlayerCharacter中调用
void PlayerDie()
{
gameObject.SetActive(false);
//播放爆炸粒子
Transform prefab = prefabDieParticleRed;
if(color == PlayerColor.Green)
{
prefab = prefabDieParticleGreen;
}
Transform p = Instantiate(prefabDieParticleRed,transform.position,Quaternion.identity);
Invoke("DelayPlayerDie", 1);
}
void DelayPlayerDie()
{
//调用GameMode的OnPlayerDie函数
GameMode.Instance.OnPlayerDie();
}
渲染光照
StartScence.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class StartScence : MonoBehaviour
{
public void OnBtnStart()
{
SceneManager.LoadScene(1);
}
}
GameMode.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameMode : MonoBehaviour
{
Transform gameOverPanel;
public static GameMode Instance { get; private set; }
private void Awake()
{
Instance = this;
}
public void Start()
{
GameObject canvas = GameObject.Find("Canvas");
gameOverPanel = canvas.transform.Find("GameOverPanel");
gameOverPanel.gameObject.SetActive(false);
}
public void OnPlayerDie()
{
gameOverPanel.gameObject.SetActive(true);
}
public void OnRestart()
{
SceneManager.LoadScene("Game");
}
}
PlayerCharacter.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum PlayerColor
{
Red,
Green,
}
public class PlayerCharacter : MonoBehaviour
{
//各种组件
Rigidbody rigid;
Animator anim;
Renderer render;
public float speed = 10;
public float jumpSpeed = 4.6f;
int jumpCount = 0;
bool isGround;
PlayerColor color = PlayerColor.Red;
public Transform prefabDieParticleRed;
public Transform prefabDieParticleGreen;
// Start is called before the first frame update
void Start()
{
rigid = GetComponent<Rigidbody>();
anim = GetComponent<Animator>();
render = GetComponentInChildren<Renderer>();
Debug.Log("render" + render);
}
// Update is called once per frame
public void Move(bool jump,bool changeColor)
{
Vector3 vel = rigid.velocity;
vel.z = speed;
if (jump && jumpCount < 10)
{
vel.y = jumpSpeed;
jumpCount++;
}
rigid.velocity = vel;
anim.SetBool("IsGround", isGround);
isGround = false;
if (changeColor)
{
ChangeColor();
}
}
void ChangeColor()
{
if(color == PlayerColor.Red)
{
color = PlayerColor.Green;
}
else
{
color = PlayerColor.Red;
}
if (color == PlayerColor.Red)
{
render.material.color = Color.red;
}
else
{
render.material.color = Color.green;
}
anim.SetTrigger("Change");
}
private void OnCollisionEnter(Collision collision)
{
string tag = collision.gameObject.tag;
if(tag == "Green"||tag == "Red")
{
jumpCount = 0;
isGround = true;
}
}
private void OnCollisionStay(Collision collision)
{
string tag = collision.gameObject.tag;
if (tag == "Green" || tag == "Red")
{
jumpCount = 0;
isGround = true;
}
if(color == PlayerColor.Green && tag == "Red")
{
PlayerDie();
}
else if(color == PlayerColor.Red && tag == "Green")
{
PlayerDie();
}
}
void PlayerDie()
{
gameObject.SetActive(false);
//播放爆炸粒子
Transform prefab = prefabDieParticleRed;
if(color == PlayerColor.Green)
{
prefab = prefabDieParticleGreen;
}
Transform p = Instantiate(prefabDieParticleRed,transform.position,Quaternion.identity);
Invoke("DelayPlayerDie", 1);
}
void DelayPlayerDie()
{
//调用GameMode的OnPlayerDie函数
GameMode.Instance.OnPlayerDie();
}
}
PlayerController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
// Start is called before the first frame update
bool jump;
bool changeColor;
PlayerCharacter cha;
private void Start()
{
cha = GetComponent<PlayerCharacter>();
}
void Update()
{
if (Input.GetButtonDown("Jump"))
{
jump = true;
}
if (Input.GetButtonDown("Fire2"))
{
changeColor = true;
}
}
private void FixedUpdate()
{
cha.Move(jump,changeColor);
jump = false;
changeColor = false;
}
}
FollowCam.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowCam : MonoBehaviour
{
// Start is called before the first frame update
public Transform target;
Vector3 offset;
void Start()
{
offset = transform.position - target.position;
}
// Update is called once per frame
void FixedUpdate()
{
Vector3 to = target.position + offset;
transform.position = Vector3.Lerp(transform.position, to, 0.3f);
}
}