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、给按钮绑定事件:

Unity3d工具方法小集的更多相关文章

  1. JQuery操作类数组的工具方法

    JQuery学习之操作类数组的工具方法 在很多时候,JQuery的$()函数都返回一个类似数据的JQuery对象,例如$('div')将返回div里面的所有div元素包装的JQuery对象.在这中情况 ...

  2. jQuery工具方法

    目录 常用工具方法 判断数据类型的方法 Ajax操作 $.ajax 简便写法 Ajax事件 返回值 JSONP 文件上传 参考链接 jQuery函数库提供了一个jQuery对象(简写为$),这个对象本 ...

  3. jQuery晦涩的底层工具方法们

    这里整理的是jQuery源码中一些比较晦涩难懂的.内部的.最底层的工具方法,它们多为jQuery的上层api方法服务,目前包括: jQuery.access jQuery.access: functi ...

  4. angular的工具方法笔记(equals, HashKey)

    分别是angular脏值检测的工具方法equals和 类HashKey的使用方法 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transi ...

  5. zepto源码学习-02 工具方法-详细解读

    上一篇:地址 先解决上次留下的疑问,开始看到zepto.z[0]这个东西的时候,我很是不爽,看着它都不顺眼,怎么一个zepto的实例对象var test1=$('#items');  test__pr ...

  6. jQuery源代码 解析一 工具方法

    1. 外层沙箱以及命名空间$ 几乎稍微有点经验前端人员都这么做,为了避免声明了一些全局变量而污染,把代码放在一个"沙箱执行",然后在暴露出命名空间(可以为API,函数,对象): 2 ...

  7. Underscore.js 常用类型判断以及一些有用的工具方法

    1. 常用类型判断以及一些有用的工具方法 underscore.js 中一些 JavaScript 常用类型检查方法,以及一些工具类的判断方法. 首先我们先来谈一谈数组类型的判断.先贴出我自己封装好的 ...

  8. 秒味课堂Angular js笔记------Angular js中的工具方法

    Angular js中的工具方法 angular.isArray angular.isDate angular.isDefined angular.isUndefined angular.isFunc ...

  9. javascript 的工具方法 --- 类型判断

    Javascript中常见类型对象有: Boolean, Number, String, Function, Array, Date, RegExp, Object, Error, Symbol等等. ...

随机推荐

  1. ACM-ICPC(10/21)

    写一发后缀数组套路题,看起来简单,写起来要人命哦~~~ 总共13题. 分两天debug吧,有点累了~~~ suffix(后缀数组的应用) sa[i] :排名第 i 的后缀在哪(i 从 1 开始) ra ...

  2. React中的虚拟DOM

    当组件当state和props发生变化当时候,组件当render函数就会重新执行,组件就会被重新渲染,react中实现这种重新渲染,他的性能是非常高的,因为他引入了一个虚拟Dom的概念,那么什么是虚拟 ...

  3. ASP.NET SignalR 与LayIM配合,轻松实现网站客服聊天室(七)之 图文,附件消息(2016-05-05 12:13)

    上一篇介绍了加好友的流程,这里不再赘述,不过之前的聊天只能发送普通文字,那么本篇就教你如何实现发送附件和图片消息.我们先对功能进行分析: 发送图片,附件,需要实现上传图片和附件的功能. textare ...

  4. 机器学习基础(HGL的机器学习笔记1)

    统计学习:统计学习是关于计算机基于数据构建概率统计模型并运用模型对数据进行预测与分析的一门学科,统计学习也成为统计机器人学习[1]. 统计学习分类:有监督学习与无监督学习[2]. 统计学习三要素:模型 ...

  5. 菜鸟笔记 -- Chapter 6.3 对象

    6.3 对象 Java是一门面向对象的程序设计语言,对象是由类抽象出来的,所有的问题都是通过对象来处理的,对象可以操作类的属性和方法解决相应的问题,所以了解对象的产生.操作和生存周期对学习Java语言 ...

  6. UICollectionViewCell的设置间距

    UICollectionViewCell的设置间距 #pragma mark - UICollectionView 大小(宽高,平均一行三个) - (CGSize)collectionView:(UI ...

  7. ABAP术语-Accounting Document

    Accounting Document 原文:http://www.cnblogs.com/qiangsheng/archive/2007/12/12/991731.html Accounting d ...

  8. 关于对连接数据库时出现1130-host “**” is not allowed to connect to this MySql/mariadb server 的错误解决方法

    在完成mariadb的搭建后,在端口与防火墙均为正常的情况下,出现了1130- Host xxx is not allowed to connect to this MariaDb server 的情 ...

  9. vue项目中缓存问题

    单页面应用总是存在缓存问题,特别是在微信端,更新页面之后访问的还是老页面,缓存的问题是因为用户访问的脚本地址并没有改变,浏览器就会读取原来的脚本 网上有几种解决办法,首先列举一下 1.加meta,禁止 ...

  10. jquery easyui alert闪一下的问题

    最近做项目使用了 jQuery EasyUI,版本是 1.4.3.x,在使用alert方法的时候如果alert后面执行页面跳转的话alert的消息只会闪一下,就跳到其他页面了 $.messager.a ...