[RESOLU] problème de rotation

Pour les scripts écrits en C#
Règles du forum
Merci de respecter la NOMENCLATURE suivante pour vos TITRES de messages :

Commencez par le niveau de vos scripts
DB = Débutant
MY = Moyen
CF = Confirmé

Puis le domaine d'application
-RS = Réseau
-AL = Algorithmie

Exemple :

[DB-RS] Mouvement perso multijoueur
Avatar de l’utilisateur
Kaloverian
Messages : 345
Inscription : 10 Août 2016 03:03

[RESOLU] problème de rotation

Message par Kaloverian » 03 Mars 2024 06:15

bonjour,

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);
 }

Mais il réside un problème car mon but suivant n'est pas réalisé:
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));
        }
    }
}

ni aucun autre ne me donne le résultat souhaité !
Vous pouvez vérifier.
Peut-on m'aider?

merci de votre aide

Avatar de l’utilisateur
Kaloverian
Messages : 345
Inscription : 10 Août 2016 03:03

Re: problème de rotation

Message par Kaloverian » 03 Mars 2024 19:10

problème résolu avec ce code corrigé:
rot_plan_x=-90; rot_y=90

Code : Tout sélectionner

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

public class local_rotation : MonoBehaviour
{
    public GameObject obj, grid;
    public float rot_init_x, rot_init_y, rot_init_z, rot_plan_x, rot_plan_y, rot_plan__z, rot_x, rot_y, rot_z;
    public KeyCode plan_tourne, obj_tourne;
    float Angle;
   // GameObject plan;

    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(plan_tourne))
        {
            obj.transform.parent = grid.transform;

          
            // le plan tourne
            grid.transform.Rotate(rot_plan_x, rot_plan_y, rot_plan__z, Space.Self);
        }
        if (Input.GetKeyDown(obj_tourne))
        {  
            
            //version locale_rotation
            // Ajouter la rotation actuelle

            obj.transform.localEulerAngles = new Vector3(rot_x, rot_y, rot_z);

            

            // Normaliser les angles
            obj.transform.localEulerAngles = NormalizeEulerAngles(obj.transform.localEulerAngles);

            print("Après rotation: " + NormalizeEulerAngles(obj.GetComponent<Transform>().localEulerAngles));
            
             }
    }
}

Verrouillé

Revenir vers « (C#) CSharp »