Professional Documents
Culture Documents
Tailieu Unity3d
Tailieu Unity3d
Tailieu Unity3d
Unity l engine mnh trong lp trnh game 3d, v m phng h thng 3d, ng
thi Unity h tr xut ra trn mi h iu hnh di ng vi cc phin bn khc
nhau. Nhm pht trin game, v o to lp trnh vin unity v ha 3d ti
3dvietpro gii thiu vi cc bn bc u tin lm quen vi Unity:
Hnh:
V d:
Thnh
Cc bn lm quen vi cc trc ta nh hnh di:
Cc bn code nh sau:
// Khai bo bin
pivot = cam.parent;
void OnDrawGizmos()
Gizmos.color = Color.green;
Gizmos.DrawLine(pivot.position,
cam.transform.position);
Nh hnh:
Hnh 2.3.2. V tr t im cha ti im con
Bc 2: Code hin th
void Update () {
if (Input.GetKeyDown(KeyCode.Q))
if (statusBol == false)
Time.timeScale = 0.7f;
statusBol = true;
{
Time.timeScale = 0.1f;
statusBol = false;
print("fixedDeltaTime:" + Time.fixedDeltaTime);
if (Input.GetKeyDown(KeyCode.Space))
transform.Translate(0,Time.deltaTime * Speed,0);
if (val == false)
return;
if (Time.timeScale == 0f) {
Time.timeScale = 1f;
pause = false;
return;
} else {
Time.timeScale = 0f;
pause = true;
return;
var spawnPoint:Transform;
var Gtext:GUIText;
function Start(){
var i=0;
i++;
function Update () {
//ly v tr t im u ti cc im c nh du
dau=Vector3.Distance(transform.position,waypoint[j].position);
var relvitePos=waypoint[j].position-transform.position;
var rotation=Quaternion.LookRotation(relvitePos);
transform.rotation=Quaternion.Slerp(transform.rotation,rotation,Time.d
eltaTime*2);
if(dau<=1){
j++;
if(j>=waypoint.length){
j=0;
Bc 2: To cc im c nh du
Bc 2: Cc bn tch m hnh Oto trong hnh 4.1 ra mt project ring, thc hin
tch cc bn lu vn sau:
- Vo th mc gc ca project tm n \Assets\Models\Car
- Xut hin file Materials v catamount.fbx, cc bn thc hin ko vo ca s
Project ca project mi to. Trong Materials l map ca t, cn fbx l m hnh
3D ca t.
Bc 3: Khi thc hin ko t hin th khng c mu sc, cc bn click vo cc
phn ca Oto s hin hnh nh di:
Hnh 4.1.2. Thnh phn ca t c gn map
Nu phn Material trong hnh trn khng c, th cc bn tm trong th mc Materials va Import ph hp.
Bc 4: Click p vo bnh xe, sau vo Game Object chn Create Empty, khi
chn xong bn s thy xut hin mt im nm trung tm bnh xe v chnh l
im Game Object va ri bn to.
Bc 8: Vit lnh cho bnh xe chuyn ng bng cch to ra mt file C#, sau
bn ko vo chic xe.
- Chng ta khai bo dng lnh sau l Public
- thc hin lm chuyn ng, nh thc t cho thy l chng ta phi dng lc y 2 bnh sau i v bnh trc
ch c nhim v quay gc lm bnh li cho xe chy theo cc hng c iu khin. Cc bn vit hm sau trong
Update:
WheelRL.Rotate (speed*Input.GetAxis("Vertical"),0,0);
WheelRR.Rotate (speed* Input.GetAxis("Vertical"),0,0);
WheelFL.Rotate (speed*Input.GetAxis("Vertical"),0,0);
WheelFR.Rotate (speed * Input.GetAxis ("Vertical"), 0, 0);
V chnh xc vi tc quay chng ta c th thay bin speed bng dng lnh sau:
WheelFL_col.rpm/60*360
Th l chng ta vit lnh chuyn ng cho bnh xe, by gi cc bn ch cn gn cc i tng Public cho cc
bin c khai bo v chy chng trnh:
Hnh 4.1.8. Ko cc i tng vo cc bin ca xe 3D
Bc 9: Kt qu chy
rigidbody.velocity.magnitude * 3.6f
VD:
public Transform target;
void Update() {
//xc nh hng cn xoay ti
Vector3 relativePos = target.position - transform.position;
// s dng hm LookRotation a ra vng cn quay
Quaternion rotation = Quaternion.LookRotation(relativePos);
// di chuyn camera theo vng quay c tnh
transform.rotation = rotation;
}
VD:
public Transform from;
public Transform to;
public float speed = 0.1F;
void Update() {
transform.rotation = Quaternion.Slerp(from.rotation, to.rotation, Time.time *
speed);
}
GetTouch(0).phase
TouchPhase
+ Began: Ngn tay chm vo mn hnh
VD 1:
if (Input.GetTouch(i).phase == TouchPhase.Began)
VD 2:
if(nbTouches > 0)
switch(phase)
case TouchPhase.Began:
break;
case TouchPhase.Moved:
break;
case TouchPhase.Stationary:
break;
case TouchPhase.Ended:
break;
case TouchPhase.Canceled:
break;
Tnh tc ca chm
Ly v tr ca mn hnh
Vector3 position =
Camera.main.ScreenToWorldPoint(touch.position);
tapCount
if (Input.touchCount > 0) {
Debug.Log("ONE TAP");
if(Input.GetTouch(0).tapCount == 2)
Debug.Log("DOUBLE TAP");
Input.acceleration
Cc s dng:
VD 1:
4. Chn Add New Event Type> PointerEnter thc hin vic hover
chut, hay Chm vo i tng
Trong :
Bc 1: To 2 bin sau
public ParticleEmitter Effect_L;
public ParticleEmitter Effect_R;
public Transform skidmark;
Ngun
3Dvietpro