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 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
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
1
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
Add comment · 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 $$anonymous$$axShadowSize 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 O$$anonymous$$ as I have debugged this. 1. may be affecting 2. as $$anonymous$$axShadowSize 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.

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

How to scale a object with distance from the center of the canvas? 1 Answer

(iOS) Plane shadow that follows object on flat terrain - Translate but not Rotate. 1 Answer

Fixed distance between objects? 2 Answers

Dynamic shadow in huge scenes. 0 Answers

Shadow quality and small object scale? 3 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