1.Untiy移动端屏幕操作之点击左右半屏控制角色左右移动。
先上效果
代码实现
public class MobileContorl : MonoBehaviour
{
//public Transform Player; //用Transform方法会抖动,所以用Rigidbody
public Rigidbody2D rb;
public float Speed = 3;
void Start()
{
}
void Update()
{
//Debug.Log("Touch x" + Camera.main.ScreenToWorldPoint(Input.mousePosition));
if (Input.GetMouseButton(0)) //点击
{
if (Camera.main.ScreenToWorldPoint(Input.mousePosition).x > 0) //点击的位置在右半屏
{
rb.AddForce(Vector2.right * Speed);
//Player.Translate(Vector2.right * Time.deltaTime * Speed); //跟碰撞体碰撞会抖动
}
else
{
rb.AddForce(Vector2.left * Speed);
//Player.Translate(Vector2.left * Time.deltaTime * Speed);
}
}
else
{
rb.velocity = Vector2.zero;
}
}
}
2.Untiy移动端屏幕操作之向上滑动屏幕角色跳跃。
先上效果
代码实现(这里的代码是AI生成的),不禁感叹,这块代码我查API+写代码+调试最少得半个小时,而AI两分钟就生成好了,虽然结果有问题(结果是反的,我向下滑才有用),但是AI提供解题思路还是很强的,我们不可能记住所有的API,AI可以。对于这种通用的功能AI+Debug时间应该比自己从头写速度快得多。
public class CharacterController : MonoBehaviour
{
private Vector2 fingerDownPosition;
private Vector2 fingerUpPosition;
public float minSwipeDistance = 20f;
public float jumpForce = 5f;
private void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
Debug.Log("touch.phase" + " " + touch.phase);
if (touch.phase == TouchPhase.Began) //Touch开始
{
fingerDownPosition = touch.position;
fingerUpPosition = touch.position;
}
if (touch.phase == TouchPhase.Moved) //Touch移动
{
fingerUpPosition = touch.position;
}
if (touch.phase == TouchPhase.Ended)
{
fingerUpPosition = touch.position; //Touch结束
CheckSwipe();
}
}
}
private void CheckSwipe()
{
Debug.Log(VerticalMoveDistance());
//Debug.Log()
if (VerticalMoveDistance() > minSwipeDistance && VerticalMoveDistance() > HorizontalMoveDistance())
{
//if (fingerDownPosition.y - fingerUpPosition.y > 0) // Up swipe
if (fingerDownPosition.y - fingerUpPosition.y < 0) // Up swipe
{
Jump();
}
}
}
private float VerticalMoveDistance()
{
return Mathf.Abs(fingerDownPosition.y - fingerUpPosition.y);
}
private float HorizontalMoveDistance()
{
return Mathf.Abs(fingerDownPosition.x - fingerUpPosition.x);
}
private void Jump()
{
// 在这里添加让角色往上跳的代码
GameObject.Find("Player").GetComponent<PlayerMovement>().MyJump(); //这里是用的原来写好的Jump代码
//GetComponent<Rigidbody>().AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
3.Untiy移动端屏幕操作之向上滑动屏幕角色跳跃,向左滑动角色向左移动,向右滑动角色向右移动。类似地铁酷跑操作。
先上效果
代码实现
public class Controller
{
int swipeThreshold = 200; //注意,这里200是pixel(200分辨率的意思)pixel是相对大小,不是绝对大小,可以理解为比例
private Vector2 fingerDownPosition;
private Vector2 fingerUpPosition;
public void Update()
{
// 检测触摸输入
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
// 记录手指按下的位置
fingerDownPosition = touch.position;
fingerUpPosition = touch.position;
}
if (touch.phase == TouchPhase.Moved)
{
fingerUpPosition = touch.position;
}
//if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Ended)
if (touch.phase == TouchPhase.Ended)
{
// 记录手指移动或抬起的位置
fingerUpPosition = touch.position;
// 检测滑动距离是否超过阈值
if (Vector2.Distance(fingerDownPosition, fingerUpPosition) > swipeThreshold)
{
// 计算滑动的方向
Vector2 swipeDirection = fingerUpPosition - fingerDownPosition;
Debug.Log("swipeDirection.x"+ swipeDirection.x+" "+ "swipeDirection.y"+ swipeDirection.y);
// 如果向左滑动
if (swipeDirection.x < -250 ) //这里为啥么要弄个250pix值呢,原因是因为,当玩家使用手指跳跃
{ //的时候,肯定x坐标肯定不是0,就会触发左右移动。所以我们左右
MoveCharacterLeft(); //各给250玩家跳跃时的误触空间。
}
// 如果向右滑动
else if (swipeDirection.x > 250)
{
MoveCharacterRight();
}
if (swipeDirection.y > 250) //和上面同理
{
Jump();
}
// 重置手指按下的位置
fingerDownPosition = touch.position;
}
}
}
}
private void MoveCharacterLeft()
{
_model.Move(false);
}
private void MoveCharacterRight()
{
_model.Move(true);
}
private void Jump()
{
// 在这里添加让角色往上跳的代码
_model.Jump();
}