1。將滑鼠移至 Scene 視窗的任何位置,按下F , 則可拉近正在編輯的物件。
2。Directional Light Transform Rotation 設定為 x=50 y=60 z=0較適合
3.更新事件:
Update() {} //算出Frame前執行
FixedUpdate(){} //j運算物理性前執行
WEB GL link
http://charms0905.myweb.hinet.net
PlayerController.cs
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float Speed;
private Rigidbody rb; // 宣告暫存剛體類別位置
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody> (); //取得this內Rigidbody組件數據,存於rb
}
// Update is called once per frame
void FixedUpdate () {
float moveHorizontal = Input.GetAxis ("Horizontal");//每次執行此行,若A或左鍵是按著,回傳-1,反之,D或右鍵,則回傳1, 沒按則回傳0
float moveVertical = Input.GetAxis ("Vertical");//垂直(前後)移動偵測WS
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);//施力方向變數
rb.AddForce (movement * Speed); //將剛體rb施力,方向為Vector3
}
}
腳本(CameraController):攝影機跟隨player
CameraController.cs
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public GameObject player; // camera 要跟隨的對象
private Vector3 offset; // camera 與要跟隨的對象的差距值
// 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;
}
}
切換攝影機
切換攝影機
using UnityEngine;
using System.Collections;
public class cameracontroll : MonoBehaviour {
public GameObject cam1, cam2;
//public GameObject light1;
public GameObject canvas1;
void Awake(){
cam1.SetActive (true);
cam2.SetActive (false);
//light1.SetActive (false);
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKey("s") || Input.GetKey("down") == true)
{
//若是按下鍵盤的z則切換成第二部攝影機
canvas1.SetActive(false);
cam1.SetActive(false);
cam2.SetActive(true);
//light1.SetActive (true);
}
else
{
//若是按下鍵盤的x則切換成第一部攝影機
canvas1.SetActive(true);
cam2.SetActive(false);
cam1.SetActive(true);
//light1.SetActive (false);
}
}
}
腳本(Rotator):物體旋轉
Ratator.cs
using UnityEngine;
using System.Collections;
public class Rotator : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Rotate (new Vector3 (15, 30, 45) * Time.deltaTime);
}
}
物件觸發
碰撞消失
void OnTriggerEnter (Collider other){ //進入物件磁撞觸發事件啟動,被碰者置於other參數中
if(other.gameObject.CompareTag("PickUp"))
{
other.gameObject.SetActive (false);
}
}
免費音效綱站
加入音效
宣告 public AudioClip soundPickUp;
碰接時 AudioSource.PlayClipAtPoint (soundPickUp, transform.position);
UI
首先在兩個using中間加入 using Unityengine.UI;
宣告
private int count;
public Text countText;
public Text winText;
(公開定義,再拖曳視窗內的text 至變數)
start()內 (設定初始值)
count = 0;
countText.text = "Count: "+ count.toString();
winText.text = "";
碰接時
count = count+1;
countText.text = "Count: "+ count.toString();
if(count >=12)
{
winText.text = "You Win!";
}
參考網站:
transform 操作說明:
http://glimmerfish.blogspot.com/2016/01/unity-gameobject-transform-2.html
駕駛艙轉向
using UnityEngine;
using System.Collections;
public class cockpitcontrol : MonoBehaviour {
private Transform m_Cam;
private Vector3 m_CamForward; // The current forward direction of the camera
private Vector3 m_Move;
public Camera maincam;
// Use this for initialization
void Start () {
if (maincam != null)
{
m_Cam = maincam.transform; //取得主攝影機的資料
}
}
void FixedUpdate () {
// read inputs
float h = Input.GetAxis ("Horizontal");
float v = Input.GetAxis ("Vertical");
// calculate move direction to pass to character
if (m_Cam != null)
{
// calculate camera relative direction to move:
transform.Rotate ( new Vector3 (0.0f, h, 0.0f)); //利用左右移動來旋轉
m_CamForward = Vector3.Scale(m_Cam.forward, new Vector3(1, 0, 1)).normalized; //取攝影機的轉向
m_Move = v*m_CamForward ; //+ h*m_Cam.right;
//Debug.Log("int val: " + h);
}
else
{
// we use world-relative directions in the case of no main camera
transform.Rotate ( new Vector3 (0.0f, h, 0.0f));
m_Move = v*Vector3.forward;
//Debug.Log("null cam");
}
// pass all parameters to the character control script
transform.position=transform.position+m_Move;
}
// Update is called once per frame
}
生成Prefabs物 品,並給予速度
生成物品並加速
using UnityEngine;
using System.Collections;
public class test1 : MonoBehaviour {
public Rigidbody ball1;
public float movey;
public float movez;
private Rigidbody a;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Fire1")) {
a = Instantiate(ball1, transform.position, transform.rotation) as Rigidbody;;
a.velocity = transform.TransformDirection (new Vector3(1.0f*m_x, 1.0f*m_y 1.0f*m_z));
//a.velocity = new Vector3(0, 10, 0);
}
}
}
銷毀物品
Destroy(gameObject,1.0f); //1.0f 等於1秒