目录
2D游戏控制
简单的上下左右移动
第一种 使用Rigidbody2D
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
private Rigidbody2D rb;
private float moveH, moveV;
[SerializeField] private float moveSpeed = 5.0f;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
private void Update()
{
moveH = Input.GetAxis("Horizontal") * moveSpeed;
moveV = Input.GetAxis("Vertical") * moveSpeed;
}
private void FixedUpdate()
{
rb.velocity = new Vector2(moveH, moveV);
}
}
第二种 上下左右移动加上旋转
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
public int step; //步长
public float velocity = 0.35f; //速度
private int x;
private int y;
private Vector3 playerPos;
void Start()
{
InvokeRepeating("Move", 0, velocity);
x = 0; y = step;
}
void Update()
{
if (Input.GetKey(KeyCode.W))
{
gameObject.transform.localRotation = Quaternion.Euler(0, 0, 0);
x = 0;
y = step;
}
if (Input.GetKey(KeyCode.S))
{
gameObject.transform.localRotation = Quaternion.Euler(0, 0, 180);
x = 0;
y = -step;
}
if (Input.GetKey(KeyCode.A))
{
gameObject.transform.localRotation = Quaternion.Euler(0, 0, 90);
x = -step;
y = 0;
}
if (Input.GetKey(KeyCode.W))
{
gameObject.transform.localRotation = Quaternion.Euler(0, 0, 0);
x = 0;
y = step;
}
if (Input.GetKey(KeyCode.D))
{
gameObject.transform.localRotation = Quaternion.Euler(0, 0, -90);
x = step;
y = 0;
}
}
void Move()
{
playerPos = gameObject.transform.localPosition;
gameObject.transform.localPosition = new Vector3(playerPos.x + x, playerPos.y + y, playerPos.z);
}
}
2D空战飞机的移动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
//RectTransform引用,
RectTransform rectTransform;
// Use this for initialization
void Start()
{
//当对象被放入内存调用
rectTransform = GetComponent<RectTransform>();
}
// Update is called once per frame
void Update()
{
Move();
}
void Move()
{
float x_speed = 0; //横轴速度
float y_speed = 0; //纵轴
if (Input.GetKey(KeyCode.A) == true
&& Input.GetKey(KeyCode.D) == false)
{
x_speed = -30;
}
else if (Input.GetKey(KeyCode.D) == true
&& Input.GetKey(KeyCode.A) == false)
{
x_speed = +30;
}
if (Input.GetKey(KeyCode.W) == true
&& Input.GetKey(KeyCode.S) == false)
{
y_speed = +30;
}
else if (Input.GetKey(KeyCode.S) == true
&& Input.GetKey(KeyCode.W) == false)
{
y_speed = -30;
}
//上下左右斜向的速度一样
if (Mathf.Abs(x_speed) > 1 && Mathf.Abs(y_speed) > 1)
{
x_speed *= Mathf.Sqrt(2) / 2;
y_speed *= Mathf.Sqrt(2) / 2;
}
//取出飞机的当前位置
Vector2 position = rectTransform.anchoredPosition;
//修改x坐标
position.x += x_speed * Time.deltaTime;
position.y += y_speed * Time.deltaTime;
//限制x,y的范围,避免超出屏幕
position.x = Mathf.Clamp(position.x, -2.3f, 2.3f);
position.y = Mathf.Clamp(position.y, -4.5f, 4.2f);
//位置修改后再放回去
rectTransform.anchoredPosition = position;
}
}
汽车、坦克等移动
坦克的控制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TankMove : MonoBehaviour
{
public float tankSpeed = 10f;
public float turnSpeed = 150;
private Rigidbody tanklRigidbody;
private float tankMoveInputValue;
private float tankTurnInputValue;
public void Awake()
{
tanklRigidbody = GetComponent<Rigidbody>();
}
private void OnEnable()
{
tanklRigidbody.isKinematic = false;
tankMoveInputValue = 0f;
tankTurnInputValue = 0f;
}
private void OnDisable()
{
tanklRigidbody.isKinematic = true;
}
private void Update()
{
tankMoveInputValue = Input.GetAxis("Vertical");
tankTurnInputValue = Input.GetAxis("Horizontal");
}
private void FixedUpdate()
{
Move();
Turn();
}
private void Move()
{
Vector3 move = transform.forward * tankMoveInputValue * tankSpeed * Time.deltaTime;
tanklRigidbody.MovePosition(tanklRigidbody.position + move);
}
private void Turn()
{
float turn = tankTurnInputValue * turnSpeed * Time.deltaTime;
Quaternion turnRotation = Quaternion.Euler(0f, turn, 0f);
tanklRigidbody.MoveRotation(tanklRigidbody.rotation * turnRotation);
}
}