java移动小人游戏

阅读: 评论:0

java移动小人游戏

java移动小人游戏

我是Unity新手并开发移动2D游戏,现在当我在屏幕中心之前或之后触摸屏幕时,我能够使对象左右移动 . 但我想触摸对象并将其拖动到x轴上,同时我的手指仍然触摸屏幕并移动,所以我希望对象位于我手指的相同x位置,Any One可以帮助我如何做到这一点正确:这是我在屏幕中心之前或之后触摸时如何移动对象的代码:

public class paddle : MonoBehaviour {

public Rigidbody2D rb;

public float speed;

public float maxX;

bool currentisAndroid=false;

// Use this for initialization

void Start () {

rb = GetComponent ();

#if UNITY_ANDROID

currentisAndroid=true;

#else

currentisAndroid=false;

#endif

}

// Update is called once per frame

void Update () {

if (currentisAndroid == true) {

if (Input.GetTouch (0).position.x < Screen.width/2 && Input.GetTouch (0).phase == TouchPhase.Stationary)

moveLeft ();

else if (Input.GetTouch (0).position.x > Screen.width/2 && Input.GetTouch (0).phase == TouchPhase.Stationary)

moveRight ();

else

stop ();

} else {

float x = Input.GetAxis ("Horizontal");

//if (Input.GetTouch (0).position.x == rb.position.x && Input.GetTouch (0).phase == TouchPhase.Moved)

if (x == 0)

stop ();

if (x < 0)

moveLeft ();

if (x > 0)

moveRight ();

Vector2 pos = transform.position;

pos.x=Mathf.Clamp (pos.x,-maxX,maxX);

transform.position = pos;

}

}

void moveLeft()

{

rb.velocity = new Vector2 (-speed, 0);

}

void moveRight()

{

rb.velocity = new Vector2 (speed, 0);

}

void stop()

{

rb.velocity = new Vector2 (0, 0);

}

public float getposition()

{

return rb.position.y;

}

}

本文发布于:2024-02-02 14:03:52,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170685383344291.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:小人   游戏   java
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23