gpt4 book ai didi

c# - 我如何移动具有触摸统一方向的对象

转载 作者:行者123 更新时间:2023-11-30 23:17:55 26 4
gpt4 key购买 nike

我是游戏开发的初学者。我正在开发一款游戏,我有一个球,我想通过触摸来移动它。我希望它像足球比赛一样移动,球可以沿着触摸的方向移动。

不过,眼下看来只能往前走!这是我的代码:

 public float ThroughSpped;
public float ArchSpped;
void OnMouseDown() {

distance = Vector3.Distance (transform.position, Camera.main.transform.position);
Dragging = true;

}
public void OnMouseUp()
{
//Instantiate(this) ;
this.GetComponent<Rigidbody> ().useGravity = true;



this.GetComponent<Rigidbody> ().velocity += this.transform.forward*ThroughSpped;
this.GetComponent<Rigidbody> ().velocity += this.transform.up*ArchSpped;
Dragging = false;

}

void DraggingBall()
{
if (Dragging)
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
Vector3 raypoint = ray.GetPoint (distance);
transform.position = Vector3.Lerp (this.transform.position, raypoint, Speed * Time.deltaTime);
Destroy(BallPrefabClone, 1f);
}
}

最佳答案

可以通过touch phase获取触摸的起始位置和触摸的结束位置来计算触摸的矢量/方向

然后使用这个新向量为你的球增加力

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
public Vector2 startPos;
public Vector2 direction;
public bool directionChosen;
void Update()
{
// Track a single touch as a direction control.
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);

// Handle finger movements based on touch phase.
switch (touch.phase)
{
// Record initial touch position.
case TouchPhase.Began:
startPos = touch.position;
directionChosen = false;
break;

// Determine direction by comparing the current touch position with the initial one.
case TouchPhase.Moved:
direction = touch.position - startPos;
break;

// Report that a direction has been chosen when the finger is lifted.
case TouchPhase.Ended:
directionChosen = true;
break;
}
}
if (directionChosen)
{
// Something that uses the chosen direction...
}
}
}

https://docs.unity3d.com/ScriptReference/Touch-phase.html

关于c# - 我如何移动具有触摸统一方向的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41156872/

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