Unity3d: Shoot Em_Up Tutorial 3 - Player Movement

3.1 ทดลองสร้างสคริปต์
ในหน้าต่าง Project ให้สร้างสคริปต์ C# Script ชื่อ
Assets > Scripts > Player > PlayerMovement
ลากสคริปต์ PlayerMovement ที่สร้างไว้ใส่ไปที่ Player ในหน้าต่าง Hierarchy


3.2 ดูรายละเอียดของสคริปต์
เข้าไปที่
http://docs.unity3d.com/Documentation/ScriptReference/index.html
สั่งค้นหาคำว่า Transform
จากนั้นพอหน้าจอแสดงผลการค้นหาออกมา ให้กดที่
Transform
 แล้วสั้งเกตในหมวดหมู่ Functions จะพบคำว่า
Translate          Moves the transform in the direction and distance of translation.
ให้กดที่คำว่า
Translate
จะพบข้อมูลของ
Transform.Translate
กดเปลี่ยนประเภทของสคริปต์เป็น C# จะพบการเปลี่ยนแปลงของสคริปต์


3.3 ทดลองแก้ไข  C# Script
กดดับเบิ้ลคลิกที่ PlayerMovement ซึ่งถูกสร้างขึ้นมา
โดยจะพบค่าเริ่มต้นของสคริปต์ถูกเขียนไว้ว่า
using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}
}


3.4 ทำให้ Player เคลื่อนที่ได้เอง

ที่บรรทัดข้างใต้
void Update () {

ให้เขียนเพิ่มลงไปว่า
Vector3 direction = new Vector3(1, 0, 0); //Creates a variable for the movement direction
transform.Translate(direction); //Applies the movement

จะได้โค้ดใหม่ดังข้างล่างนี้

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
Vector3 direction = new Vector3(1, 0, 0); //Creates a variable for the movement direction
transform.Translate(direction); //Applies the movement
}
}

จากนั้นเมื่อลองกดเล่นทดสอบจะพบว่า วัตถุ Player พุ่งหายไปทางขวาอย่างรวดเร็ว


3.5 ทำให้ Player มีความเร็วลดลง

ให้แทนที่
new Vector3(1, 0, 0);

ด้วยโค้ดด้านล่างนี้
new Vector3(1*Time.deltaTime, 0, 0);

จะได้โค้ดใหม่ดังข้างล่างนี้
using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
Vector3 direction = new Vector3(1*Time.deltaTime, 0, 0); //Creates a variable for the movement direction
transform.Translate(direction); //Applies the movement
}
}

จากนั้นเมื่อลองกดเล่นทดสอบจะพบว่า วัตถุ Player เคลื่อนที่ช้าลง


3.6 สร้างเมนูกำหนดความเร็วของ Player

ข้างบนเหนือ
// Use this for initialization
ให้พิมพ์ว่า
public float speed;

ต่อมาให้แทนที่
new Vector3(1*Time.deltaTime, 0, 0);
ด้วยโค้ดด้านล่างนี้
new Vector3(1*Time.deltaTime*speed, 0, 0);

จะได้โค้ดใหม่ดังข้างล่างนี้
using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {
public float speed;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
Vector3 direction = new Vector3(1*Time.deltaTime*speed, 0, 0); //Creates a variable for the movement direction
transform.Translate(direction); //Applies the movement
}
}

ทีนี้ตรง Component ของ Player จะมีตัวเลือก Speed แสดงออกมา
โดยให้ลองใส่เลข 5 ลงไป Player จะลอยไปทางขวา
และถ้าลองใส่เลข -5 ลงไป Player จะลอยไปทางซ้าย


3.7 ควบคุม Player ให้ย้ายไปซ้ายขวาได้ตามต้องการ

ให้แทนที่เลข 1 ที่อยู่ใน
Vector3 direction = new Vector3(1*Time.deltaTime*speed, 0, 0);
ด้วย
Input.GetAxis("Horizontal")

จะได้โค้ดใหม่ดังข้างล่างนี้
using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {
public float speed;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
Vector3 direction = new Vector3(Input.GetAxis("Horizontal")*Time.deltaTime*speed, 0, 0); //Creates a variable for the movement direction
transform.Translate(direction); //Applies the movement
}
}

ซึ่งมีผลคือ
กดปุ่ม A ก็จะทำให้ Player ย้ายไปทางซ้าย
กดปุ่ม D ก็จะทำให้ Player ย้ายไปทางขวา


3.8 ทำให้ควบคุม Player ให้วิ่งย้ายไปได้ทั้งซ้ายขวาบนล่าง

แทนที่
Vector3 direction = new Vector3(Input.GetAxis("Horizontal")*Time.deltaTime*speed, 0, 0); //Creates a variable for the movement direction

ด้วย
float x_direction = Input.GetAxis("Horizontal")*Time.deltaTime*speed;
float y_direction = Input.GetAxis("Vertical")*Time.deltaTime*speed;
Vector3 direction = new Vector3(x_direction, y_direction, 0); //Creates a variable for the movement direction

จะได้โค้ดใหม่ดังข้างล่างนี้
using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {
public float speed;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
float x_direction = Input.GetAxis("Horizontal")*Time.deltaTime*speed;
float y_direction = Input.GetAxis("Vertical")*Time.deltaTime*speed;
Vector3 direction = new Vector3(x_direction, y_direction, 0); //Creates a variable for the movement direction
transform.Translate(direction); //Applies the movement
}
}

ซึ่งมีผลคือ
กดปุ่ม A ก็จะทำให้ Player ย้ายไปทางซ้าย
กดปุ่ม D ก็จะทำให้ Player ย้ายไปทางขวา
กดปุ่ม W ก็จะทำให้ Player ย้ายไปข้างบน
กดปุ่ม S ก็จะทำให้ Player ย้ายไปข้างล่าง


Credit: http://www.youtube.com/watch?v=FiOPDCHVbr4

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

คาโน่ จิโกโร่ ผู้ก่อตั้งวิชายูโด

ปัญหาสายตา

วิธีติดตั้ง V-Ray for SketchUp 8 (V-Ray 1.48.89)