unity学习(三)
生活随笔
收集整理的這篇文章主要介紹了
unity学习(三)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.?新建plateForm預制件,添加腳本,加入四種不同的材質球及物理材質,即可使腳下的階梯實現四種不同效果
using UnityEngine; using System.Collections; using System.Collections.Generic;public class PlateFormMan : MonoBehaviour {public Material[] mats;public PhysicMaterial[] phyMats;public int gameObjectNumber;public Vector3 startPosition;Vector3 nextPosition;public Transform prefab;Queue<Transform> queue;public int maxY, minY;public Vector3 minGap, maxGip;public Vector3 minSize, maxSize;public float offset;void Start(){queue = new Queue<Transform>();for (int i = 0; i < gameObjectNumber;i++ ){queue.Enqueue((Transform)GameObject.Instantiate(prefab));}nextPosition = startPosition;for (int i = 0; i < gameObjectNumber;i++ ){Recycle();}}private void Recycle(){Vector3 position = nextPosition;Vector3 scale = new Vector3(Random.Range(minSize.x,maxSize.x),1,1);position.x += scale.x * 0.5f;position.y += scale.y * 0.5f;Transform t = queue.Dequeue();t.localScale = scale;t.position = position;int r = Random.Range(0,mats.Length);t.renderer.material = mats[r];t.collider.material = phyMats[r];nextPosition += new Vector3(scale.x+Random.Range(minGap.x,maxGip.x),scale.y+Random.Range(minGap.y,maxGip.y),Random.Range(minGap.z,maxGip.z));if(nextPosition.y>maxY){nextPosition.y = maxY - Random.Range(minGap.y,maxGip.y);}if(nextPosition.y<minY){nextPosition.y = maxY + Random.Range(minGap.y,maxGip.y);}queue.Enqueue(t);}void Update(){if(RunnerScript.distance-queue.Peek().position.x>=offset){Recycle();}} }2.?給runner添加rigidbody(剛體),使用重力,添加腳本,將各項參數設置好之后就實現了runner在階梯上行進,跳躍的效果
using UnityEngine; using System.Collections;public class RunnerScript : MonoBehaviour {public static float distance;public Vector3 jumpVelocity;bool isOnPlateForm=false;void Update(){transform.Translate(5f * Time.deltaTime, 0, 0);distance = transform.position.x;if(Input.GetButtonDown("Jump")){if(isOnPlateForm){rigidbody.AddForce(jumpVelocity, ForceMode.VelocityChange);}}}void OnCollisionEnter(){isOnPlateForm = true;}void OnCollisionExit(){isOnPlateForm = false;}}
總結
以上是生活随笔為你收集整理的unity学习(三)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 三维细胞结构生物显微影像分析处理软件
- 下一篇: PPT目录页怎么设计才高大上?告诉你一个