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.
- Load Scene/SampleScene.
- Press “Play” button.
- 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.
6140238–670071–BugTest01.zip (404 KB)
Post the code using tags. You may just be using some obsolete methods.
Thank you very much for your suggestion.
BTW, I forgot to remove an unused script named CharacterScript.cs. Please ignore this script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyPlayerController : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.X))
{
Animator anim = gameObject.GetComponent<Animator>();
if(anim != null)
anim.SetTrigger("attack");
}
}
public void setFaceDir(Vector3 dir)
{
Quaternion quat = Quaternion.FromToRotation(Vector3.forward, dir);
gameObject.transform.rotation = quat;
// Debug Log
Debug.Log("setFaceDir(): dir = " + dir.ToString());
}
void AnimEvent_AutoFaceEnemy()
{
GameObject found_obj = GameObject.FindWithTag("Enemy");
if (found_obj != null)
{
Vector3 dir = found_obj.transform.position - gameObject.transform.position;
dir.y = 0.0f;
dir.Normalize();
setFaceDir(dir);
// log character direction in 3 frames
StartCoroutine(LogCharacterDirection(dir, found_obj));
}
}
IEnumerator LogCharacterDirection(Vector3 dir, GameObject found_obj)
{
// Log character dir (frame 0)
{
Vector3 cur_chardir = gameObject.transform.rotation * Vector3.forward;
Debug.Log("------------------------------------------------------");
Debug.Log(Time.time.ToString() + ">>> found " + found_obj.name.ToString() +
", cur_facedir = " + dir.ToString() +
", cur_chardir = " + cur_chardir.ToString());
}
yield return null;
// Uncomment this line to get the correct result.
//setFaceDir(dir);
// Log character dir (frame 1)
{
Vector3 cur_chardir = gameObject.transform.rotation * Vector3.forward;
Debug.Log(Time.time.ToString() + ">>> found " + found_obj.name.ToString() +
", cur_facedir = " + dir.ToString() +
", cur_chardir = " + cur_chardir.ToString());
}
yield return null;
// Log character dir (frame 2)
{
Vector3 cur_chardir = gameObject.transform.rotation * Vector3.forward;
Debug.Log(Time.time.ToString() + ">>> found " + found_obj.name.ToString() +
", cur_facedir = " + dir.ToString() +
", cur_chardir = " + cur_chardir.ToString());
}
}
}