gpt4 book ai didi

android - 将 Canvas 内的游戏对象移动到接触点

转载 作者:行者123 更新时间:2023-11-30 00:53:03 24 4
gpt4 key购买 nike

我是 Unity 3D 的新手,正在按照 Udemy 教程学习该平台。我在类(class)中开发了一个游戏 GoHippoGo,其中河马的图像在屏幕上移动到触摸点。

虽然当我尝试使用 Canvas 和面板等...使游戏适合所有屏幕尺寸时,河马停止移动了!!触摸时它只会掉出屏幕(那太慢了),而不是移动到触摸点。

我尝试在整个互联网上搜索,甚至是 unity 论坛,但找不到解决方案。我的错误可能很明显,但由于我是新手,请配合:P

谢谢

这是我的 Canvas 的截图: enter image description here

这是我的 MoveHippo 脚本:

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;
}

}

最佳答案

尝试使用 (RectTransform)transform.positionGetComponent<RectTransform>().position (或者简称为 transform.position,但我喜欢记住我正在处理一个 RectTransform 而不是一个标准的 Transform)而不是 Camera.main.WorldToScreenPoint(transform.position) :由于您的 Canvas 设置为 Screen Space - OverlayRectTransform 组件的全局位置将返回 X、Y、Z 值像素坐标。

如果您的屏幕是 800x600 并且您的对象居中,GetComponent<RectTransform>().position将返回(400.0f、300.0f、0.0f)。

关于android - 将 Canvas 内的游戏对象移动到接触点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40623882/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com