- Home /
Using Lerp to increase SphereCollider radius over time
I am trying to increase the SphereCollider radius over time using the Lerp function. With this code, I am able to start from a high radius number and go down to the starting radius, as the health for the object is set to 100. How can I do this the other way round? InverseLerp does not seem to work, or working incorrectly
void Start()
{
areaOfDetection = gameObject.GetComponent<SphereCollider>();
startDetectionRadius = areaOfDetection.radius;
}
// Update is called once per frame
void Update()
{
// Area of Detection
finalAreaOfDetection = Mathf.Lerp(startDetectionRadius, GameController.Instance.GetRBSHealth(), GameController.Instance.lifeDeductionRate);
areaOfDetection.radius = finalAreaOfDetection;
}
Answer by Arano · Oct 10, 2019 at 12:48 PM
in your update loop you could call something like this when you want to shrink your 'radius'
areaOfDetection.radius = areaOfDetection.radius + shrinkspeed* time.DeltaTime;
p.s. seeing this question makes me think you are a beginner? let me(or us) know if u need some more pointers
Answer by Captain_Pineapple · Oct 10, 2019 at 12:48 PM
Hey there,
if you want to change the direction you are lerping simply change your start and target values. though normally when using lerp it looks something like A = Lerp(A, B, stepPerUpdate*Time.deltaTime);
where A would be your current radius and B the radius where you want to go. So whenever you set B to another value you will automatically move A in that direction.
As a sidenote to this: keep in mind that even if you use Time.deltaTime here the result will be framerate dependant. If absolute deterministic behaviour is needed think of mooving this into FixedUpdate.
EDIT: Just realized i was kinda stupid since my solution will not lead to linear shrinking speed but asymptotic behaviour. So ignor this if it does not suit your case. Either way just switching the Target and Start values in the Lerp function should do the trick.
Answer by firepro20 · Oct 10, 2019 at 01:07 PM
@Arano Yes that would be the simplest way to implement it, however in my case I want to create a relationship with the gameObject's health. Therefore the less health I have, the more the radius for the sphere will grow. To put it in context, the sphere visually represents hunger, so the less health I have the hungrier the gameobject will become, which will increase the areaofdetection to become aware of food within area.
To answer your question, yes I am relatively new, only created two small games so far.
@Captain_Pineapple I want to achieve the opposite of this, I want the sphere radius to start from a small number, and grow up to a maximum of 10. I have managed to partially achieve this behaviour. The reason why I am not using Time.deltaTime is that I want to create a relationship with the gameObjects health for the game designer.
To add to this, this is the new code
finalAreaOfDetection = $$anonymous$$athf.InverseLerp(startDetectionRadius, GameController.Instance.GetRBSHealth(), GameController.Instance.lifeDeductionRate);
areaOfDetection.radius = finalAreaOfDetection * 50f;
// Limit AreaOfDetection
if (areaOfDetection.radius >= 3f)
{
areaOfDetection.radius = 3f;
}
Changed to 3 as 10 was too large
Either you did not understand what i wrote or i don't get your problem...
assume every Update you set your radius to a value A that is a variable.
Further assume you have a target radius called B. This for example can be set to "somehealthvalue" or a proportional value of this.
Then you can simply add the line i added in my OP and will have a radius value that will always lerp to the value provided as B. "lerp to" as in "move smoothly asymptoically to a new given value".
no need for any inverse lerp or anything.
Your answer
![](https://koobas.hobune.stream/wayback/20220612213236im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
rigidbody.velocity vs lerp efficiency 0 Answers
How to use Lerp? 1 Answer
How to rotate 90 degrees from 270 degrees to 0 with lerp? c# 1 Answer
Color' does not contain a definition for 'red' 1 Answer
c# wait script 2 Answers