Afin de ne pas faire d'erreurs concernant les plages d'angles,j'ai codé ceci:
Code : Tout sélectionner
float NormalizeAngle(float angle)
{
Angle = angle % 360f;
if (Angle < 0)
{
Angle += 360f;
}
return Angle;
}
Vector3 NormalizeEulerAngles(Vector3 eulerAngles)
{
float x = NormalizeAngle(eulerAngles.x);
float y = NormalizeAngle(eulerAngles.y);
float z = NormalizeAngle(eulerAngles.z);
return new Vector3(x, y, z);
}
La fonction Start assigne les composantes de rotation initiales (-90,0,0) à mon objet
Si j'appuie sur la touche R, il prendra la composante de rotation suivante:(-90,,90,-90)
Ce code :
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class rotations_2 : MonoBehaviour
{
public GameObject obj;
public float rot_init_x, rot_init_y, rot_init_z, rot_x, rot_y, rot_z;
public KeyCode tourne;
float Angle;
float NormalizeAngle(float angle)
{
Angle = angle % 360f;
if (Angle < 0)
{
Angle += 360f;
}
return Angle;
}
Vector3 NormalizeEulerAngles(Vector3 eulerAngles)
{
float x = NormalizeAngle(eulerAngles.x);
float y = NormalizeAngle(eulerAngles.y);
float z = NormalizeAngle(eulerAngles.z);
return new Vector3(x, y, z);
}
// Start is called before the first frame update
void Start()
{
// Rotation initiale
obj.transform.eulerAngles = NormalizeEulerAngles(new Vector3(rot_init_x, rot_init_y, rot_init_z));
print("Rotation initiale: " + NormalizeEulerAngles(obj.GetComponent<Transform>().eulerAngles));
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(tourne))
{
// Stockez les rotations actuelles
Vector3 currentRotation = NormalizeEulerAngles(obj.GetComponent<Transform>().eulerAngles);
// Appliquer la rotation souhaitée
obj.transform.Rotate(rot_x, rot_y, rot_z, Space.Self);
// Ajouter la rotation actuelle
obj.transform.eulerAngles += currentRotation;
// Normaliser les angles
obj.transform.eulerAngles = NormalizeEulerAngles(obj.transform.eulerAngles);
print("Après rotation: " + NormalizeEulerAngles(obj.GetComponent<Transform>().eulerAngles));
}
}
}
Vous pouvez vérifier.
Peut-on m'aider?
merci de votre aide