In Unity, I have this problem where the Game Object this script is attached to jumps to Z position 0 as soon as the game runs. In the Editor, I have the Game Object set as Z position -50. I want startPos to read this position (-50) and use it when newPos is updated.
Why does startPos get updated immediately to 0?
using UnityEngine;
public class MoveOutScript : MonoBehaviour {
public float speed = 10f;
float startPos;
Vector3 newPos;
float changePerSecond2;
public void Start()
{
startPos = transform.position.z;
changePerSecond = speed * Time.deltaTime;
}
public void Update()
{
#region Move sprite away steadily
newPos.z += changePerSecond;
newPos.z = Mathf.Clamp(newPos.z, startPos, 15f);
transform.localPosition = newPos;
#endregion
}
}