- การหมุนตัวละครเมื่อ Player ทำการเคลื่อนที่ตัวละครด้วย Joystick
- การเลื่อน Joystick เป็นวงกลม
- การยิงกระสุนด้วย Instantiate
- เพิ่มตัวแปร bullet, bulletPosition
- เพิ่มในส่วน Update()
using UnityEngine;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;
[RequireComponent(typeof(Rigidbody))]
public class PlayerMovment : MonoBehaviour
{
public float moveSpeed = 5.0f;
public float rotateSpeed = 5.0f;
public GameObject bullet;
public Transform bulletPosition;
private Vector3 moveDirection;
private Rigidbody rb;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Instantiate(bullet, bulletPosition.position, bulletPosition.rotation);
}
}
void FixedUpdate()
{
#if !MOBILE_INPUT
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0 , Input.GetAxis("Vertical"));
#else
moveDirection = new Vector3(CrossPlatformInputManager.GetAxis("Horizontal"), 0 , CrossPlatformInputManager.GetAxis("Vertical"));
#endif
Move();
Turn();
}
void Move()
{
moveDirection = moveDirection.normalized * moveSpeed * Time.deltaTime;
rb.MovePosition(transform.position + moveDirection);
}
void Turn()
{
if (moveDirection.x != 0 || moveDirection.z != 0)
{
//rotate player
Vector3 faceDirection = moveDirection + transform.position;
faceDirection.y = transform.position.y;
if ((faceDirection - transform.position) == Vector3.zero)
{
return;
}
Quaternion rotation = Quaternion.LookRotation(faceDirection - transform.position);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, rotateSpeed * Time.deltaTime);
}
}
}
using UnityEngine;
public class BulletController : MonoBehaviour
{
public float speed = 5.0f;
// Update is called once per frame
void Update()
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
}
NO GAME NO DREAM!!!!