Hello,
I’m new to Unity scripting and have finished the Roll-a-ball tutorial. Before I move on I’m extending the game to play around with some other functions, and thought it would be nice if the pickUp cubes would have a little animation when the player sphere collides with them. I’ve been trying to do this for the last few days though and can’t seem to get it right!
So firstly I created an animation whereby the cube’s scale goes momentarily from 0.5 to 0.75, then over a second shrinks to 0.1 (all in x, y and z). My intention was to play this in the onCollision method, just before the cube is deactivated. I’d added the animation component onto the ‘player’ sphere and the animation was associated with the pickUp prefab, but the animation didn’t play even though the cube was deactivated.
I then tried to do the same but through two Lerp’s, one to increase the scale and another to reduce it (full code below) with some delays in between. I’ve written this as a separate function and called it once the condition has been met on collision. The error I get in the console is:
So from this, I’m guessing I’m not passing the correct parameters into the function (when I have got the game to run, it doesn’t seem to run the changeScale function at all, any debug logs I put there do not show). So rather having exhausted trial and error I thought I’d ask where I’m going wrong? Also, is doing this via a Lerp the best way or should I stick with the animation?
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
private int count;
private float timeCount;
private bool timer;
public float smooth1;
public float smooth2;
public GUIText countText;
public GUIText winText;
public GUIText timerText;
public GUIText finalTime;
void Start()
{
timer = true;
count = 0;
timeCount = 0;
SetCountText ();
winText.text = "";
finalTime.text = "";
timerText.text = "Time: "+ 0;
smooth1 = 0.1f;
smooth2 = 1f;
}
void Update()
{
if (timer == true)
{
timeCount += Time.deltaTime;
timerText.text = "Time: " + timeCount.ToString ("F2");
}
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.AddForce(movement * speed * Time.deltaTime);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "PickUp")
{
changeScale ();
other.gameObject.SetActive(false);
count = count +1;
SetCountText ();
endGame ();
}
}
void SetCountText ()
{
countText.text = "Count: " + count.ToString ();
}
void endGame ()
{
if (count >= 16)
{
timer = false;
countText.gameObject.SetActive(false);
timerText.gameObject.SetActive(false);
winText.text = "You Win!!!";
finalTime.text = "Final Time: "+timeCount.ToString ("F2");
}
}
IEnumerator changeScale(Collider other)
{
Debug.Log ("Function has been called");
Vector3 original = other.gameObject.transform.localScale;
Vector3 bigger = new Vector3 (0.75f, 0.75f, 0.75f);
Vector3 smaller = new Vector3 (0.1f, 0.1f, 0.1f);
other.gameObject.transform.localScale = Vector3.Lerp(bigger,original,smooth1*Time.deltaTime);
yield return new WaitForSeconds(0.2f);
other.gameObject.transform.localScale = Vector3.Lerp(smaller,bigger,smooth2*Time.deltaTime);
yield return new WaitForSeconds(1f);
}
}
Any help and general coding advise on the way I’m going about this would be appreciated!