• 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 /
  • Help Room /
avatar image
Question by yaaks83 · Aug 23, 2016 at 11:58 AM · gameobjectmaterialtransparencyfade

Issues with Mathf.MoveTowards

Hi, I'm having issues with Mathf.MoveTowards. I want to interpolate between 1 and 0 over time with a public float "fadeValue". But the console yields jumpy results from 1, 0, randomly increasing and decreasing till it reaches 0 eventually.

using UnityEngine; using System.Collections;

 public class CloudBehaviour : MonoBehaviour {
     public float cloudSpeed = 1;
     public float cloudScaleMin = 1;
     public float cloudScaleMax = 1;
     [Range(0f, 355f)]
     public float rotationThreshould;
     public float fadeValue = 10;
     private MeshRenderer meshrenderer;
     private float cloudScale;
     private float fade;
     
 
     // Use this for initialization
     void Start () {
         meshrenderer = GetComponent<MeshRenderer>();
         cloudScale = Random.Range(cloudScaleMin, cloudScaleMax);
         transform.localScale += new Vector3(cloudScale, cloudScale, cloudScale);
         transform.Rotate(0f, Random.Range(0f, rotationThreshould), 0f);
 
     }
     
     // Update is called once per frame
     void Update () {
         transform.position += new Vector3(cloudSpeed * Time.deltaTime, 0f, 0f);
         fade = Mathf.MoveTowards(1f, 0f, fadeValue * Time.deltaTime);
         meshrenderer.material.SetFloat("_AlphaLevel", fade);
         Debug.Log(fade);
     }
       
 }



Comment

People who like this

0 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 yaaks83 · Aug 23, 2016 at 12:57 PM 0
Share

Lowering the fade Value worked. But, I find that really strange.

avatar image ScaniX yaaks83 · Aug 23, 2016 at 01:04 PM 0
Share

The last parameter is the allowed maxDelta towards the target value. Probably you should just use the simpler approach without using that method.

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by ScaniX · Aug 23, 2016 at 12:19 PM

MoveTowards should be called on the current value, you keep calling it on the value 1. Try the following instead. Your script is supposed to immediately fade it out to 0 and stay there?

  public class CloudBehaviour : MonoBehaviour {
      public float cloudSpeed = 1;
      public float cloudScaleMin = 1;
      public float cloudScaleMax = 1;
      [Range(0f, 355f)]
      public float rotationThreshould;
      public float fadeValue = 10;
      private MeshRenderer meshrenderer;
      private float cloudScale;
      private float fade = 1f;
      
  
      // Use this for initialization
      void Start () {
          meshrenderer = GetComponent<MeshRenderer>();
          cloudScale = Random.Range(cloudScaleMin, cloudScaleMax);
          transform.localScale += new Vector3(cloudScale, cloudScale, cloudScale);
          transform.Rotate(0f, Random.Range(0f, rotationThreshould), 0f);
  
      }
      
      // Update is called once per frame
      void Update () {
          transform.position += new Vector3(cloudSpeed * Time.deltaTime, 0f, 0f);
          fade = Mathf.MoveTowards(fade, 0f, fadeValue * Time.deltaTime);
          meshrenderer.material.SetFloat("_AlphaLevel", fade);
          Debug.Log(fade);
      }
        
  }

Comment
ben-rasooli
JonnyG7

People who like this

2 Show 7 · 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 yaaks83 · Aug 23, 2016 at 12:23 PM 0
Share

Thanks! I will try what you've mentioned. I want it to fade to zero over time and then destroy gameobject when its zero.

avatar image yaaks83 · Aug 23, 2016 at 12:35 PM 0
Share

Okay! I tried it, but the Object fade goes to zero instantly and not over the fadeValue * Time.deltaTme mentioned.

avatar image ScaniX yaaks83 · Aug 23, 2016 at 12:37 PM 0
Share

Did you do both changes?

 private float fade = 1f;

and

 fade = Mathf.MoveTowards(fade, 0f, fadeValue * Time.deltaTime);

If yes: you probably need to lower the value of fadeValue.

avatar image yaaks83 ScaniX · Aug 23, 2016 at 12:40 PM 0
Share

Yes! I made both changes. I tried with fadeValue 10, and 100. for the fade to happen over 10, and 100 secs. What ma I doing wrong? :( I will try lowering the fadeValue.

Show more comments

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Transparency / Fade Material lighing issues 0 Answers

IOS does not render `fade` rendering mode 1 Answer

How I can make whole gameobject transparent? 0 Answers

How can I create a hit effect by changing the alpha value of material of GameObject? 1 Answer

Cannot link material to gameObject 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