Unity 通用的代码集合

1.血条注视屏幕

[csharp]  view plain  copy
  1. void Update () {  
  2.         transform.LookAt(Camera.main.transform);  
  3.     }  

2.角色变色

[cpp]  view plain  copy
  1. GetComponent<MeshRenderer>().material.color = Color.blue;  

3.随机生成8个不同角度的敌人

[cpp]  view plain  copy
  1. public GameObject enemy;  
  2.     public int enemyCount = 8;  
[cpp]  view plain  copy
  1. for (int i = 0; i < enemyCount; i++) {   
  2.            float randomX = Random.Range(-5.0f, 5.0f);  
  3.        float randomY = Random.Range(0, 360.0f);  
  4.        float randomZ = Random.Range(-5.0f, 5.0f);  
  5.   
  6.        Vector3 pos = transform.position+new Vector3(randomX,0,randomZ);  
  7.        Quaternion rot = Quaternion.Euler(0, Random.Range(0f, 360f), 0);  
  8.            GameObject enemyClone = Instantiate(enemy,pos,rot) as GameObject;  
  9.        NetworkServer.Spawn(enemyClone);  

4.掉血

PlayerHealth脚本

[cpp]  view plain  copy
  1.  public int healthMax = 100;     //最大血量  
  2. public int healthCurrent;       //当前血量  
  3.  void Start() {  
  4.         healthCurrent = healthMax;    //当前血量等于最大血量  
  5.     }  
  6.  public void HurtMe(int damage) {    //血条减少  
  7.         healthCurrent -= damage;  
  8.         if (healthCurrent <= 0) {       //如果小于次血量     
  9.            healthCurrent = healthMax;              //满血复活  
  10.            }  
  11. healthBar.sizeDelta = new Vector2(healthCurrent, healthBar.sizeDelta.y);    //血条变了  
  12. }  

Bullet脚本

[cpp]  view plain  copy
  1. public class Bullet : MonoBehaviour {  
  2.   
  3.     private void OnCollisionEnter(Collision collision) {  
  4.        PlayerHealth playerHealth=  collision.gameObject.GetComponent<PlayerHealth>();  
  5.          
  6.      playerHealth.HurtMe(10);  
  7. Destroy(this.gameObject);}}   

5.角色人物移动

[cpp]  view plain  copy
  1.   public float traSpeed = 3;  //移动的速度  
  2.   public float rotSpeed = 120;  //一秒旋转的角度  
  3. void Update () {        
  4.      float h = Input.GetAxis("Horizontal");  
  5.      float v = Input.GetAxis("Vertical");  
  6. transform.Translate(Vector3.forward * v * traSpeed * Time.deltaTime); //朝某个方向移动 transform.Rotate(Vector3.up * h * rotSpeed * Time.deltaTime); //围绕某轴旋转  

6.player发射

[cpp]  view plain  copy
  1. public GameObject bulletPre;   //子弹的prefab  
  2.  public Transform bulletTrans;  //生成子弹的位置  
  3.  void Update(){  
  4.  if (Input.GetKeyDown(KeyCode.Space))  
  5.      {            
  6. ameObject bullet = Instantiate(bulletPre, bulletTrans.position, Quaternion.identity) as GameObject;  
  7.      bullet.GetComponent<Rigidbody>().velocity = transform.forward * 10;  
  8.      Destroy(bullet, 2);   //2秒后销毁子弹   } }}  

7.跨类调用脚本

[cpp]  view plain  copy
  1.     private <span style="color:#33cc00;">GameControl</span> shoots;    //GameControl是需要调用脚本的类  
  2.   
  3.     void Start() {  
  4.         shoots = GameObject.Find("GameController").GetComponent<GameControl>();  //物体名,脚本名  
  5.     }  
  6.     void Ffff(){  
  7.         shoots.  ()  
  8. }  

8.射线

[csharp]  view plain  copy
  1. if (Input.GetMouseButtonDown(0)) {  
  2.             //Vector3 rayPosition = new Vector3(Camera.main.pixelWidth * 0.5f, Screen.height * 0.5f);  
  3.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);   //定义准备一条射线  
  4.             RaycastHit hit;  
  5.             if (Physics.Raycast(ray, out hit)) {  
  6.                 if (hit.transform.tag == "bbb") {  
  7.               Instantiate(g,hit.transform.position, Quaternion.identity);  
  8.                 }  
  9.             }  
  10.         }  

注意要在加碰撞器

[csharp]  view plain  copy
  1. Camera.main.ScreenPointToRay(Input.mousePosition)    这里是从鼠标点击的位置垂直在game窗口生成一条射线,用于点击物体生成物体  

[csharp]  view plain  copy
  1.  Vector3 rayPosition = new Vector3(Camera.main.pixelWidth * 0.5f, Screen.height * 0.5f);  
  2.   Ray ray = Camera.main.ScreenPointToRay(rayPosition);   从game窗口屏幕中间垂直生成一条射线,用于FPS,准星位置  

9.防止连续点击按钮实例化出多个预置体

[csharp]  view plain  copy
  1. bool isBuilding=false;  
[csharp]  view plain  copy
  1. if (isBuilding == false) {  
  2.      Instantiate(g, hit.transform.position, Quaternion.identity);  
  3.      isBuilding = true;  
  4.   }  

10.同一个场景切换游戏界面

Gamecontrolle脚本:

[csharp]  view plain  copy
  1. bool started = false;  

然后在开始进行游戏的前面加上:

[csharp]  view plain  copy
  1. if (! started)  
  2.        {  
  3.            return;  
  4.        }  

11.使用数组清除一些数据

[csharp]  view plain  copy
  1. ArrayList pot2Arr;  
[csharp]  view plain  copy
  1. void Start()  
  2.     {  
  3.         pot2Arr = new ArrayList();  
  4. }  
[csharp]  view plain  copy
  1. if (item.movable)  
  2.         {  
  3.  Item pottt=CreatePot(pot2, item.rowIndex, item.columnIndex);  
  4.  pot2Arr.Add(pottt);  

[csharp]  view plain  copy
  1. public void Startgame(){  
  2. for(int i = 0; i < pot2Arr.Count; i++)  
  3.         {  
  4.             Item pot2 = pot2Arr[i] as Item;  
  5.             Destroy(pot2.gameObject);  
  6.         }  
  7.         pot2Arr = new ArrayList();}  

*************************************************

人物靠近门时,自动开门:

[csharp]  view plain  copy
  1. private void OnTriggerEnter(Collider other) {  
  2.         if (other.transform.tag == "Player") {  
  3.             transform.position = transform.position + new Vector3(0, 0, -2.0f);  
  4.             Invoke("Guanmen", 3.0f);  
  5.         }  
  6.     }  
  7.     void Guanmen() {  
  8.         transform.position = transform.position + new Vector3(0, 0, 2.0f);  
  9.     }  
  • 6
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值