【学习日志】2022.09.17 Unity 球生球 大炮射击 定时机制 Audio

40d564f1520442d782586c2eb41779d6.png

 46c3d7f2f42b4b5fa8fd26b83a1d4360.png

4bf30f6638fa44b586db335aae3577c7.png

86ac5e365f394b2583d05730ae005862.png

3255bfb173dd47678ef04127db5f997c.png

f3527c81ec83451eaf8ee8c1d2c0e748.png

 6b62cf0006cd4c7da9f8a9ba6821fba5.png

0af403d27bb04640859a5b62bb247c0b.png

球生球 

Ball.cs 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ball : MonoBehaviour
{
    // Start is called before the first frame update
    Rigidbody rigid;

    public float speed = 3;
    public float jumpPower = 1000;

    void Start()
    {
        Debug.Log(this.gameObject.name);
        Debug.Log(gameObject.name);
        rigid = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        float v = Input.GetAxis("Vertical");
        float h = Input.GetAxis("Horizontal");
        //float s = Input.GetAxis("Jump");

        Vector3 input = new Vector3(h, 0, v);
        rigid.AddForce(input * speed);

        bool b1 = Input.GetButton("Jump");
        bool b2 = Input.GetButtonDown("Jump");
        bool b3 = Input.GetButtonUp("Jump");

        if (b1 || b2 || b3)
        {
            Debug.Log($"输入状态:{b1} {b2} {b3}");
        }
        if (Input.GetButtonDown("Jump"))
        {
            rigid.AddForce(new Vector3(0, jumpPower, 0));
        }

        if (Input.GetKeyDown(KeyCode.X))
        {
            Debug.Log("XXX");
        }

    }
}

 Coin.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Coin : MonoBehaviour
{
    public GameObject prefabBall;
    List<GameObject> objs;
    void Start()
    {
        objs = new List<GameObject>();
    }
    private void OnCollisionEnter(Collision collision)
    {
        for (int i = 0; i < 2; i++)
        {
            GameObject go = Instantiate(prefabBall);
            go.transform.position = Random.insideUnitSphere * 3;

            objs.Add(go);
        }
    }
}

TestCreate.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestCreate : MonoBehaviour
{
    // Start is called before the first frame update
    //预制体
    public GameObject prefabMouse;
    List<GameObject> objs;


    void Start()
    {
        objs = new List<GameObject>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            for(int i = 0; i < 20; i++)
            {
                GameObject go = Instantiate(prefabMouse);
                go.transform.position = Random.insideUnitSphere * 3;

                objs.Add(go);
            }         
        }
        if (Input.GetButtonDown("Fire2"))
        {
            foreach(var go in objs)
            {
                if (!go.GetComponent<Rigidbody>())
                {
                    go.AddComponent<Rigidbody>();
                }                
            }
        }
        if (Input.GetKeyDown(KeyCode.X))
        {
            Debug.Log($"List长度:{objs.Count}");
            foreach(var go in objs)
            {
                Destroy(go);
            }
            objs.Clear();
        }
    }
}

大炮射击

cannon.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Cannon : MonoBehaviour
{
    // Start is called before the first frame update
    public float hSpeed = 100;
    public float vSpeed = 60;

    public Rigidbody prefabBullet;
    public Transform cannonBody;
    public Transform bulletPos;
    public Transform pitch;  //俯仰

    public float bulletSpeed = 50;

    void Start()
    {
        pitch = transform.Find("俯仰");
    }

    // Update is called once per frame
    void Update()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        //左右
        transform.Rotate(v * vSpeed * Time.deltaTime, h * hSpeed * Time.deltaTime, 0);
        //俯仰
        pitch.Rotate(v * vSpeed * Time.deltaTime,0,0);

        bool fire = Input.GetButtonDown("Jump");

        if (fire)
        {
            Rigidbody bullet = Instantiate (prefabBullet,bulletPos.position,transform.rotation);
            bullet.velocity = cannonBody.up * bulletSpeed;
        }
    }
}

Invoke

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Delay : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Start" + Time.time);
        Invoke("DelayCall", 0.2f);
        
    }

    void DelayCall()
    {
        Debug.Log("DelayCall" + Time.time);
        Invoke("DelayCall", 0.2f);
    }

    void Update()
    {
        
    }
}

Delay.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Delay : MonoBehaviour
{
    // Start is called before the first frame update
    //预制体
    public GameObject prefabMouse;
    List<GameObject> objs;

    void Start()
    {
        Debug.Log("Start" + Time.time);
        objs = new List<GameObject>();
    }
    
    void Create(int n)
    {
        for(int i = 0; i < n; i++)
        {
            GameObject go = Instantiate(prefabMouse);
            go.transform.position = Random.onUnitSphere * 10 + transform.position;

            objs.Add(go);
        }
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            StartCoroutine(TestCoroutine(100));
        }
    }

    void DestroyAll()
    {
        foreach (var go in objs)
        {
            Destroy(go);
        }
        objs.Clear();
    } 

    //协程函数
    IEnumerator TestCoroutine(int n)
    {
        Create(n);

        yield return new WaitForSeconds(2);

        for(int i = 0; i < 400; i++)
        {
            foreach(var obj in objs)
            {
                //向delay物体所在位置运动
                Vector3 to = transform.position;
                //物体当前位置
                Vector3 from = obj.transform.position;
                Vector3 dir = to - from;
                obj.transform.position += dir.normalized * 3 * Time.deltaTime;

                //缩放
                obj.transform.localScale += new Vector3(2f, 2f, 2f) * Time.deltaTime;

            }
            yield return null;
        }

        DestroyAll();

    }
}

Audio

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayAudio : MonoBehaviour
{
    // Start is called before the first frame update
    AudioSource source;
    public List<AudioClip> clips;
    int index = 0;

    void Start()
    {
        source = GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.C))
        {
            source.clip = clips[index];
            source.Play();

            index++;
            if(index >= clips.Count) { index = 0; }
        }
    }
}

ba7da125bc4b449d9486e6ce11cd1dbd.png

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity 2022.3引入了一些新的导航功能,以帮助开发者更方便地为游戏添加导航系统。新版本中的导航系统改进了性能和功能,使其更加强大和灵活。 首先,Unity 2022.3引入了实时路径搜索功能。该功能可以根据场景中的物体位置和碰撞体等信息,在实时场景中进行路径搜索。这意味着开发者可以在游戏运行时动态计算角色或其他游戏实体的路径。这一功能对于实时策略游戏或动态变化的游戏环境非常有用。 其次,新版本的导航系统还改进了自动化的对话框生成功能。开发者可以通过简单的设置,自动生成适应场景的对话框来实现导航指示。这个功能可以大大减轻开发者的负担,并提高游戏中导航的质量。 此外,Unity 2022.3还提供了更多的导航算法选项。不同的游戏场景可能需要不同的导航算法来平衡效率和精度。新版本中的导航系统提供了多种算法选项,让开发者能够根据具体需求选择合适的算法。 最后,Unity 2022.3导航系统的改进还包括了对2D游戏的支持。早期的导航系统主要面向3D游戏,但新版本中加入了对2D游戏的导航支持。这使得开发者能够更方便地为2D游戏创建导航系统,为玩家提供更好的游戏体验。 总的来说,Unity 2022.3版本的导航系统带来了更多强大和灵活的功能,包括实时路径搜索、自动对话框生成、多种导航算法选项以及对2D游戏的支持。这些新功能使得开发者在为游戏添加导航系统时更加便捷,为玩家提供更好的游戏体验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值