1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour
{
//public float speedH = 2.0f;
//public float speedV = 2.0f;
//private float yaw = 0.0f;
//private float pitch = 0.0f;
private float speed;
private float rotSpeed;
Vector3 mouseWorldPosition;
float angle;
void Start()
{
rotSpeed = 180f;
this.speed = 2.0f;
}
void FixedUpdate()
{
this.Move();
this.Turn();
}
private void Move()
{
Vector3 rot = new Vector3(transform.rotation.x, transform.rotation.y, transform.rotation.z);
float y = Mathf.Sin(rot.z);
float x = Mathf.Cos(rot.z);
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.position += new Vector3(-x,0,0) * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.RightArrow))
{
transform.position += new Vector3(x, 0, 0) * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.DownArrow))
{
transform.position += new Vector3(0,y,0) * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.UpArrow))
{
transform.position += new Vector3(0, -y, 0) * speed * Time.deltaTime;
}
}
private void Turn()
{
angle = AngleBetweenPoints(transform.position, mouseWorldPosition);
mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition + Vector3.forward * 5f);
transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, angle - rotSpeed));
}
private float AngleBetweenPoints(Vector2 a, Vector2 b)
{
return Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg;
}
} |
Partager