gpt4 book ai didi

c# - 在 unity 2D c# 中如何旋转几何体破折号之类的对象?

转载 作者:行者123 更新时间:2023-12-04 07:36:09 25 4
gpt4 key购买 nike

my game ,我的主角是一个立方体,如果按下空格键,它会根据您按下按钮的大小而跳跃,但我想在按下空格键时将我的立方体旋转 90 度。我不确定如何实现这一目标。
这是我目前正在尝试的

if (Input.GetKey(KeyCode.Space)) 
{
timeCount=Time.time
}

if (Input.GetKeyUp(KeyCode.Space))
{
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, 0, 90), timeCount*.1f);
timeCount = 0;
}
编辑:这是我用你的代码更新的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerEverything : MonoBehaviour{
public Transform GroundCheck; // Put the prefab of the ground here

public LayerMask groundLayerBlue; // Insert the layer here.
public LayerMask groundLayerPurple;
public bool isGroundedBlue;
public bool isGroundedPurple;
private Rigidbody2D rb2D;
private float jumpTimer;
private float timeCount = 0.0f;
private bool isGrounded;
public Transform groundCheck;
public LayerMask groundLayer;
// we are going to store our coroutine to assure that it is only going to run once
private Coroutine RotationCoroutine = null;

[SerializeField] private float degreesToRotateBy; // assign this variable to 90.0f in the inspector
[SerializeField] private float timeToRotate; // assign this variable to however much time you want the rotation to occur

// axis of rotation that will occur
private Vector3 rotationAxis = Vector3.forward;

// Start is called before the first frame update
public void Start()
{
rb2D = GetComponent<Rigidbody2D>();
}

// Update is called once per frame
public void Update()
{
isGroundedBlue = Physics2D.OverlapCircle(GroundCheck.position, 0.15f, groundLayerBlue);
if (isGroundedBlue)
{
transform.position += Vector3.right/60;
}

isGroundedPurple = Physics2D.OverlapCircle(GroundCheck.position, 0.15f, groundLayerPurple);
if (isGroundedPurple)
{
transform.position += Vector3.left / 60;
}

if (isGrounded)
{
if (Input.GetKey(KeyCode.Space))
{
jumpTimer += Time.deltaTime;
timeCount = Time.time;
}

if (Input.GetKeyUp(KeyCode.Space))
{
DoJump(250f * jumpTimer);

jumpTimer = 0;
}

// I changed your input to GetKeyDown instead of GetKey
// as GetKeyDown is triggered the single frame the user presses space instead of every frame
if (Input.GetKeyDown(KeyCode.Space))
{
if (RotationCoroutine == null)
RotationCoroutine = StartCoroutine(RotateAxisByDegrees(degreesToRotateBy, timeToRotate, rotationAxis));
}

if (Input.GetKeyUp(KeyCode.Space))
{
// assuming you want the rotation to stop
if (RotationCoroutine != null)
StopCoroutine(RotationCoroutine);

RotationCoroutine = null;
}
}

}

/// <summary>
/// Rotate an object by a set angle in the rotationAxis over a set time
/// </summary>
/// <param name="angleToRotateTo">Angle we are rotating by</param>
/// <param name="timeToRotate">Amount of time the rotation takes</param>
/// <param name="rotationAxis">Axis to rotate around</param>
/// <returns></returns>
private IEnumerator RotateAxisByDegrees(float angleToRotateTo, float timeToRotate, Vector3 rotationAxis)
{
// store our initial rotation and keep a timer for the rotation progress
float currentTimeElapsed = 0.0f;
Quaternion startRotation = transform.rotation;
Quaternion endRotation = startRotation * Quaternion.AngleAxis(angleToRotateTo, rotationAxis);

while (currentTimeElapsed < timeToRotate)
{
// set our new rotation
transform.rotation = Quaternion.Slerp(startRotation, endRotation, currentTimeElapsed / timeToRotate);

// increment our timer with how long it has been since the last frame
currentTimeElapsed += Time.deltaTime;

// this line will tell the coroutine to end here and continue work in the next frame
yield return null;
}

// assign our rotation in case there are floating point errors
transform.rotation = endRotation;

// set our coroutine to null as we are done
RotationCoroutine = null;
}

private void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
}

public void DoJump(float JumpForce)
{
float jumpForceMagnitutde = Mathf.Clamp(JumpForce/3, 0, 50);
rb2D.AddForce(Vector2.up * jumpForceMagnitutde/3, ForceMode2D.Impulse);
rb2D.AddForce(Vector2.right*jumpForceMagnitutde/10,ForceMode2D.Impulse);

}
}

