一、摄像机控制
1.摄像机与物体同步移动
public class CameraController : MonoBehaviour {
// store a public reference to the Player game object, so we can refer to it's Transform
public GameObject player;
// Store a Vector3 offset from the player (a distance to place the camera from the player at all times)
private Vector3 offset;
// At the start of the game..
void Start ()
{
// Create an offset by subtracting the Camera's position from the player's position
offset = transform.position - player.transform.position;
}
// After the standard 'Update()' loop runs, and just before each frame is rendered..
void LateUpdate ()
{
// Set the position of the Camera (the game object this script is attached to)
// to the player's position, plus the offset amount
transform.position = player.transform.position + offset;
}
}
2.摄像机跟随物体移动
public float smoothing = 5f; // The speed with which the camera will be following.
Vector3 offset; // The initial offset from the target.
void Start ()
{
// Calculate the initial offset.
offset = transform.position - target.position;
}
void FixedUpdate ()
{
// Create a postion the camera is aiming for based on the offset from the target.
Vector3 targetCamPos = target.position + offset;
// Smoothly interpolate between the camera's current position and it's target position.
transform.position = Vector3.Lerp (transform.position, targetCamPos, smoothing * Time.deltaTime);
}
3.摄像机对准物体旋转移动
if (rotateCenter) { //当旋转中心对象设置时才进行物体公转
transform.RotateAround (
rotateCenter.transform.position, //旋转中心点
rotateCenter.transform.up, //旋转轴:此处设置为为旋转中心的向上方向(正Y轴)
Time.deltaTime * rotateSpeed //旋转的角度,rotateSpeed表示旋转的速度,Time.deltaTime表示该帧执行的时间
);
transform.LookAt(rotateCenter.transform); //使游戏对象始终朝向旋转中心
}
二、物体控制
1.物体旋转
transform.Rotate (new Vector3 (15, 30, 45) * Time.deltaTime);
public float tumble = 10.0f;
GetComponent().angularVelocity = Random.insideUnitSphere * tumble;
2.物体受力移动
void FixedUpdate ()
{
// Set some local float variables equal to the value of our Horizontal and Vertical Inputs
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
// Create a Vector3 variable, and assign X and Z to feature our horizontal and vertical float variables above
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
// Add a physical force to our Player rigidbody using our 'movement' Vector3 above,
// multiplying it by 'speed' - our public player speed that appears in the inspector
rb.AddForce (movement * speed);
}
3.物体位置移动(归一化后,斜着移动距离不变)
Vector3 movement; // The vector to store the direction of the player's movement.
void Move (float h, float v)
{
// Set the movement vector based on the axis input.
movement.Set (h, 0f, v);
// Normalise the movement vector and make it proportional to the speed per second.
movement = movement.normalized * speed * Time.deltaTime;
// Move the player to it's current position plus the movement.
playerRigidbody.MovePosition (transform.position + movement);
}
4.设置物体速度
GetComponent().velocity = transform.forward * speed;
x.随机生成物体
void SpawnWaves()
{
spawnPosition.x = Random.Range(-spawnValues.x, spawnValues.x);
spawnPosition.z = spawnValues.z;
spawnRotation = Quaternion.identity;
Instantiate(hazard, spawnPosition, spawnRotation);
}
三、触发器
void OnTriggerEnter(Collider other)
{ if (other.tag == "Boundary") return;
Destroy(other.gameObject);
Destroy(gameObject);
Instantiate(explosion, transform.position, transform.rotation);
if(other.tag == "player")
{
Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
}
}
void OnTriggerStay(Collider other)
{
if (other.attachedRigidbody)
other.attachedRigidbody.AddForce(Vector3.up * 10);
}
private void OnTriggerExit(Collider other)
{
Destroy(other.gameObject);
}
四、射线
1.射线模拟射击
// Set the shootRay so that it starts at the end of the gun and points forward from the barrel.
shootRay.origin = transform.position;
shootRay.direction = transform.forward;
// Perform the raycast against gameobjects on the shootable layer and if it hits something...
if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
{
// Try and find an EnemyHealth script on the gameobject hit.
EnemyHealth enemyHealth = shootHit.collider.GetComponent ();
// If the EnemyHealth component exist...
if(enemyHealth != null)
{
// ... the enemy should take damage.
enemyHealth.TakeDamage (damagePerShot, shootHit.point);
}
// Set the second position of the line renderer to the point the raycast hit.
gunLine.SetPosition (1, shootHit.point);
}
// If the raycast didn't hit anything on the shootable layer...
else
{
// ... set the second position of the line renderer to the fullest extent of the gun's range.
gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
}
五、导航代理
1.最简单的利用unity自带的导航系统实现寻路
void Awake ()
{
// Set up the references.
player = GameObject.FindGameObjectWithTag ("Player").transform;
playerHealth = player.GetComponent ();
enemyHealth = GetComponent ();
nav = GetComponent ();
}
void Update ()
{
// If the enemy and the player have health left...
if(enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)
{
// ... set the destination of the nav mesh agent to the player.
nav.SetDestination (player.position);
}
// Otherwise...
else
{
// ... disable the nav mesh agent.
nav.enabled = false;
}
}
其他
1.按生命时间销毁
public float lifetime;
void Start ()
{
Destroy (gameObject, lifetime);
}
2.播放动画
Animator anim; // Reference to the animator component.
void Animating(float h, float v)
{
// Create a boolean that is true if either of the input axes is non-zero.
bool walking = h != 0f || v != 0f;
// Tell the animator whether or not the player is walking.
anim.SetBool("IsWalking", walking);
}
3.不断的调用
InvokeRepeating ("Spawn", spawnTime, spawnTime);