球生球
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; }
}
}
}