New guy coding help. Is this correct?

Hey everybody!

I’m very new to unity and I’m trying my best to figure it out. In order to learn I’ve been using a book called “Game Development with Unity” by Michelle Menard. I’m trying to create a very simple character controller. As far as I’m concerned I should be able to move the character now. I’ve followed the instructions to the letter and made sure my code matches the book yet it does not work. I’ve even taken the code provided by the book and tried implementing it and that wont work either. The book uses an older version of unity (not sure which) and maybe that’s the problem? I not sure what to do anymore so I’m coming here to see if somebody can figure out what the issue is.

I’m a novice programmer and this is my first time trying to code in unity. I figured since the books supplied code wont work there is definitely a problem.

I really hope the problem isn’t something embarrassing that i over looked!

Please ignore the code comments i was just making notes as i went along.

Here is the pasted code I’m afraid i don’t know what you mean by

  brackets.

// movement control variables
var rollSpeed = 6.0;
var fastRollSpeed=2.0;
var JumpSpeed=8.0;
var gravity = 20.0;
var rotateSpeed=4.0;
var duckSpeed=0.5;
// current placement trackers
private var moveDirection=Vector3.zero;
private var grounded:boolean=false;
private var moveHorz=0.0;
private var normalHeight=2.0;
private var duckHeight=1.0;
private var rotateDirection=Vector3.zero;

var isControllable: boolean = true;

var controller: CharacterController;
controller=GetComponent(CharacterController);

//if you arent controllable reset
function FixedUpdate(){
if(!isControllable)
   Input.ResetInputAxes();
   else{
   
   // start of if grounded movment control --------------------------------------
   if(grounded){ // get its xyz coordinates when grounded
   moveDirection = new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
   moveDirection = transform.TransformDirection(moveDirection);
   moveDirection *= rollSpeed;
   controller.height = normalHeight; // putting back to normal size each update
   controller.center.y = controller.height/2;
   
   moveHorz = Input.GetAxis("Horizontal"); // all based on horizontal axis
   if(moveHorz > 0) // positive values are right negative are left 0 is forward and back
   rotateDirection = new Vector3(0,1,0); //turn right
   else if(moveHorz < 0)
   rotateDirection = new Vector3(0,-1,0); //turn left
   else
   rotateDirection = new Vector3(0,0,0);
   
   
   if(Input.GetButton ("Jump")) {
   moveDirection.y = JumpSpeed; // y position is changed to jumpspeed variable
   }
   if(Input.GetButton("Boost")){// boost speed is current speed X boost variable
   moveDirection *= fastRollSpeed;
   }
   if(Input.GetButton("Duck")){
   controller.height=duckHeight;
   controller.center.y = controller.height/2 + .25;
   moveDirection *= duckSpeed;}
   
   moveDirection.y -= gravity * Time.deltaTime;// multiplying by time delta is doing stuff by the second
   
   // Getting the controller to move and stay with character
   var flags = controller.Move(moveDirection * Time.deltaTime);
   controller.transform.Rotate(rotateDirection * Time.deltaTime, rotateSpeed);
   grounded= ((flags  CollisionFlags.CollidedBelow)!=0);
   
   
   }// end of if grounded -----------------------------------------------------------


}}

1280150–57631–$WidgetController.js (2.29 KB)

New guy needing coding help, just paste the code in the forum post, but use the brackets. It's easier to read (for mobile anyhow). Also, make sure you have your Console window open. It can be opened from the Windows drop down menu. It will tell you your error, what it is and where it is located.

I Tried looking through it again and the console window isn’t reporting anything.

Quillish… please reformat your code using the code tags.

example

Also have you attached your code to an object?

You have placed the script on a GameObject with a CharacterController right?

The error is this:

controller=GetComponent(CharacterController);

You cant do that outside a method. Here is a fixed script:

// movement controls
var rollSpeed = 6.0;
var fastRollSpeed=2.0;
var JumpSpeed=8.0;
var gravity = 20.0;
var rotateSpeed=4.0;
var duckSpeed=0.5;
// current placement trackers
private var moveDirection=Vector3.zero;
private var grounded:boolean=false;
private var moveHorz=0.0;
private var normalHeight=2.0;
private var duckHeight=1.0;
private var rotateDirection=Vector3.zero;

var isControllable: boolean = true;

var controller: CharacterController;

//controller=GetComponent(CharacterController); < This is wrong

//The below is correct.
function Start(){
	controller=GetComponent(CharacterController);
}

// stuff might go here

//if you arent controllable reset
function FixedUpdate(){
	if(!isControllable){
		Input.ResetInputAxes();
	}else{
		   // start of if grounded movment controll all based on keys i have to set up--------------------------------------
		if(grounded){ // get its xyz coordinates when grounded
			moveDirection = new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
			moveDirection = transform.TransformDirection(moveDirection);
			moveDirection *= rollSpeed;
			controller.height = normalHeight; // putting back to normal size each update
			controller.center.y = controller.height/2;
			moveHorz = Input.GetAxis("Horizontal"); // all based on horizontal axis
			
			if(moveHorz > 0) // positive values are right negative are left 0 is forward and back
				rotateDirection = new Vector3(0,1,0); //turn right?
			else if(moveHorz < 0)
				rotateDirection = new Vector3(0,-1,0); //turn left
			else
				rotateDirection = new Vector3(0,0,0);
		  
		   
			if(Input.GetButton ("Jump")) {
				moveDirection.y = JumpSpeed; // y position is changed to jumpspeed variable
			}
			if(Input.GetButton("Boost")){// boost speed is current speed X boost variable
				moveDirection *= fastRollSpeed;
			}
			if(Input.GetButton("Duck")){
				controller.height=duckHeight;
				controller.center.y = controller.height/2 + .25;
				moveDirection *= duckSpeed;
			}
			moveDirection.y -= gravity * Time.deltaTime;// multiplying by time delta is doing stuff by the seco
			// Getting the controller to move and stay with character
			var flags = controller.Move(moveDirection * Time.deltaTime);
			controller.transform.Rotate(rotateDirection * Time.deltaTime, rotateSpeed);
			grounded= ((flags  CollisionFlags.CollidedBelow)!=0);  
		}// end of if grounded -----------------------------------------------------------
	}
}