Hi,
So I’ve written a simple code (based on a Unity tutorial) that uses raycasting, and when you hit an object with a ray (with the tag “Pick Up”) it grows. When you look away, it shrinks. However, I want to make the growing/shrinking a smooth transition, not instant. Should I use lerp? The object I’m scaling has an original size of (.5, .5, .5). (I’m a beginner, sorry if this is straightforward)
Thanks for the help!
Here is the code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Raycast : MonoBehaviour {
public Text NameText;
void Start ()
{
}
void Update () {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
Debug.DrawRay (ray.origin, ray.direction * 10, Color.green);
RaycastHit hit;
if (Physics.Raycast (ray, out hit) == true)
{
Debug.DrawRay (ray.origin, ray.direction * hit.distance, Color.red);
NameText.text = ("Ray hit: " + hit.transform.gameObject.name);
if (hit.collider.gameObject.CompareTag("Pick Up"))
{
Debug.DrawRay (ray.origin, ray.direction * hit.distance, Color.cyan);
//here is where I wan't to add the growing/shrinking
hit.transform.localScale = new Vector3(1F, 1F, 1F);
}
}
}
}
You can achieve this effect through Lerp, but it’s not a necessity; there are plenty of ways to do this. This is how I would do it:
// Put this part at the top where you have NameText
float scale = 0.5f;
float minScale = 0.5f;
float maxScale = 1.0f;
float scaleSpeed = 0.5f;
// Put this part inside Update where you want it to grow
// Grow
scale += scaleSpeed * Time.deltaTime;
// Limit the growth
if (scale > maxScale) {
scale = maxScale;
}
// Apply the new scale
hit.transform.localScale = new Vector3(scale, scale, scale);
To do the shrinking, you’d need to subtract from the scale, make sure it doesn’t go below minScale, and re-apply it to the object.
Warning, incoming javascript:
You can use the animator for something like this. This way you can get some interesting squash and stretch with your animation, easily.
Set up your animation (research how to or ask me if you want to know a very lengthy step by step on how to do it), and then in the script just do this:
private var anim : Animator;
function Start(){
//if the script is on the scale object
anim = GetComponent(Animator);
//if the script is not on the scale object
anim = GameObject.Find("theScaleObject").GetComponent(Animator);
}
function Update(){
if(rayCastHit)
Grow(true);
}
function Grow(grow : boolean){
if(grow)
anim.SetBool("Grow", true);
yield;
anim.SetBool("Grow", false);
}
alternatively, I think you can bypass the expensive raycasting by making a script on the object you want to scale, and just doing something like:
private var anim : Animator;
function OnMouseDown(){
anim.SetBool("Grow", true);
yield;
anim.SetBool("Grow", false);
}
disclaimer: I write in java. Replace function with void, and deal with C#'s annoying Coroutines lol, I’m sure you’re used to them. I think you have to do something like ‘yield return new;’ instead of just yield.
But yeah, that’s how I’d do scaling of an object. Unity’s animator offers a lot of flexibility and you don’t have to worry about fps. And it won’t look static and boring as it would with simple coding, lerped or not.