- Home /
Changes of Transform cancel on next frame after change
Hello everyone.
I started making game today, where transform.position and transform.rotation of different objects have to get back to their initial values in one point. It works correctly, but in the next frame after changing the transform it gets back to the value that was before the change.
I've tried a lot of options and disabled different functions to check if they are affecting that, but nothing helped.
Code:
GameController - he starts transform restart:
public class GameController : MonoBehaviour {
[SerializeField] private GameObject[] levelObjects;
[SerializeField] private PlayerController player;
public void ResetGame() {
player.ResetTransform();
foreach(GameObject obj in levelObjects) {
if (obj.GetComponent<LevelObject_Parent>() != null) {
obj.GetComponent<LevelObject_Parent>().ResetTransform();
}
}
}
}
Player Controller
private CharacterController characterController;
private Vector3 initialPosition;
private Quaternion initialRotation;
private void Awake() {
characterController = GetComponent<CharacterController>();
initialPosition = gameObject.transform.position;
initialRotation = gameObject.transform.rotation;
}
public void ResetTransform() {
gameObject.transform.position = initialPosition;
gameObject.transform.rotation = initialRotation;
}
ResetTransform() on LevelObjects works correctly
Answer by dannFlyJeRay · Sep 05, 2020 at 05:39 PM
Found the problem.. and solution.
The problem is: CharacterController. It is used to move in FixedUpdate(), and changes the position of player to the one that was before.
Solution:
private bool isMoveable = true;
private void Awake() {
characterController = GetComponent<CharacterController>();
initialPosition = gameObject.transform.position;
initialRotation = gameObject.transform.rotation;
}
public void ResetTransform() {
isMoveable = false;
gameObject.transform.position = initialPosition;
gameObject.transform.rotation = initialRotation;
StartCoroutine(BecomeMoveable());
}
private IEnumerator BecomeMoveable(){
yield return new WaitForSeconds(0.1f);
isMoveable = true;
}
private void FixedUpdate() {
if (isMoveable) Movement();
}
Your answer
Follow this Question
Related Questions
Issues with copying Y axisrotation of another object 2 Answers
Possible way to improve performance moving large hierarchies? 1 Answer
How Do I Give Two Objects the Same Transform? 1 Answer
Coroutine - transform for every frame for duration? 0 Answers
any way to access the global position and rotation of a child object? 1 Answer