using UnityEngine;
public class playercontroller : MonoBehaviour {
public Rigidbody rb;
public float forwardforce = 2000f;
void fixedupdate ()
{
rb.AddForce(0, 0, forwardforce * Time.deltaTime);
}
}
using UnityEngine;
public class playercontroller : MonoBehaviour {
public Rigidbody rb;
public float forwardforce = 2000f;
void fixedupdate ()
{
rb.AddForce(0, 0, forwardforce * Time.deltaTime);
}
}
@solderboss You need to make sure that you use proper case when calling built-in Unity callback methods. The problem is that you have fixedupdate in all lower-case. It should be FixedUpdate(). Most programming languages out there are case-sensitive. Once you change the method name, you ought to have your script work, as long as rb has a value in the editor.
For robustness, you could consider:
using UnityEngine;
// This forces Unity to add a Rigidbody component to your object at design and runtime.
[RequireComponent(typeof(Rigidbody))]
public class playercontroller : MonoBehaviour {
public Rigidbody rb;
public float forwardforce = 2000f;
void Start()
{
if (!GetComponent<Rigidbody>()) // If a Rigidbody component doesn't exist on this object,
rb = AddComponent<Rigidbody>(); // Add one and assign it to the rb variable
else // Otherwise,
rb = GetComponent<Rigidbody>(); // Assign rb to the Rigidbody component already on this object
}
void FixedUpdate ()
{
// Add a force of 2000 units along the forward axis each physics update
rb.AddForce(0, 0, forwardforce * Time.deltaTime);
}
}