最佳答案

正如我所提到的,您发布的示例代码有一个主要问题

if (Input.GetKey(KeyCode.Space)) 
{
timeCount=Time.time
}

if (Input.GetKeyUp(KeyCode.Space))
{
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, 0, 90), timeCount*.1f);
timeCount = 0;
}
Lerp 是一个在多个帧上逐渐完成的过程。我假设此输入代码位于 Update 内函数,通常允许 Lerp上类。然而,由于它是在空间提升的条件检查中,这只会发生 single frame 用户提起一个键,它不会连续运行。
在某种事件(在你的情况下是跳跃)发生后的情况下,我需要逐步完成一项任务,我通常会求助于 Coroutines .一个 Coroutine是一个函数,它能够在每一帧中以小块的形式处理进程,并在下一帧中返回到它停止的地方。
// we are going to store our coroutine to assure that it is only going to run once
private Coroutine RotationCoroutine = null;

[SerializeField] private float degreesToRotateBy; // assign this variable to 90.0f in the inspector
[SerializeField] private float timeToRotate; // assign this variable to however much time you want the rotation to occur

// axis of rotation that will occur
private Vector3 zRotationAxis = Vector3.forward;

private void Update()
{
// I changed your input to GetKeyDown instead of GetKey
// as GetKeyDown is triggered the single frame the user presses space instead of every frame
if (Input.GetKeyDown(KeyCode.Space))
{
if (RotationCoroutine == null)
RotationCoroutine = StartCoroutine(RotateAxisByDegrees(degreesToRotateBy, timeToRotate, zRotationAxis));
}

if (Input.GetKeyUp(KeyCode.Space))
{
// assuming you want the rotation to stop
if (RotationCoroutine != null)
StopCoroutine(RotationCoroutine);

RotationCoroutine = null;
}
}

/// <summary>
/// Rotate an object by a set angle in the rotationAxis over a set time
/// </summary>
/// <param name="angleToRotateTo">Angle we are rotating by</param>
/// <param name="timeToRotate">Amount of time the rotation takes</param>
/// <param name="rotationAxis">Axis to rotate around</param>
/// <returns></returns>
private IEnumerator RotateAxisByDegrees(float angleToRotateTo, float timeToRotate, Vector3 rotationAxis)
{
// store our initial rotation and keep a timer for the rotation progress
float currentTimeElapsed = 0.0f;
Quaternion startRotation = transform.rotation;
Quaternion endRotation = startRotation * Quaternion.AngleAxis(angleToRotateTo, rotationAxis);

while (currentTimeElapsed < timeToRotate)
{
// set our new rotation
transform.rotation = Quaternion.Slerp(startRotation, endRotation, currentTimeElapsed / timeToRotate);

// increment our timer with how long it has been since the last frame
currentTimeElapsed += Time.deltaTime;

// this line will tell the coroutine to end here and continue work in the next frame
yield return null;
}

// assign our rotation in case there are floating point errors
transform.rotation = endRotation;

// set our coroutine to null as we are done
RotationCoroutine = null;
}
我目前实现了在特定时间内完成的轮换。可以改为旋转给定速度的对象。您还可以扩展该函数以接收特定轴,以创建一个函数,该函数允许在任何时间以任何角度在任何轴上旋转。只需添加新参数,不要使轴全局化。
如果您想了解更多 Lerp ,有一个有用的 article 在处理它们时会介绍一些一般做法。
编辑:为了澄清当前代码不起作用的原因,这是因为您正在对 GetKeyUp 进行 if 条件检查。您的内部 isGrounded查看。
if (isGrounded)
{
if (Input.GetKey(KeyCode.Space))
{
jumpTimer += Time.deltaTime;
timeCount = Time.time;
}

if (Input.GetKeyUp(KeyCode.Space))
{
DoJump(250f * jumpTimer);

jumpTimer = 0;
}

// I changed your input to GetKeyDown instead of GetKey
// as GetKeyDown is triggered the single frame the user presses space instead of every frame
if (Input.GetKeyDown(KeyCode.Space))
{
if (RotationCoroutine == null)
RotationCoroutine = StartCoroutine(RotateAxisByDegrees(degreesToRotateBy, timeToRotate, rotationAxis));
}
}

// this needs to be outside of the isGrounded
if (Input.GetKeyUp(KeyCode.Space))
{
// assuming you want the rotation to stop
if (RotationCoroutine != null)
StopCoroutine(RotationCoroutine);

RotationCoroutine = null;
}

关于c# - 在 unity 2D c# 中如何旋转几何体破折号之类的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67729473/

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