为什么在游戏首次运行时,将物体绕Y轴旋转时,物体会立即旋转180度?

huangapple go评论92阅读模式
英文:

Why when rotating an object on the y axis when running the game first the object is rotating at once by 180 degrees?

问题

脚本使对象根据速度和角度值从一侧旋转到另一侧。

但当我第一次运行游戏时,对象立刻旋转了180度,然后才开始根据速度和角度从一侧旋转到另一侧。但我希望它从原始旋转0,0,0开始旋转,而不是从180度开始。

在运行游戏之前,子对象Pod_Weapon_L的旋转为0,0,0

然后,在运行游戏的第一帧中,子对象Pod_Weapon_L的旋转变为0,176.51,0

我无法弄清楚为什么会发生这种情况。

这是附加到Pod_Weapon_L的脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WeaponsController : MonoBehaviour
{
    [SerializeField] private float rotationSpeed = 10f;
    [SerializeField] private float rotationAngle = 45f;

    private float currentAngle;
    private bool rotateClockwise = true;

    private void Update()
    {
        // 根据旋转速度和时间计算旋转量
        float rotationAmount = rotationSpeed * Time.deltaTime;

        // 根据旋转方向更新当前角度
        currentAngle += rotateClockwise ? rotationAmount : -rotationAmount;

        // 检查当前角度是否超过指定的角度限制
        if (Mathf.Abs(currentAngle) >= rotationAngle)
        {
            // 反转旋转方向
            rotateClockwise = !rotateClockwise;
        }

        // 将旋转应用于对象
        transform.rotation = Quaternion.Euler(0f, currentAngle, 0f);
    }
}

我希望它从0,0,0开始旋转,而不是从0,176.51,0开始。

英文:

The script make the object to rotate from side to side depending on the speed and angle values.

but when I'm running the game first time the object is rotating at once by 180 degrees and then it start rotating from side to side depending on the speed and angle. but I want it to start rotating from side to side from it's original rotation 0,0,0 and not from 180.

The object before running the game the rotation of the child Pod_Weapon_L is 0,0,0

为什么在游戏首次运行时,将物体绕Y轴旋转时,物体会立即旋转180度?

then when running the game at the first frame the child Pod_Weapon_L rotation is changing to 0,176.51,0

为什么在游戏首次运行时,将物体绕Y轴旋转时,物体会立即旋转180度?

I can't figure out why it happens.

This is the script attached to the Pod_Weapon_L:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WeaponsController : MonoBehaviour
{
    [SerializeField] private float rotationSpeed = 10f;
    [SerializeField] private float rotationAngle = 45f;

    private float currentAngle;
    private bool rotateClockwise = true;

    private void Update()
    {
        // Calculate the rotation amount based on the rotation speed and time
        float rotationAmount = rotationSpeed * Time.deltaTime;

        // Update the current angle based on the rotation direction
        currentAngle += rotateClockwise ? rotationAmount : -rotationAmount;

        // Check if the current angle exceeds the specified angle limit
        if (Mathf.Abs(currentAngle) >= rotationAngle)
        {
            // Reverse the rotation direction
            rotateClockwise = !rotateClockwise;
        }

        // Apply the rotation to the object
        transform.rotation = Quaternion.Euler(0f, currentAngle, 0f);
    }
}

I want it to start rotating from 0,0,0 not from 0,176.51,0

答案1

得分: 1

在第一个帧中以编程方式初始化对象。

public class WeaponsController : MonoBehaviour
{
    [SerializeField] private float rotationSpeed = 10f;
    [SerializeField] private float rotationAngle = 45f;

    private float currentAngle;
    private bool rotateClockwise = true;
    private bool initiated = false;

    private void Update()
    {
        if (!initiated)
        {
            transform.rotation = Quaternion.Euler(0f, 0f, 0f);
        }
        // 根据旋转速度和时间计算旋转量
        float rotationAmount = rotationSpeed * Time.deltaTime;

        // 根据旋转方向更新当前角度
        currentAngle += rotateClockwise ? rotationAmount : -rotationAmount;

        // 检查当前角度是否超过指定的角度限制
        if (Mathf.Abs(currentAngle) >= rotationAngle)
        {
            // 反转旋转方向
            rotateClockwise = !rotateClockwise;
        }

        // 将旋转应用于对象
        transform.rotation = Quaternion.Euler(0f, currentAngle, 0f);
    }
}
英文:

Initiate the object programmatically in the first frame.

public class WeaponsController : MonoBehaviour
{
    [SerializeField] private float rotationSpeed = 10f;
    [SerializeField] private float rotationAngle = 45f;

    private float currentAngle;
    private bool rotateClockwise = true;
    private bool initiated = false;

    private void Update()
    {
        if(!initiated){
            transform.rotation = Quaternion.Euler(0f, 0f, 0f);
        }
        // Calculate the rotation amount based on the rotation speed and time
        float rotationAmount = rotationSpeed * Time.deltaTime;

        // Update the current angle based on the rotation direction
        currentAngle += rotateClockwise ? rotationAmount : -rotationAmount;

        // Check if the current angle exceeds the specified angle limit
        if (Mathf.Abs(currentAngle) >= rotationAngle)
        {
            // Reverse the rotation direction
            rotateClockwise = !rotateClockwise;
        }

        // Apply the rotation to the object
        transform.rotation = Quaternion.Euler(0f, currentAngle, 0f);
    }
}

huangapple
  • 本文由 发表于 2023年7月31日 23:27:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76805059.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定