Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by KitsuneWarriorr · Nov 19, 2014 at 02:53 PM · c#

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · Nov 19, 2014 at 03:01 PM 0
Share

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)

avatar image KitsuneWarriorr · Nov 19, 2014 at 03:12 PM 0
Share

And how would this look in code? I understand what you mean but I have difficulty visualising it in code

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
     ...


Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KitsuneWarriorr · Nov 19, 2014 at 03:11 PM 0
Share

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

avatar image meat5000 ♦ · Nov 19, 2014 at 03:18 PM 0
Share

$$anonymous$$ake a backup! :)

avatar image KitsuneWarriorr · Nov 19, 2014 at 03:18 PM 0
Share

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

avatar image KitsuneWarriorr · Nov 19, 2014 at 03:19 PM 0
Share

Good idea

avatar image KitsuneWarriorr · Nov 19, 2014 at 03:21 PM 0
Share

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?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges