Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by pAWNproductions · Apr 30, 2021 at 10:20 PM · clamplevel-designsine-wave

How to create a sine function with cutoff peaks and troughs

I want to create a saw hazard for my current game and I want the arm to move back in forth similar to capturing the output of a sine function like this movementDistance * Mathf.Sin(counter * movementSpeed); except I want the "edges" of the sine function to be capped off (compressed) like this alt text

(original source: https://www.youtube.com/watch?v=yj53Q-pisbw&t=87s&ab_channel=CSGuitars between 0:48 and 0:57)

I'm aware I could simply use Mathf.Clamp(calculatedValue, -extent, extent) , but this would create drastic stopping and starting at the edges and I'd like to round these edges if possible to make a more realistic slowdown effect for the saw arm. How could I achieve such an effect?

clippedsine.png (155.7 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Hellium · Apr 30, 2021 at 11:29 PM

Simply use an animation curve and use Evaluate instead of the Sin method.

https://docs.unity3d.com/ScriptReference/AnimationCurve.Evaluate.html

 public AnimationCurve Curve; // Define the curve in the inspector
 
 //...
 float value = movementDistance * Curve.Evaluate(counter * movementSpeed);


If you want the curve to be prepopulated with the Sin curve:

 private void OnValidate()
 {
     if(curve.keys.Length == 1)
     {
         curve.RemoveKey(0);
         curve.postWrapMode = WrapMode.Loop;
         curve.AddKey(new Keyframe(0, 0, 1, 1)); // 0, sin(0), sin'(0) = cos(0), sin'(0) = cos(0)
         curve.AddKey(new Keyframe(0.5f * Mathf.PI, 1, 0, 0));
         curve.AddKey(new Keyframe(Mathf.PI, 0, -1, -1));
         curve.AddKey(new Keyframe(1.5f * Mathf.PI, -1, 0, 0));
         curve.AddKey(new Keyframe(2f * Mathf.PI, 0, 1, 1));
     }
 }


Then, add the keys you want in the inspector

Comment
Add comment · Show 8 · 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 pAWNproductions · Jun 03, 2021 at 08:35 PM 0
Share

So I added your OnValidate code, and tried this in the Update function as well:

  timer += Time.deltaTime;
         if (timer > maxTime)
         {
             timer = 0;
         }
 
         float lValue = movementDistance * lerpCurve.Evaluate(timer * movementSpeed);
 
         transform.position += -transform.up * lValue;

but now the gameobject just slowly drifts into one direction. Did I make a mistake with my code?

avatar image Hellium pAWNproductions · Jun 03, 2021 at 08:46 PM 0
Share
      transform.position = -transform.up * lValue;
avatar image pAWNproductions Hellium · Jun 03, 2021 at 08:53 PM 0
Share

I tried this but the gameobject just spawned at the origin and didn't move anymore

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

117 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get RenderTexture non-square? 2 Answers

door script problems 2 Answers

Clamp Mouse Zoom using Scroll Wheel (SOLVED) 2 Answers

Is it possible in mechanim to have a state be clamped? 0 Answers

smoother rotation 0 Answers


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