Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Bug set rotation does not work in current frame, I have to force the rotation in the next frame update.

Discussion in 'Scripting' started by ultimagic, Jul 28, 2020.

  1. ultimagic

    ultimagic

    Joined:
    Feb 21, 2013
    Posts:
    2
    Hello, I recently upgraded the Engine from 5.6.5 -> 2019.4.4f1 (64bit)
    And I found a series bug. In my game, there are many skills that will automatically face the enemy.
    But it does not work now. Therefore I created a simple project to test this issue. Please download the attached project and open it by Unity 2019.4.4f1.

    1. Load Scene/SampleScene.
    2. Press "Play" button.
    3. Press "X" key to see the bug.

    Fortunately, if you uncomment the line #61 to set the rotation in the "NEXT" frame update. It works!!!
    Is there anything wrong with the Rotation update? Thanks for your time.
     

    Attached Files:

  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,012
    Post the code using tags. You may just be using some obsolete methods.
     
  3. ultimagic

    ultimagic

    Joined:
    Feb 21, 2013
    Posts:
    2
    Thank you very much for your suggestion.
    BTW, I forgot to remove an unused script named CharacterScript.cs. Please ignore this script.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MyPlayerController : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.        
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         if(Input.GetKeyDown(KeyCode.X))
    17.         {
    18.             Animator anim = gameObject.GetComponent<Animator>();
    19.             if(anim != null)
    20.                 anim.SetTrigger("attack");
    21.         }
    22.        
    23.     }
    24.  
    25.     public void setFaceDir(Vector3 dir)
    26.     {
    27.         Quaternion quat = Quaternion.FromToRotation(Vector3.forward, dir);
    28.         gameObject.transform.rotation = quat;
    29.         // Debug Log
    30.         Debug.Log("setFaceDir(): dir = " + dir.ToString());
    31.     }
    32.  
    33.     void AnimEvent_AutoFaceEnemy()
    34.     {
    35.         GameObject found_obj = GameObject.FindWithTag("Enemy");
    36.         if (found_obj != null)
    37.         {
    38.             Vector3 dir = found_obj.transform.position - gameObject.transform.position;
    39.             dir.y = 0.0f;
    40.             dir.Normalize();
    41.             setFaceDir(dir);
    42.             // log character direction in 3 frames
    43.             StartCoroutine(LogCharacterDirection(dir, found_obj));
    44.         }
    45.     }
    46.  
    47.     IEnumerator LogCharacterDirection(Vector3 dir, GameObject found_obj)
    48.     {
    49.         // Log character dir (frame 0)
    50.         {
    51.             Vector3 cur_chardir = gameObject.transform.rotation * Vector3.forward;
    52.             Debug.Log("------------------------------------------------------");
    53.             Debug.Log(Time.time.ToString() + ">>> found " + found_obj.name.ToString() +
    54.                 ", cur_facedir = " + dir.ToString() +
    55.                 ", cur_chardir = " + cur_chardir.ToString());
    56.         }
    57.  
    58.         yield return null;
    59.  
    60.         // Uncomment this line to get the correct result.
    61.         //setFaceDir(dir);
    62.  
    63.         // Log character dir (frame 1)
    64.         {
    65.             Vector3 cur_chardir = gameObject.transform.rotation * Vector3.forward;
    66.             Debug.Log(Time.time.ToString() + ">>> found " + found_obj.name.ToString() +
    67.                 ", cur_facedir = " + dir.ToString() +
    68.                 ", cur_chardir = " + cur_chardir.ToString());
    69.         }
    70.  
    71.         yield return null;
    72.  
    73.         // Log character dir (frame 2)
    74.         {
    75.             Vector3 cur_chardir = gameObject.transform.rotation * Vector3.forward;
    76.             Debug.Log(Time.time.ToString() + ">>> found " + found_obj.name.ToString() +
    77.                 ", cur_facedir = " + dir.ToString() +
    78.                 ", cur_chardir = " + cur_chardir.ToString());
    79.         }
    80.     }
    81.  
    82. }
    83.  
     
unityunity