- Home /
Lerp.localScale is not working OnTriggerEnter,Unity5.3.3,Gear VR App Dev
I am buliding a Gear VR APP,I want my character to scale down on entering the box collider zone,I can scale it down all of sudden by using transform.localScale = new Vector3(0.3F,0.3F,0.3F); But i want it to be done smoothly.Dont knw the reason why its not picking up this lerp line??Can any one helpp???I tagged my box collider(cube to "Mani")
//---------------
pragma strict
var newScale : Vector3 = Vector3 (0.1,0.1,0.1);
var Grow : Vector3 = Vector3 (1,1,1);
var speed : float =2.0;
//---------
function Start () {
transform.localScale = new Vector3(1F,1F,1F);
}
//----
function Update () {
}
//--------
function OnTriggerEnter (info : Collider)
{
if(info.tag == "Mani")
{
transform.localScale =Vector3.Lerp(transform.localScale, newScale, speed * Time.deltaTime);
//transform.localScale = new Vector3(0.3F,0.3F,0.3F);
Debug.Log("Player hit new cube");
}
}
//----------
function OnTriggerExit (Col : Collider)
{
if(Col.tag == "Mani")
{
//transform.localScale = new Vector3(transform.localScale.x, 1F, transform.localScale.y);
transform.localScale =Vector3.Lerp(newScale, Grow, speed * Time.deltaTime);
//transform.localScale = new Vector3(1F,1F,1F);
Debug.Log("Player left cube");
}
}
Answer by RiQQ92 · Apr 21, 2016 at 01:13 PM
The reason why its not working is because OnTriggerEnter is only called when you enter/collide with the trigger, use OnTriggerStay instead if you want it to be done in every frame. Or use flags and do it in update, its up to you.
On triggerStay Updates it and in the trigger zone it keeps on changing my character size with every frame.This Lerp function was working perfectly fine with FPS character frm standard assests but now am using OVR character frm oculus utilities.It was lerping perfectly before
So it lerped it smoothly overtime, but only for the FPS character? If so, maybe your OVR character is missing Rigidbody component which might cause it to not sense the triggers correctly.
Thankyou soo much RiQQ92, there was a stupid error,A line of code was placed inthe start function which wasnt letting the lerp function to do its job. function Start () { //transform.localScale = new Vector3(1F,1F,1F);// this was the stupid line. //--------------------------
Now $$anonymous$$y ontriggerEnter and Exit r working but when i active the On trigger exit line of code,my OVR charcters starts to shake inside trigger zone //------ function OnTriggerStay (info : Collider) { if(info.tag == "$$anonymous$$ani1") { transform.localScale =Vector3.Lerp(transform.localScale, newScale, speed * Time.deltaTime);
//transform.localScale = new Vector3(0.9F,0.9F,0.9F);
Debug.Log("Player $$anonymous$$ani1");
}
} //-----------
function OnTriggerExit (info : Collider) { if(info.tag == "$$anonymous$$ani1") { transform.localScale =Vector3.Lerp(transform.localScale, Grow, speed * Time.deltaTime);
}
}
@Nose$$anonymous$$ills I commented that start fun line and my ontrigge stay started workign fine.every one is saying that u need to call lerp repeadtfly but no one is showing me the proper way,i knw i will have to do something with the update function but how to pass it to triggers??$$anonymous$$y main concern is that they perform certain funcationality on triggeer
From and To in your Lerp... Imagine the two points in space and imagine a straight line between them. t=0 is the start and t=1 is the end. Calling Lerp once returns to you a single point along that line based on what you've put in to t. You can think of t as a percentage (0.50 is 50%). So you are saying, I want a point along that line at x percentage of the distance between the two points.
This is why people usually end up putting some Time factor in t.
Some people like to flip it around and vary the 'To' factor, keeping 'From' as the current location. In this situation t is kept constant, which represents the proportional distance along that line, but this time imagine the end of the line is moving. In this situation t is like the size of the step in that direction you are taking.
There are thousands of coding examples or normal Lerp and Easing Lerp.