需求:在游戏界面中,实现物体从屏幕左边出去,从右边进来的功能。也可对横向、纵向的运动进行限制。以及物体是如何在游戏中实现键盘控制移动的。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Movement(horizontal, vertical);
}
private void Movement(float horizontal, float vertical)
{
horizontal *= moveSpeed * Time.deltaTime;
vertical *= moveSpeed * Time.deltaTime;
//判断物体的屏幕坐标
//世界坐标转换为屏幕坐标
Vector3 screenPoint = Camera.main.WorldToScreenPoint(this.transform.position);
//限制物体在屏幕中的运动:
//1. 如果超过屏幕,停止运动
//if (screenPoint.x >= Screen.width && horizontal > 0 || screenPoint.x <= 0 && horizontal < 0)
//{
// horizontal = 0;
//}
//2. 左出右进,右出左进;上出下进,下出上进
if (screenPoint.x > Screen.width)
{
screenPoint.x = 0;
}
else if (screenPoint.x < 0)
{
screenPoint.x = Screen.width;
}
if (screenPoint.y > Screen.height)
{
screenPoint.y = 0;
}
else if (screenPoint.y < 0)
{
screenPoint.y = Screen.height;
}
this.transform.position = Camera.main.ScreenToWorldPoint(screenPoint);
this.transform.Translate(horizontal, 0, vertical);
}
}