1、获取垂直水平方向上的输入:

float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");

2、给刚体一个力:

Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
GetComponent<Rigidbody>().AddForce(movement * speed * Time.deltaTime);

3、摄像头45度俯视角:

Camera:

Position:  x()   y()    z(-)

Rotate:    x()  y()    z()

Scale:     x()   y()    z()

4、摄像机跟随:

public GameObject player;
private Vector3 offset; // Use this for initialization
void Start () {
//摄像机与跟随物体的初始相对位置
offset = transform.position - player.transform.position;
} // Update is called once per frame
void LateUpdate () {
//跟随物体的位置加上相对位置
transform.position = player.transform.position + offset;
}

5、物体自己旋转(固定时间,跟幀率无关):

void Update () {
transform.Rotate(new Vector3(, , ) * Time.deltaTime);
}

6、检测碰撞(且碰撞后对方消失):

private void OnTriggerEnter(Collider other)
{
//这里的TAG为对方物体Tag
if (other.gameObject.CompareTag(TAG))
{
//要让对方消失,对方物体要设置为碰撞触发器(即IsTrigger属性要为true)
other.gameObject.SetActive(false);
}
}

7、设置Button里的Text值

GetComponentInChildren<Text>().text = "";

8、移除刚体上的力:

GetComponent<Rigidbody>().velocity = Vector3.zero;
GetComponent<Rigidbody>().angularVelocity = Vector3.zero;

9、背景滾动:

using UnityEngine;

public class BGScroller : MonoBehaviour {

    public float scrollSpeed;
public float tileSizeZ; private Vector3 startPosition; void Start () {
startPosition = transform.position;
} void Update () {
float newPosition = Mathf.Repeat(Time.time * scrollSpeed, tileSizeZ);
transform.position = startPosition + Vector3.forward * newPosition;
}
}

10、出边界后销毀:

using UnityEngine;

public class DestoryByBoundary : MonoBehaviour {

    void OnTriggerExit(Collider other)
{
Destroy(other.gameObject);
}
}

11、把prefabs放入场景中:

Instantiate(object, position, rotation);

12、一定时间后销毁:

using UnityEngine;

public class DestoryByTime : MonoBehaviour {

    public float lifetime;

    void Start () {
Destroy(gameObject, lifetime);
}
}

13、游戏开始等待一定时间,过程中时间间隔,每一关时间间隔:

void Start()
{
StartCoroutine (SpawnWaves());
} IEnumerator SpawnWaves()
{
yield return new WaitForSeconds(startWait);
while (true) {
playerObject.GetComponent<PlayerController>().SetFireByLevel(level);
for (int i = ; i < hazardCount; i++)
{
GameObject hazard = hazards[Random.Range(, hazards.Length)];
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate(hazard, spawnPosition, spawnRotation);
yield return new WaitForSeconds(spawnWait);
}
level++;
yield return new WaitForSeconds(waveWait);
if (gameOver)
{
restartButton.SetActive(true);
restart = true;
break;
}
}
}

14、加载场景:

SceneManager.LoadScene("_Scenes/Main", LoadSceneMode.Single);

15、物体倾斜:

rb.rotation = Quaternion.Euler(0.0f, 0.0f, rb.velocity.x * -tilt);

16、随机旋转:

GetComponent<Rigidbody>().angularVelocity = Random.insideUnitSphere * tumble;

tumble在5左右。

17、开火触模板:

using UnityEngine;
using UnityEngine.EventSystems; public class SimpleTouchAreaButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
private bool touched;
private int pointerID;
private bool canFire; void Awake()
{
touched = false;
canFire = false;
} public void OnPointerDown(PointerEventData eventData)
{
if (!touched)
{
touched = true;
pointerID = eventData.pointerId;
canFire = true;
}
} public void OnPointerUp(PointerEventData eventData)
{
if (eventData.pointerId == pointerID)
{
canFire = false;
touched = false;
}
} public bool CanFire()
{
return canFire;
}
}

18、位置方向触模板:

