- Home /
How to reset the scaling after 1second
I don't know what I did wrong.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Paddle : MonoBehaviour {
// configuration paramaters
[SerializeField] float minX = 1f;
[SerializeField] float maxX = 15f;
[SerializeField] float screenWidthInUnits = 16f;
[SerializeField] float shrink;
private bool small ;
private Vector3 originalscale;
public float timer ;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float mousePosInUnits = Input.mousePosition.x / Screen.width * screenWidthInUnits;
Vector2 paddlePos = new Vector2(transform.position.x, transform.position.y);
paddlePos.x = Mathf.Clamp(mousePosInUnits, minX, maxX);
transform.position = paddlePos;
}
public void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag.Equals("enemy"))
{
originalscale = transform.localScale -= new Vector3(0.5f, 0, 0);
small = true;
}
if (small==true)
{
timer = 1f;
}
if (small)
{
timer -= Time.deltaTime;
if (timer <= 0)
{
transform.localScale += new Vector3(0.5f, 0, 0);
}
if (transform.localScale == originalscale)
{
small = false;
}
}
}
}
Could you describe your question more clearly, explain what you want to happen and what's actually happening?
Answer by highpockets · Mar 25, 2019 at 09:37 AM
You should get the originalScale in the Start method:
void Start(){
originalScale = transform.localScale;
}
You should only use the trigger script to scale smaller, set your timer and set your bool and if you just want the transform's scale to change on the x axis, do it like so:
public void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag.Equals("enemy"))
{
transform.localScale = new Vector3(0.5f, transform.localScale.y, transform.localScale.z);
small = true;
timer = 1.0f;
}
}
In the update method, you should include the small Boolean logic, but apply the originalScale vector to localScale when the timer is finished:
void Update(){
if (small)
{
timer -= Time.deltaTime;
if (timer <= 0)
{
transform.localScale = originalScale;
small = false;
}
}
}
Your answer
Follow this Question
Related Questions
how to reset time 2 Answers
How do i reset a timer once it hits 0? 2 Answers
Resetting a timer after game over 1 Answer
Reset a Timer made with .fillAmount 1 Answer
Add seconds to timer countdown. 3 Answers