Hello!
Ideally I would want my game object to continually decrease in size and increase for the time it stays within a collider.
So far the game object will increase in size, but not decrease upon exiting. Instead it will just stop at whatever size it reached when it was inside the collider. It will increase continually in size once it re-enters the collider.
When I have the decreasing part in the void update the game object will only decrease, but not increase upon entering the collider.
Iād be grateful for any hints. Thank you!
My code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AmandaSinaTry : MonoBehaviour
{
public bool enter = true;
public bool stay = true;
public bool exit = true;
//public float moveSpeed;
void Awake()
{
// move and reduce the size of the cube
//transform.position = new Vector3(0, 0.25f, 0.75f);
transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
// add isTrigger
var boxCollider = gameObject.AddComponent<BoxCollider>();
boxCollider.isTrigger = true;
//moveSpeed = 1.0f;
// create a sphere for this cube to interact with
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.gameObject.transform.position = new Vector3(0, 0, 0);
sphere.gameObject.AddComponent<Rigidbody>();
sphere.gameObject.GetComponent<Rigidbody>().isKinematic = true;
sphere.gameObject.GetComponent<Rigidbody>().useGravity = true;
}
void Update()
{
//transform.Translate(Vector3.forward * Time.deltaTime * Input.GetAxis("Vertical") * moveSpeed);
//transform.Translate(Vector3.right * Time.deltaTime * Input.GetAxis("Horizontal") * moveSpeed);
}
private void OnTriggerEnter(Collider other)
{
if (enter)
{
Debug.Log("entered");
}
}
// stayCount allows the OnTriggerStay to be displayed less often
// than it actually occurs.
private float stayCount = 0.0f;
private void OnTriggerStay(Collider other)
{
if (stay)
{
if (stayCount > 0.25f)
{
Debug.Log("staying");
stayCount = stayCount - 0.25f;
Vector3 add = new Vector3(0.1f, 0.1f, 0.1f);
transform.localScale += new Vector3(0.001f, 0.001f, 0.001f);
}
else
{
stayCount = stayCount + Time.deltaTime;
}
}
}
private void OnTriggerExit(Collider other)
{
if (exit)
{
Debug.Log("exit");
if (transform.localScale.x > 0 && transform.localScale.y > 0 && transform.localScale.z > 0)
{
Vector3 add = new Vector3(0f, 0f, 0f);
transform.localScale += new Vector3(-0.0001f, -0.0001f, -0.0001f);
}
if (transform.localScale.x < 0.1 && transform.localScale.y < 0.1 && transform.localScale.z < 0.1)
{
transform.localScale = Vector3.zero;
}
}
}
}