C# 如何用触摸输入旋转一个物体,然后缓慢地停止?
我从这里得到了一些代码: 这段代码通过鼠标点击和拖动来旋转一个对象,点击之后,对象缓慢停止。我现在想在手机上使用这个功能。这是我的手机版代码:C# 如何用触摸输入旋转一个物体,然后缓慢地停止?,c#,unity3d,C#,Unity3d,我从这里得到了一些代码: 这段代码通过鼠标点击和拖动来旋转一个对象,点击之后,对象缓慢停止。我现在想在手机上使用这个功能。这是我的手机版代码: // Update is called once per frame public void Update() { // Track a single touch as a direction control. if (Input.touchCount > 0) { Touch touch = Input.Get
// Update is called once per frame
public 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;
stopSlowly = true;
Debug.Log("end");
break;
}
}
if (directionChosen) {
// Something that uses the chosen direction...
theSpeed = new Vector3(-direction.x, direction.y, 0.0F);
avgSpeed = Vector3.Lerp(avgSpeed, theSpeed, Time.deltaTime * 5);
} else {
if (stopSlowly) {
Debug.Log("TESTOUTPUT");
theSpeed = avgSpeed;
isDragging = false;
}
float i = Time.deltaTime * lerpSpeed;
theSpeed = Vector3.Lerp(theSpeed, Vector3.zero, i);
}
transform.Rotate(camera.transform.up * theSpeed.x * rotationSpeed, Space.World);
transform.Rotate(camera.transform.right * theSpeed.y * rotationSpeed, Space.World);
以下是一些变量,我使用Vector2变量表示起始点和方向:
private float rotationSpeed = 1f;
private float lerpSpeed = 1.0F;
private Vector3 theSpeed;
private Vector3 avgSpeed;
private bool isDragging = false;
private Vector3 targetSpeedX;
public Vector2 startPos;
public Vector2 direction;
public bool directionChosen;
public bool stopSlowly;
现在,如果我按play并旋转手机上的对象,它会旋转,但不会自行结束。它的旋转速度也非常快。当我触摸物体一次时,它立即停止。
请有人告诉我我的代码到底出了什么问题。我的目标只是一个旋转,从触摸输入开始初始化,缓慢结束直到静止
谢谢你的情况很令人困惑。 试试这样的
// Handle finger movements based on touch phase.
switch (touch.phase) {
// Record initial touch position.
case TouchPhase.Began:
startPos = touch.position;
break;
// Determine direction by comparing the current touch position with the initial one.
case TouchPhase.Moved:
direction = touch.position - startPos;
isDragging = true;
break;
// Report that a direction has been chosen when the finger is lifted.
case TouchPhase.Ended:
isDragging = false;
stopSlowly = true;
Debug.Log("end");
break;
}
if (isDragging) {
// Something that uses the chosen direction...
theSpeed = new Vector3(-direction.x, direction.y, 0.0F);
avgSpeed = Vector3.Lerp(avgSpeed, theSpeed, Time.deltaTime * 5);
} else {
if(stopSlowly) {
Debug.Log("TESTOUTPUT");
theSpeed = avgSpeed;
stopSlowly = false;
}
float i = Time.deltaTime * lerpSpeed;
theSpeed = Vector3.Lerp(theSpeed, Vector3.zero, i);
}
你的情况很令人困惑。 试试这样的
// Handle finger movements based on touch phase.
switch (touch.phase) {
// Record initial touch position.
case TouchPhase.Began:
startPos = touch.position;
break;
// Determine direction by comparing the current touch position with the initial one.
case TouchPhase.Moved:
direction = touch.position - startPos;
isDragging = true;
break;
// Report that a direction has been chosen when the finger is lifted.
case TouchPhase.Ended:
isDragging = false;
stopSlowly = true;
Debug.Log("end");
break;
}
if (isDragging) {
// Something that uses the chosen direction...
theSpeed = new Vector3(-direction.x, direction.y, 0.0F);
avgSpeed = Vector3.Lerp(avgSpeed, theSpeed, Time.deltaTime * 5);
} else {
if(stopSlowly) {
Debug.Log("TESTOUTPUT");
theSpeed = avgSpeed;
stopSlowly = false;
}
float i = Time.deltaTime * lerpSpeed;
theSpeed = Vector3.Lerp(theSpeed, Vector3.zero, i);
}
我现在用一些物理学来解决这个问题:
// React on User Touch Input -> Rotate gameObject
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
// Get movement of the finger since last frame
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
// Add Torque to gameObject
rb.AddTorque(camera.transform.up * -touchDeltaPosition.x/* * optionalForce*/);
rb.AddTorque(camera.transform.right * touchDeltaPosition.y/* * optionalForce*/);
} else if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended) {
// throw anker, stop rotating slowly
rb.angularDrag = 0.7f;
}
此代码用于触摸输入,旋转对象+使其缓慢下降。你需要在你想要旋转的物体上有一个刚体。这里叫rb。相机是我的主相机。我现在用一些物理来解决这个问题:
// React on User Touch Input -> Rotate gameObject
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
// Get movement of the finger since last frame
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
// Add Torque to gameObject
rb.AddTorque(camera.transform.up * -touchDeltaPosition.x/* * optionalForce*/);
rb.AddTorque(camera.transform.right * touchDeltaPosition.y/* * optionalForce*/);
} else if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended) {
// throw anker, stop rotating slowly
rb.angularDrag = 0.7f;
}
此代码用于触摸输入,旋转对象+使其缓慢下降。你需要在你想要旋转的物体上有一个刚体。这里叫rb。照相机是我的主照相机。嗨,很抱歉回答晚了。我用刚体和一些物理知识解决了这个问题。谢谢你的回答:)嗨,很抱歉回答晚了。我用刚体和一些物理知识解决了这个问题。谢谢你的回答:)