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 MagickAurus · Dec 16, 2014 at 07:56 PM · floatfloatingboatunexpected-token

Unexpected token if

Hi im new in unity and im trying to make an object float in the water and i make a script but gives me an error. This is the code can someone help me? :

 var waterlevel : float =4;
 var floatheight : float =  2;
 var bouyancyCentreOffset : Vector3;
 private var forceFactor : float;
 private var actionPoint : Vector3;
 private var upLift : Vector3;
 function Update ()
 {
   
   actionPoint = transform.position + transform.TransformDirection(bouyancyCentreOffset);
   forceFactor = if - ((actionPoint.y = waterlevel) / floatHeight);
   
    if (forceFactor > 0f)
   {
   
      upLift = -Physics.gravity * (forceFactor - rigidbody.velocity.y * bounceDamp);
      rigidbody.AddForceAtPosition(upLift, actionpoint);
      
    }
 }  

  
Comment
Add comment · Show 3
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 MrSoad · Dec 16, 2014 at 07:27 PM 0
Share

You got that from the vid on youtube :)

It's a good script to have found but you did not "make" it :D

You have a typo on line 11. You have typed "if" it should be 1f

avatar image MagickAurus · Dec 16, 2014 at 07:39 PM 0
Share

But now the "=" before 1f gives an error dont know why, what can i do?

avatar image MagickAurus · Dec 16, 2014 at 11:32 PM 0
Share

that code works perfectly thanks

1 Reply

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

Answer by MrSoad · Dec 16, 2014 at 09:31 PM

Yep you had more than just the one error. The second = on that line should have been a - . You had various uppercase/lowercase typos, and you had not declared "bounceDamp" or initialized it.

I found the tutorial and checked your script against it, this should be it without the errors :

 var waterLevel : float = 4.0;
 var floatHeight : float =  2.0;
 var bounceDamp : float = 0.05;
 var bouyancyCentreOffset : Vector3;
 
 private var forceFactor : float;
 private var actionPoint : Vector3;
 private var upLift : Vector3;
 
 function Update() {
 
     actionPoint = transform.position + transform.TransformDirection(bouyancyCentreOffset);
     forceFactor = 1f - ((actionPoint.y - waterLevel) / floatHeight);
     
     if (forceFactor > 0f) {
     
         upLift = -Physics.gravity * (forceFactor - rigidbody.velocity.y * bounceDamp);
         rigidbody.AddForceAtPosition(upLift, actionPoint);
     }
 } 

Have fun, it really is a great little script :D

Edit : For completeness for anyone who likes this script, and as it only seems fair to the guy who made it and the tutorial, here is a link to it :

https://www.youtube.com/watch?v=mDtnT5fh7Ek

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Hovering smoothly at one height 1 Answer

Realistic boat physics? 0 Answers

Object buoyancy in water on different water levels? 1 Answer

Rigidbody Floating Problem? 2 Answers

Damping forces to stop boat oscillation 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