'Unity changing gameobject's transform position once game starts
I have a character selection screen, when a certain button is pressed the assigned character will slide into view. Unfortunately for one of my character's they transform position seems to change putting them higher, etc making them not appear on the game screen. I'm not sure why as both character's have the same components and no where in code tells them to change position. The only script they have works as slide animation to make seem as though they glide into view instead of just appear. (attached below)
If I change the character's avatar int he animator to the same as the other character's then their position will be correct but then they will not do any animations.
- Winter is the working GameObject.
- Eunha is the one that changes position. Before the game starts Eunha's transform position is the same as Winter's shown in the Inspector.
private Vector3 startPosition;
private void Awake()
{
finalPosition = transform.position;
startPosition = finalPosition - transform.right * 5.0f;
}
private void Update()
{
transform.position = Vector3.Lerp(transform.position, finalPosition, 0.1f);
}
private void OnEnable()
{
transform.position = startPosition;
} ```
Solution 1:[1]
The codeing order seems to be problem. I recommend using 2 empty objects to determine location. This script is more efficient and reliable.
public Transform startPosition; // place on start position
public Transform endPosition; // place on shifted position
public float duration = 1f;
public void OnEnable() => StartCoroutine(Shift());
public IEnumerator Shift()
{
var shiftProgress = 0f;
while (shiftProgress < 1)
{
shiftProgress += Time.deltaTime/duration;
transform.position = Vector3.Lerp(startPosition.position, endPosition.position, shiftProgress);
yield return new WaitForEndOfFrame();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |



