- Home /
How do I turn this into a Lerp function
So I'm busy with a project and we're starting to work out thr rough edges, so we figured a slow over time would be better, the problem is I don't really know how to turn this into a Lerp function, any help? Here's the code,
using UnityEngine;
using System.Collections;
public class SlowWall : MonoBehaviour {
public GameObject Player;
private ScoreSystem scoreSystem;
void Start (){
Player = GameObject.Find("Player");
scoreSystem = (ScoreSystem)FindObjectOfType(typeof(ScoreSystem));
}
public float ShrunkenSlowDownPercentage = 0.6f;
public float SlowDownPercentage = 0.8f;
public float EnlargedSlowDownPercentage = 0.9f;
void OnTriggerEnter(Collider other)
{
PlayerComponent playerComponent = other.gameObject.GetComponent<PlayerComponent> ();
//checking if collided with player
if (playerComponent && !scoreSystem.GetOverload()) {
if (playerComponent.transform.localScale.magnitude < 0.9f) {
playerComponent.Speed *= ShrunkenSlowDownPercentage;
}
if (playerComponent.transform.localScale.magnitude > 1.9f) {
playerComponent.Speed *= EnlargedSlowDownPercentage;
}
else{
playerComponent.Speed *= SlowDownPercentage;
}
}
}
I think it'd be easier to work with the else function for the example since that's much less complex.
A Lerp is an attended function. By this I mean you must attend to it and update the values per frame to get what you want out of it. This places it usually in the Update function.
You basically select a From and To field and use the 0->1 slider (t value) to move between them. Each call, the value returned is basically a linearly scaled value between From and To based on 100t % (t=0 is 0% and is the From value, t=1 is 100% so is the To value)
And how would this look in code? I understand what you mean but I have difficulty visualising it in code
Answer by Immanuel-Scholz · Nov 19, 2014 at 03:00 PM
I think it is easier if you put the lerping into the part of the code that reads out the property "PlayerComponent.Speed".
Or let me rephrase that:
Add another variable "SpeedTarget" into PlayerComponent. Instead of setting "Speed", you set "SpeedTarget". Then put an Update() - function into PlayerComponent that lerps the Speed towards SpeedTarget every frame. Done.
class PlayerComponent : MonoBehaviour {
...
[System.NonSerialized] public float SpeedTarget; // change this instead of Speed
[SerializeField] private float Speed; // don't change it from other places
public float SpeedSmoothiness = 5; // play around a bit to find nice values
void Awake() {
SpeedTarget = Speed;
}
void Update() {
Speed = Mathf.Lerp(Speed, SpeedTarget, SpeedSmoothiness * Time.deltaTime);
}
}
class SlowWall : MonoBehaviour {
...
playerComponent.SpeedTarget *= ShrunkenSlowDownPercentage;
...
I'm not the one that made PlayerComponent so I'm not going to mess with the code there, Isn't there a different way to get this done other than rewriting the PlayerComponent Script?
I do appreciate you helping me, but I'm scared to mess up that script cause it controls most of the game
Seeing this gave me an idea though, can't I put SpeedTarget in my slowwall script as: SpeedTarget = playerComponent.Speed *= SlowDownPercentage;
Not sure if it works like that though, you'd have to explain how that works cause I'm fairly new to coding
Ah there's a problem, the speed already has a certain formula for it since this is an endless runner
public float Speed { get { return speed; } set { speed = $$anonymous$$athf.Clamp(value, speed$$anonymous$$in, speed$$anonymous$$ax); } } How can i convert that into the lerp?
Your answer
Follow this Question
Related Questions
Can't find fix for buggy camera 0 Answers
Controlling when a script is enabled with another Script 1 Answer
Accessing List from Parent G.O. Script 0 Answers
How can I insert Load Level in this Script? 1 Answer
Raycast line in c# 1 Answer