Resetting the scale of an object being scaled by a timer
I'm making a game where the player's field of view shrinks in relation to the amount of time remaining in a timer. I have it set up so that when the player collides with an object the timer resets, however the field of view does not reset to it's original size when the timer is reset. This is the code I was using. using UnityEngine; using System.Collections;
public class NewTimerScript : MonoBehaviour {
public float timeLeft = 10000.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update()
{
timeLeft -= Time.deltaTime;
Debug.Log (timeLeft);
var localx = gameObject.transform.localScale.x;
var localy = gameObject.transform.localScale.y;
gameObject.transform.localScale = new Vector3 (localx*timeLeft/10000, localy*timeLeft/10000, -1f);
if (timeLeft < 9975) {
GameOver ();
}
}
public void Reset ()
{
timeLeft = 10000.0f;
}
void GameOver ()
{
Application.LoadLevel ("YouLose");
}
}
^The Timer Code using UnityEngine; using System.Collections;
public class TimerReset : MonoBehaviour {
public NewTimerScript NTS;
// Use this for initialization
void Start () {
}
void OnTriggerEnter2D()
{
NTS.Reset ();
}
// Update is called once per frame
void Update () {
}
}
^Resetting the timer Any help is much appreciated
Answer by Yuvii · Dec 10, 2015 at 09:08 AM
Try to declare both variables var localx = gameObject.transform.localScale.x; var localy = gameObject.transform.localScale.y;
in Start instead of the Update. I think the problem is that even if you reset the timeLeft, you'll still multipliate the shrinked size by 1, you don't reset the size because the variables 'localx' and 'localy' are always equals to the size they have in the present. Sorry i didn't explained very well, still need more practice in english, but i think this is your problem.
public class NewTimerScript : MonoBehaviour {
public float timeLeft = 10000.0f;
// Use this for initialization
void Start () {
var localx = gameObject.transform.localScale.x;
var localy = gameObject.transform.localScale.y;
}
// Update is called once per frame
void Update()
{
timeLeft -= Time.deltaTime;
Debug.Log (timeLeft);
//var localx = gameObject.transform.localScale.x;
//var localy = gameObject.transform.localScale.y;
gameObject.transform.localScale = new Vector3 (localx*timeLeft/10000, localy*timeLeft/10000, -1f);
if (timeLeft < 9975) {
GameOver ();
}
}
public void Reset ()
{
timeLeft = 10000.0f;
}
void GameOver ()
{
Application.LoadLevel ("YouLose");
}
}