unity官方案例RubyAdventure

✨unity基础入门官教学,文章如有误请指正,如果觉得对你有用,请点赞收藏关注一波,谢谢支持😘

提示:以下是本篇文章正文内容

Build分享

百度网盘:
链接:https://pan.baidu.com/s/1WkQbKPhD4hpvxqsPHiLFtw
提取码:6666

效果图

在这里插入图片描述
代码:

player

	Rigidbody2D rigidbody2d;
    private Animator animator;
    public GameObject bullet;
    private GameObject bulletObj;
    private int bulletValue;
    private int bulletMaxValue=99;
    public int BulletValue { get => bulletValue; set => bulletValue = value; }
    public int BulletMaxValue { get => bulletMaxValue; set => bulletMaxValue = value; }
    public AudioClip audio;

    private float horizontal;
    private float vertical;
    public float timeInvincible = 2.0f;
    public float speed = 3.0f;
    private float invincibleTimer;
    
    private int maxHealth = 5;
    public int MaxHealth { get => maxHealth; set => maxHealth = value; }

    private int currentHealth;
    public int CurrentHealth { get => currentHealth;
        set{
            currentHealth = value;
        }
    }


    private bool isInvincible;
    private Vector2 lookDirection = new Vector2(1, 0);
    private Vector2 move;

    void Start()
    {
        rigidbody2d = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
        Application.targetFrameRate = 60;
        currentHealth = MaxHealth;
        BulletValue = 10;
        UIHealthBar.Instance.UpdateBullet(BulletValue, BulletMaxValue);
    }

    void Update()
    {
         horizontal = Input.GetAxis("Horizontal");
         vertical = Input.GetAxis("Vertical");

        move = new Vector2(horizontal, vertical);

        if (!Mathf.Approximately(move.x, 0) || !Mathf.Approximately(move.y, 0))
        {
            lookDirection = new Vector2(move.x, move.y);
            lookDirection.Normalize();
        }
        animator.SetFloat("Look X", lookDirection.x);
        animator.SetFloat("Look Y", lookDirection.y);
        animator.SetFloat("Speed", move.magnitude);

        if (invincibleTimer > 0)
        {
            invincibleTimer -= Time.deltaTime;
        }
        else {
            isInvincible = false;
        }

        if (Input.GetKeyDown(KeyCode.J)&& BulletValue>0)
        {
            Launch();
            BulletValue--;
            UIHealthBar.Instance.UpdateBullet(BulletValue, BulletMaxValue);
        }

        if (Input.GetKeyDown(KeyCode.E))
        {
            RaycastHit2D hit = Physics2D.Raycast(rigidbody2d.position + Vector2.up * 0.2f, lookDirection, 1.5f, LayerMask.GetMask("NPC"));
            if (hit.collider != null)
            {
                NPC nPC = hit.collider.GetComponent<NPC>();
                if (nPC != null)
                {
                    nPC.showNPC();
                }
            }
            Debug.DrawLine(rigidbody2d.position + Vector2.up * 0.2f, hit.point, Color.red);
        }
        if (Input.GetKey(KeyCode.Escape))
        {
            Application.Quit();
        }
    }

    void FixedUpdate()
    {
        //第二次变动
        Vector2 position = rigidbody2d.position;
        position.x = position.x + 3.0f * horizontal * Time.deltaTime* speed;
        position.y = position.y + 3.0f * vertical * Time.deltaTime* speed;
        if (!AudioManager.Instance.source.isPlaying&& horizontal!=0)
        {
            //播放🔊
            AudioManager.Instance.playAudio(GamaMagager.Instance.gameConfig.PlayerMove声音);
        }
        rigidbody2d.MovePosition(position);
    }

    public void ChangeHealth(int amount)
    {
            if (amount < 0)
            {      
            if (isInvincible)
                    return;
                isInvincible = true;
                invincibleTimer = timeInvincible;
            animator.SetBool("Hit", true);

            //播放🔊
            AudioManager.Instance.playAudio(GamaMagager.Instance.gameConfig.受伤声音);
        }
          
        //返回最小到最大之间的值=Mathf.Clamp(当前的值,最小值,最大值)
        CurrentHealth = Mathf.Clamp(CurrentHealth + amount, 0, MaxHealth);

        UIHealthBar.Instance.SetValue(CurrentHealth, MaxHealth);

        Debug.Log(CurrentHealth + "/" + MaxHealth);
        if (CurrentHealth == 0)
        {
            Die();
        }
    }

    /// <summary>
    /// 发射炮弹
    /// </summary>
    void Launch()
    {
        //播放🔊
        AudioManager.Instance.playAudio(GamaMagager.Instance.gameConfig.攻击声音);

        bulletObj = Instantiate(bullet, rigidbody2d.position + Vector2.up * 1f, Quaternion.identity);

        Projectile projectile = bulletObj.GetComponent<Projectile>();

        projectile.Launch(lookDirection, 500);

        animator.SetTrigger("Launch");
    }

    /// <summary>
    /// 捡到子弹包舔加子弹
    /// </summary>
    public void bulletBagAddFun(int BulletValueF)
    {
        //播放🔊
        AudioManager.Instance.playAudio(GamaMagager.Instance.gameConfig.捡到金币声音);
        BulletValue += BulletValueF;
        UIHealthBar.Instance.UpdateBullet(BulletValue, BulletMaxValue);
    }


    private void Die()
    {
            Debug.Log("asd");
            SceneManager.LoadScene("SampleScene");
    }

