- Home /
Need help with value over time!
Hey friendly people.
I have an object going forward by some float speed.
I'm trying to bring it down on trigger over time. My code looks something like this:
public class ObjectStoppre: MonoBehaviour
{
public myObject object; // not my real objects name
float myTime; //just an example name
void Update()
{
myTime = Time.deltaTime * 10;
}
public void OnTriggerEnter(Collider col)
{
if (col.tag == "frontOfObject")
{
Debug.Log("Stop me over time Senpai!");
object.speedObject = Mathf.Lerp(object.speedObject, 0,myTime);
}
}
}
So what i'm doing is grabing the speed value from my "Move" script and trying to lerp it over time to 0. I'm having no luck.. can someone help me out please?
Thanks
It might help to know what that function is doing.
Your "myTime" value can be just about anything as you're not clamping it to [0,1], but that range is a requirement to get reasonable behavior from Lerp (Linear-Interpolation).
Additionally, you're calling this during an OnTriggerEnter event, which is usually not called multiple times in a row.
$$anonymous$$aybe you wanted to use OnTriggerStay ins$$anonymous$$d? $$anonymous$$aybe you want to start a coroutine to do that over time whenever something enters?
Yea, i was using OnTriggerEnter wrong, i'll try with OnTriggerStay.. i'm trying to avoid using Update() because i have no idea how much of an impact it has on my performance.. and i need to optimize this "game" as much as i can (200fps $$anonymous$$imum).
Thanks for the reply, cheers
Answer by ThePersister · Nov 07, 2016 at 04:25 PM
Hi there friendly @Gotal.
Based on your code. Perhaps this helps? If it does, please accept my answer! :)
Otherwise, elaborate and I could try again.
using UnityEngine;
using System.Collections;
public class ObjectStopper : MonoBehaviour
{
public myObject targetObject; // not my real objects name
public float stopSpeed = 10f;
private bool isStopping;
void Update()
{
if ( isStopping )
{
// Mathf.Max => Prevents negative speed.
targetObject.speedObject = Mathf.Max(0f, targetObject.speedObject - stopSpeed * Time.deltaTime);
}
}
void OnTriggerEnter( Collider col )
{
if( col.tag == "frontOfObject" )
{
Debug.Log( "Stop me over time Senpai!" );
isStopping = true;
}
}
}
You could add this to stop Stopping upon escape.
void OnTriggerExit( Collider col )
{
if( col.tag == "frontOfObject" )
{
Debug.Log( "The great escape!" );
isStopping = false;
}
}
Hey man, thanks for the reply.. yea.. this works, but i kind of wanted to avoid using Update(), guess it cant be helped.. i'll try doing the same thing it an OnTriggerStay or running the function while speed != 0
Cheers
Should've said so, of course you can! :) We can achieve this using Coroutines / IEnumerators.
using UnityEngine;
using System.Collections;
public class ObjectStopper : $$anonymous$$onoBehaviour
{
public myObject targetObject; // not my real objects name
public float stopSpeed = 10f;
private bool isStopping;
void OnTriggerEnter(Collider col)
{
if (col.tag == "frontOfObject")
{
Debug.Log("Stop me over time Senpai!");
StartCoroutine( StopOverTime() );
}
}
// Optional
void OnTriggerExit(Collider col)
{
if (col.tag == "frontOfObject")
{
Debug.Log("The great escape!");
isStopping = false;
}
}
//
// Alternative for using Update()
private IEnumerator StopOverTime()
{
if (!isStopping)
{
isStopping = true;
// Update-like call, stops when speed becomes 0 or when isStopping is set to false.
while (isStopping && targetObject.speedObject >= 0)
{
// $$anonymous$$athf.$$anonymous$$ax => Prevents negative speed.
targetObject.speedObject = $$anonymous$$athf.$$anonymous$$ax(0f, targetObject.speedObject - stopSpeed * Time.deltaTime);
yield return new WaitForSeconds(0.01f);
}
}
}
I hope that leaves you a bit more satisfied! ;)
Ah man.. this will work wonders.. i need to read up a bit on IEnumerators and Coroutines.. the problem i had was, it stopped correctly in my editor, but my build gave me a bigger frame-rate, so my object stopped almost instantly.. thanks again man, cheers!