【发布时间】:2015-04-05 20:19:00
【问题描述】:
我想将我的游戏改造成 Android 游戏,就像乒乓球一样。我希望能够在手机上拖动拨片。提前感谢您的帮助。这是我最旧的代码:
using UnityEngine;
using System.Collections;
public class MoveRacket : MonoBehaviour {
public float speed = 30;
public string axis = "Vertical";
void FixedUpdate () {
float v = Input.GetAxisRaw (axis);
GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, v) * speed;
}
}
这是我的旧代码,但它仍然无法正常工作。
using UnityEngine;
using System.Collections;
public class MoveRacket : MonoBehaviour {
public float speed = 30;
public string axis = "Vertical";
public object racket = "Racket";
public bool touchInput = true;
public Vector2 touchPos;
void FixedUpdate () {
//used to not have anything in parentheses
float v = Input.GetAxisRaw (axis);
//GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, v) * speed;
if (Input.touchCount == 1)
{
Vector3 wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
Vector2 touchPos = new Vector2(wp.x, wp.y);
if (racket == Physics2D.OverlapPoint(touchPos));
{
GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, v) * speed;
}
}
}
}
这是我当前已修复的代码。
using UnityEngine;
using System.Collections;
public class MoveRacket : MonoBehaviour {
public float speed = 30;
public string axis = "Vertical";
public object racket = "Racket";
public bool touchInput = true;
public Vector2 touchPos;
void FixedUpdate () {
//used to not have anything in parentheses
//float v = Input.GetAxisRaw (axis);
float v = Input.GetAxisRaw (axis);
GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, v) * speed;
if (Input.touchCount == 1)
{
Vector3 wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
Vector2 touchPos = new Vector2(wp.x, wp.y);
if (Racket.Collider2D == Physics2D.OverlapPoint(touchPos));
{
this.transform.position.y = wp.y
}
}
}
}
回答:上面的代码是固定的,应该可以使用。
【问题讨论】:
标签: c# android unity3d touch 2d