• 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
Question by asimov · Jul 04, 2012 at 02:54 PM · scalelerpdistanceshadowplane

How to scale a blob shadow plane smoothly as distance from ground changes

Hi,

I'd like to be able to have the blob shadow plane attached to my Player change size as his distance from the ground changes, and after a point disappear.

I know how to calculate the distance from ground but am not quite sure how to scale a plane smoothly, possibly link the distance with a s/lerp somehow???

Thanks

Comment

People who like this

0 Show 0
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
Best Answer

Answer by DerWoDaSo · Jul 04, 2012 at 03:22 PM

You can try the following code snippet... its just for the size, not the transparency.

However I would recommend to make this variable too. To change the transparency, do something similar, but apply the resulting value to a new Color's alpha property and apply that Color to the material's mainColor property.


C#

 public float maxShadowHeight;      // Height at which the shadow size is zero
 public float maxShadowSize;        // Size of shadow at ground level (height = 0)
 public Transform shadowTransform;  // Transform of the blob shadow
 
 void LateUpdate()
 {
    float distance = CalculateDistanceFromGround();
    ScaleShadow(distance);
 }
 
 void ScaleShadow(float distanceFromGround)
 {
    // Make sure the distance parameter is in a usefull range.
    // Using 0.0001f instead of 0 lets us safely divide by this value
    // without running into any DivisionByZero exceptions.
    distanceFromGround = Mathf.Clamp(distanceFromGround, 0.0001f, maxShadowHeight);
 
    // Normalize distance (bring into range from 0-1)
    float normalizedHeight = distanceFromGround / maxShadowHeight;
 
    // Calculate scale factor so that a distance of 0 results in maxShadowSize and
    // a distance of 1 results in 0
    float scaleFactor = maxShadowSize / normalizedHeight;
    shadowTransform.localScale = Vector3.one * scaleFactor;
 }


Hope that helps... :)

Comment
asimov

People who like this

1 Show 5 · 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 asimov · Jul 04, 2012 at 05:52 PM 0
Share

Thanks for that Jan :)

I've just given it a try and there are couple of things that don't seem to be working right.

  1. For the MaxShadowSize I have to enter an extremely small number to get the shadow to fit the size of the Player (in the region of 0.00005).

  2. As soon as the Player leaves the ground the shadow disappears. I know the distance value is changing OK as I have debugged this. 1. may be affecting 2. as MaxShadowSize is part of the calculations in ScaleShadow().

Any ideas?

Thanks

avatar image DerWoDaSo · Jul 05, 2012 at 11:50 AM 0
Share

Sorry, my fault. Replace the following line...

float normalizedHeight = maxShadowHeight / (maxShadowHeight - distanceFromGround);

avatar image asimov · Jul 05, 2012 at 01:03 PM 0
Share

That's awesome, scaling working great now!

I have tried to do the same for the transparency so the shadow gets lighter further from the ground, etc.

I wonder if you can tell me if this looks like it should?

     float normalizedColor = maxShadowColor / (maxShadowColor - distanceFromGround);

     // This produces 1.006007 on ground
     Debug.Log("normalizedColor = " + normalizedColor);

     // If 255 entered as maxShadowColor, this produces 254.1043 on ground
     Debug.Log("colorScaleFactor = " + colorScaleFactor);

     float newAlpha = originalColour.a * heightScaleFactor;

     // This produces 254.1043 on ground
     Debug.Log("newAlpha = " + newAlpha);

     ShadowTransform.renderer.material.color = new Color(originalColour.r, originalColour.g, originalColour.b, newAlpha);
avatar image DerWoDaSo · Jul 05, 2012 at 01:54 PM 0
Share

The range for Color values is from 0 to 1, not from 0 to 255! ;)

You also dont need the normalizedColor... just reuse the normalizedHeight. When your character is closer over the ground its 1 (fully opaque) and if the distance reaches maxShadowHeight its going up fast. Simply inverting it, will give you the value you will need...

So, this should be fine: float newAlpha = 1 / maxShadowHeight;

avatar image asimov · Jul 07, 2012 at 12:55 AM 0
Share

Brilliant, thanks! :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Shadow distance not working on android 0 Answers

Get shadow areas from a plane 0 Answers

Scale Object Using Lerp? 2 Answers

largest reasonable camera view distance? 1 Answer

Rectangle shadow appearing over world (Shouldn't be there) 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