I am trying to code C# for character movement. I have been following a tutorial, and this is the code that I ended up with.
{
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
Rigidbody.Addforce(movement);
}
}
I do not know what my error is but when I try to test my game play, i get an error message in Unity saying "All complier errors must be fixed before you can enter play mode. please help me.
I am using the most recent version of Unity, unity 5
The tutorial you are following was created using Unity 4. Unity 5 handles the accessing of certain components (such a Rigidbody) differently.
The easiest way to fix your code is to replace:
Rigidbody.AddForce(movement);
With:
GetComponent<Rigidbody>().AddForce(movement);
A better way would be to cache the Rigidbody component in Start(), then access the cached version thereafter. Here is tutorial’s code rewritten for Unity 5 (and caching the Rigidbody component).
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float speed;
private Rigidbody Body;
void Start()
{
Body = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis( "Horizontal" );
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
Body.AddForce(movement * speed);
}
}
Hope this helps!