【开发日志】2022.09.25 Unity变色龙跑酷自制游戏详解

Angelyatou/Endless_Unity_Projects: Unity Projects of Endlessdaydram (github.com)icon-default.png?t=M85Bhttps://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);
    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity 5.x是一种十分流行的跨平台游戏开发引擎,它提供了丰富的工具和功能来帮助开发者创建精美、高度可玩性的3D游戏。在Unity 5.x中,开发者可以通过使用C#或UnityScript编写脚本,创建游戏对象、添加组件、定义动画,以及实现游戏逻辑。 Unity 5.x的3D游戏开发技术详解主要包括以下几个方面: 1. 场景管理:Unity 5.x提供了简化的场景编辑器,可以轻松地创建和编辑游戏场景。开发者可以将3D模型、贴图、音频和UI元素等资源导入到场景中,然后使用可视化的工具调整其属性和位置。 2. 游戏物体和组件:Unity 5.x中的游戏物体可以通过添加不同的组件来完成各种功能,比如渲染器组件用于呈现游戏物体的外观,刚体组件用于模拟物理效果,碰撞器组件用于检测碰撞等。 3. 脚本编程:Unity 5.x使用C#或UnityScript编写脚本,开发者可以通过脚本控制游戏物体的行为和逻辑。脚本可以通过获取输入、运算、条件判断和函数调用等方式实现各种功能,比如移动、攻击、触发事件等。 4. 动画和特效:Unity 5.x提供了强大的动画和特效系统,开发者可以使用它们来创建逼真的角色动画、粒子效果和光影效果等。开发者可以通过关键帧动画、状态机或物理模拟等方法实现各种复杂的动画效果。 典型案例源码下载:Unity 5.x引擎的官方网站和社区论坛上有许多典型案例的源码共享,开发者可以从中学习和借鉴。此外,一些知名的游戏开发机构也会在其官方网站上发布其开发游戏的源码,供开发者下载和学习。 总结来说,Unity 5.x是一款功能丰富的3D游戏开发引擎,通过学习其技术和分析典型案例源码,开发者可以快速掌握开发3D游戏所需的知识和技能,并将其应用到自己的游戏项目中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值