*** U3D使用哪个IDE?
默认mono,使用VS 2013比较好。如何设置脚本编辑工具:
Edit-- >Preferences-->External Tools面板有一个External Script Editor 选择脚本编辑软件
*** 如何设置vs的快捷键和eclipse一样?
一个一个设置,没有批量导入功能。不爽。
*** 给一个物体一个力
1) 物体加上 rigidBody ,
2)
Rigidbody rd;
rd = GetComponent<Rigidbody>();
float x = Input.GetAxis ("Horizontal" );
float z = Input.GetAxis ("Vertical");
Vector3 pos = new Vector3 (x,0.0f,z);
rd.AddForce (pos * speed);
*** 碰撞
void OnTriggerEnter(Collider other) {
if(other.gameObject.CompareTag("pick up")){
Debug.Log("碰撞了");
other.gameObject.SetActive(false);
}
}
*** 相机跟随
1)
Vector3 offset;
offset = transform.position - player.transform.position;
transform.position = player.transform.position + offset;
2) 在 smooth s内跟随移动
public Transform target;
public float smoothing =5f;
Vector3 offset;
offset = transform.position - target.position;
Vector3 targetCamPos = target.position + offset;
transform.position = Vector3.Lerp(transform.position,targetCamPos,smoothing * Time.deltaTime);
*** Update、Fixedupdate、LateUpdate 区别
Update 每一帧都调用
FixedUpdate 固定更新 处理刚体 物理逻辑时用
lateUpdate 晚于更新 相机跟随用
*** 花1s时间 物体从 pos 移动得到 target
var start : Transform;
var end : Transform;
function Update () {
transform.position = Vector3.Lerp(start.position, end.position, Time.time);
}
*** 脚本绘制一个纹理
oneTexture = (Texture2D)Resources.Load("OneTexture/Grass");
GUI.DrawTexture(new Rect(110, 20, 120, 120), oneTexture, ScaleMode.ScaleToFit, true, 0);
*** 声音播放
public AudioClip fire;
audio.PlayOneShot(fire);
*** 移动物体
playerRigidbody.MovePosition(transform.position + movement);
*** 旋转向鼠标点击的方向
Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit floorHit;
if(Physics.Raycast(camRay,out floorHit,camRayLength,floorMask)){
Vector3 playerToMouse = floorHit.point - transform.position;
playerToMouse.y = 0f;
Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
playerRigidbody.MoveRotation(newRotation);
}
*** 加载正运行的场景 scene
Application.LoadLevel (Application.loadedLevel);
*** 敌人攻击脚本
void OnTriggerEnter (Collider other)
{
if(other.gameObject == player)
{
playerInRange = true;
}
}
void OnTriggerExit (Collider other)
{
if(other.gameObject == player)
{
playerInRange = false;
}
}
timer += Time.deltaTime;
if(timer >= timeBetweenAttacks && playerInRange && enemyHealth.currentHealth > 0)
{
Attack ();
}
if(playerHealth.currentHealth <= 0)
{
anim.SetTrigger ("PlayerDead");
}