学习目标:
简单实现相机跟随Player功能
参考视频:秦无邪OvO的个人空间_哔哩哔哩_Bilibili秦无邪OvO,独立游戏开发者/美术/编曲;秦无邪OvO的主页、动态、视频、专栏、频道、收藏、订阅等。哔哩哔哩Bilibili,你感兴趣的视频都在B站。https://space.bilibili.com/335835274?from=search&seid=2940030192624790742&spm_id_from=333.337.0.0我的上一篇文章:【Unity2D】实现敌人掉血的粒子特效_dangoxiba的博客-CSDN博客学习目标:各个参数参考翻译功能标准:Unity3D:粒子系统Particle System_nothing的专栏-CSDN博客_particle system1. GameObject → Create Other →Particle System。2. 选中 Particle System,可看到下列屬性: 3.Particle System: Duration: 粒子发射时间(设定为5秒,每5秒发射...https://blog.csdn.net/dangoxiba/article/details/122703154
学习内容:
简单实现相机跟随Player功能以及攻击敌人时相机抖动功能,让它看起来更加真实,而且可以移动场景,非常方便,以及让敌人受伤害的打击感更真实
代码部分:
先按如图所示创建好3个空对象,并且把Main Camera拖到CameraFollow当子对象
有几个新创立的脚本;
Camera Follower脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Transform target;
public float smoothing;
public Vector2 minPosition;
public Vector2 maxPosition;
void Start()
{
GameController.cameraShake = GameObject.FindGameObjectWithTag("CameraShake").GetComponent();
}
// Update is called once per frame
void Update()
{
if(target != null)
{
if(transform.position != target.position)
{
Vector3 targetPos = target.position;
//限制移动范围
targetPos.x = Mathf.Clamp(targetPos.x, minPosition.x, maxPosition.x);
targetPos.y = Mathf.Clamp(targetPos.y, minPosition.y, maxPosition.y);
transform.position = Vector3.Lerp(transform.position, targetPos, smoothing );
}
}
}
public void SetCamLimitPositon(Vector2 minPos,Vector2 maxPos)
{
minPosition = minPos;
maxPosition = maxPos;
}
}
GameController脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour
{
public static CameraShake cameraShake;
}
CameraShake脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraShake : MonoBehaviour
{
public Animator cameraAnim;
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void Shake()
{
cameraAnim.SetTrigger("Shake");
}
}
要触发这个Shake方法,就要在Enemy的TakeDamage()中调用
Enemy脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class Enemy : MonoBehaviour
{
public int health;
public int damage;
public float changeTime;
public GameObject bloodEffect;
private SpriteRenderer sr;
private Color originColor;
public void Start()
{
sr = GetComponent();
originColor = sr.color;
}
public void Update()
{
if(health
关注
打赏
- 【Untiy2D独立/合作开发】特别篇:如何实现快速丢掉物品
- [Unity2D独立/合作开发]实现记录物品在不同场景的存在状态,附:场景的淡入淡出功能和预加载
- 【Unity好插件之PlayMaker系列一上半部分】如何只用一个插件和一个脚本完成制作一个简易的游戏
- [Unity存档系统]简单介绍Unity常见存档系统二JSON以及使用方法
- [Unity存档系统]简单介绍Unity常见存档系统之一PlayerPrefs以及使用方法
- 【Unity2D好项目分享】用全是好活制作横版卷轴射击游戏⑤制作得分系统以及能量的使用。
- 【Unity2D好项目分享】用全是好活制作横版卷轴射击游戏④制作敌人管理器以及播数显示和音效以及场景加载器
- 【Unity2D好项目分享】用全是好活制作横版卷轴射击游戏③制作血条和能量条UI以及生命值系统和能量值系统
- 【Unity2D好项目分享】用全是好活制作横版卷轴射击游戏②游戏画面后处理以及制作玩家子弹
- 【Unity2D好项目分享】用全是好活制作横版卷轴射击游戏①简单制作滚动卷轴以及玩家移动
