Question by
lukas0499 · May 05, 2017 at 03:58 AM ·
c#unity 5scripting problemvelocity
Error `UnityEngine.Component' does not contain a definition
I'm getting errors in unity with the next code
using UnityEngine;
using System.Collections;
public class Floater : MonoBehaviour {
public float waterLevel, floatHeight;
public Vector3 buoyancyCentreOffset;
public float bounceDamp;
void FixedUpdate () {
Vector3 actionPoint = transform.position + transform.TransformDirection(buoyancyCentreOffset);
float forceFactor = 1f - ((actionPoint.y - waterLevel) / floatHeight);
if (forceFactor > 0f) {
Vector3 uplift = -Physics.gravity * (forceFactor - rigidbody.velocity.y * bounceDamp);
rigidbody.AddForceAtPosition(uplift, actionPoint);
}
}
}
I get this two errors, one with "velocity" and the other one with "AddForceAtPosition"
If someone could help me, thank you!
Comment
Best Answer
Answer by Ahndrakhul · May 05, 2017 at 05:33 AM
I think that you need to replace all of your uses of rigidbody with GetComponent<Rigidbody>()
.
The FixedUpdate portion would look like:
void FixedUpdate () {
Vector3 actionPoint = transform.position + transform.TransformDirection(buoyancyCentreOffset);
float forceFactor = 1f - ((actionPoint.y - waterLevel) / floatHeight);
if (forceFactor > 0f) {
Vector3 uplift = -Physics.gravity * (forceFactor - GetComponent<Rigidbody>().velocity.y * bounceDamp);
GetComponent<Rigidbody>().AddForceAtPosition(uplift, actionPoint);
}
}
You could also just use a Rigidbody variable and assign it in the editor or Start().