using UnityEngine;
using UnityEngine.EventSystems; public class SimpleTouchPad : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler
{
public float smoothing; private Vector2 origin;
private Vector2 direction;
private Vector2 smoothDirection;
private bool touched;
private int pointerID; void Awake()
{
direction = Vector2.zero;
touched = false;
} public void OnPointerDown(PointerEventData eventData)
{
if (!touched)
{
touched = true;
pointerID = eventData.pointerId;
//set our start point
origin = eventData.position;
}
} public void OnDrag(PointerEventData eventData)
{
if (eventData.pointerId == pointerID)
{
//compare the difference between our start point and current pointer pos
Vector2 currentPosition = eventData.position;
Vector2 directionRaw = currentPosition - origin;
direction = directionRaw.normalized;
}
} public void OnPointerUp(PointerEventData eventData)
{
if (eventData.pointerId == pointerID)
{
//reset everything
direction = Vector2.zero;
touched = false;
}
} public Vector2 GetDirection()
{
smoothDirection = Vector2.MoveTowards(smoothDirection, direction, smoothing);
return smoothDirection;
}
}

19、延迟重复执行:

InvokeRepeating("Fire", delay, fireRate);

20、向目标靠近:

using System.Collections;
using UnityEngine; public class EvasiveManeuver : MonoBehaviour { public float dodge;
public float smoothing;
public float tilt;
public Vector2 startWait;
public Vector2 maneuverTime;
public Vector2 maneuverWait;
public Boundary boundary; private Transform playTransform; private float currentSpeed;
private float targetManeuver;
private Rigidbody rb; void Start () {
rb = GetComponent<Rigidbody>();
GameObject playerObject = GameObject.FindGameObjectWithTag("Player");
if (playerObject != null) {
playTransform = playerObject.transform;
}
currentSpeed = rb.velocity.z;
StartCoroutine(Evade());
} IEnumerator Evade()
{
yield return new WaitForSeconds(Random.Range(startWait.x, startWait.y));
while(true)
{
targetManeuver = Random.Range(, dodge) * -Mathf.Sign(transform.position.x);
if (playTransform != null)
{
targetManeuver = playTransform.position.x;
}
yield return new WaitForSeconds(Random.Range(maneuverTime.x, maneuverTime.y));
targetManeuver = ;
yield return new WaitForSeconds(Random.Range(maneuverWait.x, maneuverWait.y));
}
} void FixedUpdate () {
float newManeuver = Mathf.MoveTowards(rb.velocity.x, targetManeuver, Time.deltaTime * smoothing);
rb.velocity = new Vector3(newManeuver, 0.0f, currentSpeed);
rb.position = new Vector3(
Mathf.Clamp(rb.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp(rb.position.z, boundary.zMin, boundary.zMax)
);
rb.rotation = Quaternion.Euler(0.0f, 0.0f, rb.velocity.x * tilt);
}
}

21、给按钮绑定事件:

最新文章

  1. mac电脑的使用
  2. virtualbox桥接网络配置--CentOS
  3. pyenv 使用简介
  4. wine install 32bit netframewok
  5. spring发送邮件(多人接收或抄送多少带附件发送)
  6. C# DateTime.Now
  7. hadoop python and Twitter
  8. 用git上传项目到github
  9. VC 编程ANSI环境下读写Unicode文件
  10. 1.Office 365系列(-)
  11. Response.Write()方法响应导致页面字体变大的解决办法
  12. 11个炫酷的Linux终端命令大全
  13. CentOS6— Redis安装(转和延续)
  14. 360se打开慢,lsass 过高 , cpu温度上升
  15. Vue2.0 v-for 中 :key 到底有什么用?
  16. 软件工程(GZSD2015) 第三次作业提交进度
  17. SpringMVC一例 是否需要重定向
  18. openstack 安全策略权限控制等api接口
  19. 手撕vue-cli配置——webpack.dev.conf.js篇
  20. Java代理(三)

热门文章

  1. 多层感知机MLP的gluon版分类minist
  2. 【[CTSC2000]冰原探险】
  3. MVC学习一:MVC简单流程
  4. markdown的常用高级操作。
  5. Android学习笔记_74_Android回调函数触发的几种方式 广播 静态对象
  6. yarn下资源配置
  7. 【办公】Microsoft Office 2016 专业增强版下载及永久激活-亲测分享
  8. LeetCode2.两数相加 JavaScript
  9. vue开发工具node.js及构建工具webpack
  10. 8.Element-ui日期组件上传到后台日期少一天解决办法