I have a javascript player controller that works.
#pragma strict
//For player movements
var speed : float = 4;
var jumpHeight : float = 5;
var superSpeed : float = 10;
var superSpeedTime : float = 5;
//The players amount of health
//var health : int = 5;
//var target : GameObject;
private var lockCursor : boolean = true;
//To adjust the speed of the player
private var superSpeedOn : boolean = false;
private var superSpeedElapsed : float = 0;
function Awake ()
{
Screen.lockCursor = lockCursor;
}
function OnDisable()
{
Screen.lockCursor = false;
}
function Update()
{
if (Input.GetMouseButtonUp(0))
{
Screen.lockCursor = lockCursor;
}
}
function FixedUpdate () {
//Handles the horizontal movement of the player
rigidbody.velocity.x = speed * Input.GetAxis("Horizontal");
if(superSpeedOn == true){
rigidbody.velocity.x = superSpeed * Input.GetAxis("Horizontal");
//Add to elapsed time
superSpeedElapsed += Time.fixedDeltaTime;
//Check if enough time has passed
if(superSpeedElapsed >= superSpeedTime){
superSpeedOn = false;
}
} else {
rigidbody.velocity.x = speed * Input.GetAxis("Horizontal");
}
//Handle the jumping of the player
if( Input.GetButton("Jump") && IsGrounded() ){
rigidbody.velocity.y = jumpHeight;
}
//Check if we are hitting the sides of an object
var distance : float = rigidbody.velocity.magnitude * Time.fixedDeltaTime;
var hit : RaycastHit;
if (rigidbody.SweepTest(rigidbody.velocity, hit, distance)){
//Stop moving!
rigidbody.velocity.x = 0;
}
}
function IsGrounded (){
//Fire a raycast to check for the ground
return Physics.Raycast(transform.position, Vector3.down, collider.bounds.extents.y + 0.01);
}
/*
function Hit (){
//Remove one hit point and check if we're dead.
health -= 1;
if(health == 0){
Debug.Log("You died!");
Destroy(gameObject);
}
}
function OnCollisionEnter(collision : Collision ){
//Check if we hit an object that can hurt me
if(collision.gameObject.tag == "Hurt")
Hit();
}
*/
/*function SuperSpeed () {
//Turn on superspeed!
superSpeedOn = true;
superSpeedElapsed = 0;
}*/
But my C# player controller has no errors but doesn’t even budge at all?
using UnityEngine;
using System.Collections;
public class PlayerControlC : MonoBehaviour {
float speed = 4f;
float jumpHeight = 5f;
private bool lockCursor = true;
void Awake()
{
Screen.lockCursor = lockCursor;
}
void OnDisable()
{
Screen.lockCursor = false;
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonUp(0))
{
Screen.lockCursor = lockCursor;
}
}
void FixedUpdate()
{
//Handles the horizontal movement of the player
bool isGrounded = Physics.Raycast(transform.position, Vector3.down, collider.bounds.extents.y + 0.01f);
Vector3 velocity = rigidbody.velocity;
velocity.x = speed * Input.GetAxis("Horizontal");
//Handles the jumping of the Player
if (Input.GetButton("Jump") && isGrounded == true)
{
velocity.y = jumpHeight;
}
//Check if we are hitting the sides of an Object
float distance = rigidbody.velocity.magnitude * Time.fixedDeltaTime;
RaycastHit hit;
if (rigidbody.SweepTest(rigidbody.velocity, out hit, distance))
{
//Stop moving!
velocity.x = 0f;
}
}
/* void IsGrounded()
{
//Fire a raycast to check for the ground
// Physics.Raycast(transform.position, Vector3.down, collider.bounds.extents.y + 0.01f);
}*/
}