發表文章

目前顯示的是有「Unity3D C#範例」標籤的文章

Unity3D範例之3.DragRigidbody

 DragRigidbody.cs using UnityEngine; using System.Collections; public class DragRigidbody : MonoBehaviour { public float spring; public float damper; public float drag; public float angularDrag; public float distance; public bool attachToCenterOfMass; private SpringJoint springJoint; private Rigidbody body; // Use this for initialization void Start () { spring = 50.0f; damper = 5.0f; drag = 10.0f; angularDrag = 5.9f; distance = 0.2f; attachToCenterOfMass = false; } // Update is called once per frame void Update () { if(!Input.GetMouseButtonDown(0)) return; Camera mainCamera = FindCamera(); RaycastHit hit; //檢測滑鼠按鍵是否點擊 if(!Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition),out hit,100)) return; //是否有點擊到剛性物體或剛性物體可被物理控制 if(!hit.rigidbody||hit.rigidbody.isKinematic) return; if(!springJoint){ //彈性物體是否存在 //新增一個名稱為"Rigidbody dragger"的遊戲物件 GameObject go = new GameObject("Rigidbody dr...

Unity3D範例之2.取得Unity3D上的所有物件

void Awake(){ Object[] objs = FindObjectsOfType(typeof(GameObject)); foreach(GameObject go in objs){ print("objname:"+go.name); } }

Unity3D範例之1.FPSWalker

FPSWalker.cs using UnityEngine; using System.Collections; [RequireComponent(typeof(CharacterController))] //js->@script RequireComponent(CharacterController) public class FPSWalker : MonoBehaviour { //請不要在這裡就將參數初始化 public float speed = 6.0f; public float jumpSpeed = 8.0f; public float gravity = 20.0f; private Vector3 moveDirection = Vector3.zero; private bool grounded = false; // Use this for initialization void Start () { //一定要在Start或Awake裡面做初始化,這樣才不會有參數值更改無效的問題 speed = 6.0f; jumpSpeed = 8.0f; gravity = 20.0f; moveDirection = Vector3.zero; grounded = false; } // Update is called once per frame void FixedUpdate () { if(grounded){ //取得自身座標 moveDirection = new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical")); //轉換自身座標為世界座標 moveDirection = transform.TransformDirection(moveDirection); //x,y,z向量值乘與speed moveDirection *= speed; //有按到Edit-Project Settings-Input中Ju...