Android 将画布内的游戏对象移动到接触点
我是Unity 3D的新手,一直在跟随我的教程学习该平台。 我在课程中开发了一个游戏GoHippoGo,一个河马的图像在屏幕上移动到触摸点 虽然当我尝试使用画布和面板等。。。为了使游戏适合所有屏幕大小,河马停止了移动!!触摸时,它只会从屏幕上掉下来(速度太慢),而不是移动到触摸点 我尝试在互联网上搜索,甚至是unity论坛,但找不到解决方案。 我的错误可能很明显,但由于我是新手,请配合:P 多谢各位 以下是我的画布截图: 这是我的MoveHippo脚本:Android 将画布内的游戏对象移动到接触点,android,canvas,unity3d,gameobject,Android,Canvas,Unity3d,Gameobject,我是Unity 3D的新手,一直在跟随我的教程学习该平台。 我在课程中开发了一个游戏GoHippoGo,一个河马的图像在屏幕上移动到触摸点 虽然当我尝试使用画布和面板等。。。为了使游戏适合所有屏幕大小,河马停止了移动!!触摸时,它只会从屏幕上掉下来(速度太慢),而不是移动到触摸点 我尝试在互联网上搜索,甚至是unity论坛,但找不到解决方案。 我的错误可能很明显,但由于我是新手,请配合:P 多谢各位 以下是我的画布截图: 这是我的MoveHippo脚本: using UnityEngine;
using UnityEngine;
using System.Collections;
public class MoveHippo : MonoBehaviour {
private float lastTouchTime, currentTouchTime;
public float velocityVal;
public float torqueVal;
public float thresholdTime;
void Awake() {
velocityVal = 8.0f;
torqueVal = 200.0f;
thresholdTime = 0.3f;
}
void Update () {
#if UNITY_ANDROID
moveHippoAndroid ();
#endif
#if UNITY_EDITOR
moveHippo();
#endif
}
void moveHippo() { //For testing only in your COMPUTER
Vector3 currentPos, touchedPos, distanceVec;
if (Input.GetMouseButtonDown(0)) {
startRotatingHippoAndStopIt();
}
else if (Input.GetMouseButtonUp(0)) {
currentPos = Camera.main.WorldToScreenPoint (transform.position);
touchedPos = Input.mousePosition;
distanceVec = (touchedPos - currentPos).normalized;
stopRotatingHippoAndMoveIt(distanceVec, velocityVal);
}
}
void moveHippoAndroid() {
Vector3 currentPos, touchedPos, distanceVec;
for (int i = 0; i < Input.touches.Length; i++) {
Touch touch = Input.GetTouch(i);
currentPos = Camera.main.WorldToScreenPoint(transform.position);
touchedPos = touch.position;
distanceVec = (touchedPos - currentPos).normalized;
if (Input.GetTouch(0).phase == TouchPhase.Began) {
startRotatingHippoAndStopIt();
} else if (Input.GetTouch(0).phase == TouchPhase.Ended){
currentTouchTime = Time.time;
if (currentTouchTime - lastTouchTime > thresholdTime) { //No Double Touch detected ...
lastTouchTime = Time.time;
stopRotatingHippoAndMoveIt(distanceVec, velocityVal);
} else if (currentTouchTime - lastTouchTime < thresholdTime){ //Double Touch detected!
lastTouchTime = Time.time;
stopRotatingHippoAndMoveIt(distanceVec, velocityVal*2.0f);
}
}
}
}
void startRotatingHippoAndStopIt() {
// We rorate the hippo...
GetComponent<Rigidbody2D>().fixedAngle = false;
GetComponent<Rigidbody2D>().AddTorque(torqueVal);
// ... and stop it
GetComponent<Rigidbody2D>().velocity=Vector2.zero;
}
void stopRotatingHippoAndMoveIt(Vector3 distanceVec, float velocity) {
// We stop rotating the hippo...
Quaternion hippoQuatern = new Quaternion();
hippoQuatern.eulerAngles = new Vector3(0,0,0);
GetComponent<Rigidbody2D>().fixedAngle = true;
GetComponent<Rigidbody2D>().transform.rotation = hippoQuatern;
// ... and move it.
GetComponent<Rigidbody2D>().velocity = distanceVec*velocity;
}
}
使用UnityEngine;
使用系统集合;
公共类河马:单一行为{
私有浮动lastTouchTime、currentTouchTime;
公共浮动速度;
公共浮动汇率;
公共浮动阈值时间;
无效唤醒(){
速度瓦尔=8.0f;
扭矩值=200.0f;
阈值时间=0.3f;
}
无效更新(){
#如果你是安卓
河马类();
#恩迪夫
#如果统一编辑器
河马();
#恩迪夫
}
void moveHippo(){//仅用于计算机中的测试
矢量3 currentPos、touchedPos、distanceVec;
if(Input.GetMouseButtonDown(0)){
startRotatingHippoAndStopIt();
}
else if(Input.GetMouseButtonUp(0)){
currentPos=Camera.main.WorldToScreenPoint(变换位置);
touchedPos=Input.mousePosition;
距离向量=(touchedPos-currentPos).标准化;
停止旋转HIPPOA并移动它(距离向量、速度向量);
}
}
void moveHippoAndroid(){
矢量3 currentPos、touchedPos、distanceVec;
for(int i=0;ithresholdTime){//未检测到双击。。。
lastTouchTime=Time.Time;
停止旋转HIPPOA并移动它(距离向量、速度向量);
}否则如果(currentTouchTime-lastTouchTime
尝试使用(RectTransform)transform.position
或GetComponent().position
(或简单地使用transform.position
),但我想记住我处理的是RectTransform而不是标准的transform),而不是Camera.main.WorldToScreenPoint(transform.position)
:由于您的画布设置为屏幕空间-覆盖,因此RectTransform组件的全局位置将返回像素坐标中的X、Y、Z值
如果屏幕为800x600且对象居中,
GetComponent().position
将返回(400.0f、300.0f、0.0f)。MoveHippo脚本附加到了什么?相机还是河马?河马的动作脚本附在河马身上。河马按照你说的做了,现在河马正朝接触点移动,但速度非常慢。速度设置为8.0f,但似乎从未到达目的地。我明白了问题所在,整个场景视图只有1个单位。当我记录distanceVec时,它显示x,y坐标b/w 0-1的值,z为0。我不知道如何解决这个问题。@ayuschjain场景视图实际上不是1个单位:distanceVec值介于-1和1之间,因为向量是标准化的(distanceVec.z=0,因为使用画布时处于2D中:z与深度“相关”)。如果你想提高河马的速度,只需提高速度(大约100.0f)。如果你想实现一种缓慢的制动,你必须为Rigidbody2D.Linear Drag属性设置一个值(类似于0.5f)。当我像我的教程一样做时,即在不使用画布的情况下,distanceVec值不是b/w-1到1,它大于这个值。向量在那里也被归一化了。我甚至试图增加速度值,但没有用……:(@ayuschjain Uhm…我觉得有点不对劲:如果你规范化一个向量,它的长度(大小)将变为1,因此它的任何分量都不能小于-1,也不能大于1(请查找Vector3.normalized
)此外,我还尝试了你的脚本,用我在之前的评论中给出的值对Rigidbody2D进行了修改,效果很好。