0
\$\begingroup\$

I want to make it so that, when I'm changing the rotationAngle Range slider in the inspector at runtime, it will change the transform rotation angle, but it's not working. If I'm only changing it to 360 degrees then it's doing 360 rotation but then when I'm changing the slider back to any other smaller value it's not affecting it anymore.

And also, when changing the rotationAngle, then it's not rotating from side to side, it's rotating only in one direction. For example 360 then it keeps rotating only in one direction instead of changing direction when getting to 360.

It works only when running it for the first time, but then after that, the rotationAngle is not working as expected.

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

public class WeaponsController : MonoBehaviour
{
    [Range(0f, 100f)]
    [SerializeField] private float rotationSpeed = 10f;
    [Range(0,360)]
    [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.localRotation = Quaternion.Euler(0f, currentAngle, 0f);
    }
}
\$\endgroup\$
1

1 Answer 1

0
\$\begingroup\$

This is working as I expected. maybe there is a better way to write the script but it's working.

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

public class WeaponsController : MonoBehaviour
{
    [Range(0f, 1000f)]
    public float rotationSpeed = 10f;
    [Range(0f, 360f)]
    public float rotationAngle = 45f;
    public bool rotateClockwise = true;

    private float currentAngle;

    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
        if (rotateClockwise)
        {
            currentAngle += rotationAmount;
            if (currentAngle >= rotationAngle)
            {
                currentAngle = rotationAngle;
                rotateClockwise = false;
            }
        }
        else
        {
            currentAngle -= rotationAmount;
            if (currentAngle <= -rotationAngle)
            {
                currentAngle = -rotationAngle;
                rotateClockwise = true;
            }
        }

        // Apply the rotation to the object
        transform.localRotation = Quaternion.Euler(0f, currentAngle, 0f);
    }
}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ This answer would be better if it explained what changed in this code and why that change solves the problem. You can click the checkmark in the top-left of the answer to mark it as "Accepted" if it worked for you. \$\endgroup\$
    – DMGregory
    Commented Aug 2, 2023 at 17:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .