Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Ritches · Jun 28, 2016 at 09:37 PM · floats

Float calculation always outputting 0 when multiplied with another float....

The following is whats not working, basically every time I multiply the local position y of the object by any value other than an integer value declared in the shown script, it results in 0 and hence the object itself is moved to the origin , I've tried using doubles and all sorts of weird solutions , although to no avail , the y position of the object is currently really small at about -0.0195 , and i,m not sure if its just because the value is so small that when put into a calculation it's just read as 0 or not. If you are wondering the vertExtent is the orthographic size of the camera in my scene and is set to 8 , so theoretically the value from this should be 1 * ... but still doesn't seem to work.

 using UnityEngine;
 using System.Collections;
 
 public class ScaleLazerNodes : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
         gameObject.transform.localPosition = new Vector3(0, gameObject.transform.localPosition.y * (Spawner.vertExtent) / 8, 0);
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }
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
0

Answer by btmedia14 · Jun 28, 2016 at 10:35 PM

The y position of -0.0195 is small and effectively, with x and z at zero, the object is close to the origin. Nevertheless, the calculation looks ok.

I assume Debug.Log for each stage of the calculation was used to display individual parts of the calculation. If so, note that Debug.Log defaults to rounding 1 decimal place, and so in this example will always display y as 0.0. Suggest using a string formatter to display more decimal places:

 Debug.Log(gameObject.transform.localPosition.ToString("F4"));
 
Comment
Add comment · Show 2 · 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 Ritches · Jun 29, 2016 at 04:45 PM 0
Share

The calculation here is affecting its local position , in relation to its parent , so even being as small an offset as it is, visually its at the edge of the camera (due to the scale of its parent affecting this) until i try to do any sort of calculation to it , sorry probably should have made that bit clear. Like i mentioned previously if i just do integer * gameObject.transform.localPosition.y it works fine , although as soon as i try using the camera size it doesn't work despite the value still being 1. $$anonymous$$aybe i'm just being rather dim but i really cant find a solution to this other than reworking the setup i have currently.

avatar image btmedia14 Ritches · Jun 29, 2016 at 09:46 PM 0
Share

I am unable to make this fail ! The Vector3 generated values all seem to work fine no matter combinations of parent transforms or camera orthographic sizes. The only thing I can suggest is Debug.Log the value of Spawner.vertExtent to ensure it is always non-zero - again use string formatter to get more decimal places.

Perhaps also try changing the 8 in the calculation to 8.0f to force a float type.

If Spawner.vertExtent is defined as an Int, with a value less than 8, the division may occur as a division of integers resulting in a zero integer value before remainder of calculation. This is the only case I was able to create a zero y value.

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

59 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

Related Questions

float subtraction gives wild answers? 2 Answers

C# How can create a list of floats from different types of structs? 0 Answers

How to scale a float value to another scale ?¿ 0 Answers

Floats are acting really weird 0 Answers

Time.time value carrying over between timers 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