- Home /
 
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);
      
    }
 }  
  
 
              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
But now the "=" before 1f gives an error dont know why, what can i do?
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 :
Your answer
 
             Follow this Question
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