您当前的位置: 首页 >  dangoxiba unity

【Unity2D】简单实现相机跟随Player功能以及攻击敌人时相机抖动

dangoxiba 发布时间:2022-01-26 20:06:07 ,浏览量:1

学习目标:

简单实现相机跟随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

关注
打赏
查看更多评论

dangoxiba

暂无认证

  • 1浏览

    0关注

    55博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录