enemy


private Rigidbody2D rigidbody2d;
//特效
public ParticleSystem smokeEffect;
Animator animator;
private AudioSource source;

public bool vertical;
private bool broken=true;

public float speed=2;
public float changeTime = 3.0f;
float timer;
int direction = 1;

// 在第一次帧更新之前调用 Start
void Start()
    {
        rigidbody2d = GetComponent<Rigidbody2D>();
        timer = changeTime;
        animator = GetComponent<Animator>();
    	source = GetComponent<AudioSource>();
    }

void Update()
{
    timer -= Time.deltaTime;

    if (timer < 0)
    {
        direction = -direction;
        timer = changeTime;
    }
}

void FixedUpdate()
    {
    Vector2 position = rigidbody2d.position;
    if (vertical)
    {
        position.y = position.y + Time.fixedDeltaTime * speed * direction;
        animator.SetFloat("movX",0 );
        animator.SetFloat("movY", direction);
    }
    else
    {
        position.x = position.x + Time.fixedDeltaTime * speed * direction;
        animator.SetFloat("movX", direction);
        animator.SetFloat("movY", 0);
    }
//移动
     rigidbody2d.MovePosition(position);
    if (!broken)
    {
        return;
    }
}
//碰撞器
void OnCollisionEnter2D(Collision2D other)
{
    RubyController player = other.gameObject.GetComponent<RubyController>();

    if (player != null)
    {
      
        player.ChangeHealth(-1);
    }
}

//使用 public 的原因是我们希望像飞弹脚本一样在其他地方调用这个函数
public void Fix()
{
    broken = false;
    rigidbody2d.simulated = false;
    //如果你添加了修复动画,则为可选
    smokeEffect.Stop();
    source.Pause();
    //播放声音🔊
    AudioManager.Instance.playAudio(GamaMagager.Instance.gameConfig.维修机器人修复声音);
    animator.SetTrigger("fieed");
    胜利关.Instance.i++;
}
qw

NPC(青蛙先生)——🐸q’w

public GameObject E;
public GameObject dialogBox;

private float timer;
private float showTextTimer=4f;
private float currentTimer;

void Start()
{
    E.SetActive(true);
    dialogBox.SetActive(false);
    currentTimer = -1f;

}

void Update()
{
    if (currentTimer > 0)
    {
        currentTimer -= Time.deltaTime;
        if (currentTimer < 0)
        {
            dialogBox.SetActive(false);
            E.SetActive(true);
            timer = 0;
            currentTimer = -1f;
        }
    }
}

public void showNPC()
{
    currentTimer = showTextTimer;
    dialogBox.SetActive(true);
    E.SetActive(false);
}

总结

利用碰撞器或者触发器然后通过触发对象来拿到对应的脚本,进行判断然后调用;
通过rigidbody2d.MovePosition(position)移动可以放置颤抖

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SYFStrive

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值