Help with my movement script please?

I have made a so far succesful movement script for my game. I have however come across a few problems I am having trouble solving.

1 - Jumping - When I jump I disable the controls of the movement while in air like in real life, you cant change your direction in air. Problem is I cant get it too move in the direction I am currently going after I jump and jump at a consistant height and speed. It will sometimes barely jump at all. Any suggestions please ?.

2 - I use a ray to detect if i am on the ground, problem is that when just barely half the character is over a edge I stop and get stuck on that edge because the ray only get the very center so as soon as the center is off the edge your stuck… How do I solve this ?

Here is the current script

//Set Speed
var MoveSpeed = 3.5;
var BackwardsSpeed = 2.5;
private var CurrentMoveSpeed = 3.5;
var JumpForce = 250;



function Update () {

//Identify CameraRotator for Direction Information
var Rotator = GameObject.Find("CameraRotator");

//Cast Ray



//MOVING
//Identify Movement Buttons
var w =  Input.GetAxis("Forward");
var s =  Input.GetAxis("Backward");
var a =  Input.GetAxis("Left");
var d =  Input.GetAxis("Right");
var j = Input.GetAxis("Jump");

//Move Player
if (Physics.Raycast (transform.position, -transform.up, 1)) {
transform.Translate((d-a)*CurrentMoveSpeed*Time.deltaTime,0,(w-s)*CurrentMoveSpeed*Time.deltaTime, Rotator.transform);
}
//Backwards Speed Setup
	if (Input.GetButton("Backward")){
		
		CurrentMoveSpeed = BackwardsSpeed;
		
	}else{
	
CurrentMoveSpeed = MoveSpeed;

	}
}
//END OF MOVING





//JUMPING
function FixedUpdate () {

//Identify CameraRotator for Direction Information
var Rotator = GameObject.Find("CameraRotator");

//Jump
	if(Input.GetButtonDown("Jump")){	
	
		rigidbody.AddForce (Vector3.up * JumpForce);	
		rigidbody.AddForce (Rotator.transform.forward * JumpForce);	
		
	}
}

anyone?

You are doing your transform.Translate() inside of the conditional statement of whether you are grounded, so if you are not grounded, then no translation happens at all.

If instead, you calculate a moveDirection and continuously apply it, but only accept Input and modify the moveDirection if you are grounded.
This way you will keep moving in the last “known” moveDirection if you jump.

Look at the third person character controller example from Unity to see how they are doing it that way. You might even want to switch to using a characterController as what you are doing with translate, physics.raycast, and rigidbody.Addforce, is handled pretty well with it. But I realize you may specifically be designing a rigidbody controller so it will interact directly with other rigidbodies.

I dont exactly know the commands on some of that on how you do that… I already get that I need too get the direction but how I was sorta confused on, and I am trying to make my own scripts as much as possible and not use others… sorta against the point too use other peoples scripts when your trying too learn to make them =/

well basically what I mean is an adjustment to your code like this:

var moveDirection : Vector3 ;  //member variable

function Update()
{
  if ( Physics.Raycast (transform.position, -transform.up, 1) ) 
  {
     moveDirection  = Vector3( (d-a)*CurrentMoveSpeed*Time.deltaTime, 0, (w-s)*CurrentMoveSpeed*Time.deltaTime ) ;
  }

  transform.Translate( moveDirection, Rotator.transform );

  // other stuff, including jumping, etc.
}

so that the transform.Translate continues in whatever direction it was just before hitting jump, but any new key input is ignored unit the raycast becomes true again.

I was not suggesting you use the other code - I just meant to look at it and get some very helpful clues on how to design your own. It is what I do - I wrote my own but a lot was learned from the Unity version.

I did suggest using the characterController, which is a component that you still need to design a script to control it, but it has nice features such as an isGrounded variable, and Move function that behaves somewhere between transform.Translate and ridigdbody.AddForce

I dont get what the characterController is really. please explain?

Thanks for the help =)

Oh and the character doesnt move in the jump direction still =/ Did I do something wrong with this ?

just stops all movement in other directions.

//Set Speed
var MoveSpeed = 3.5;
var BackwardsSpeed = 2.5;
private var CurrentMoveSpeed = 3.5;
var JumpForce = 250;



function Update () {
	
	//Identify CameraRotator for Direction Information
	var Rotator = GameObject.Find("CameraRotator");
	
	//MOVING
	//Identify Movement Buttons
	var w =  Input.GetAxis("Forward");
	var s =  Input.GetAxis("Backward");
	var a =  Input.GetAxis("Left");
	var d =  Input.GetAxis("Right");
	var j = Input.GetAxis("Jump");
	
	//Move Player
	if (Physics.Raycast (transform.position, -transform.up, 1)) {
		
		MoveDirection = Vector3((d-a)*CurrentMoveSpeed*Time.deltaTime,0,(w-s)*CurrentMoveSpeed*Time.deltaTime);
		
		//Backwards Speed Setup
		if (Input.GetButton("Backward")){
			CurrentMoveSpeed = BackwardsSpeed;
			
		}else{
			CurrentMoveSpeed = MoveSpeed;
			
		}
	}
	
	transform.Translate(MoveDirection, Rotator.transform);
	
}
//END OF MOVING





//JUMPING
function FixedUpdate () {
	
	//Identify CameraRotator for Direction Information
	var Rotator = GameObject.Find("CameraRotator");
	
	//Jump
	if(Input.GetButtonDown("Jump")){
		
		rigidbody.AddForce (Vector3.up * JumpForce);
		
		
	}
}

Got that also XD

Some reason my guy doesnt jump very accurately though, sometimes he jumps the full height, other times he doesnt. Is the force way of jumping a bad way too go, or what other way could I do it?

My guy will still walk up 2-5 times the size of steps without me needing too jump, Why ?

//Set Speed
var MoveSpeed = 3.5;
var BackwardsSpeed = 2.5;
var JumpForce = 250;

private var CurrentMoveSpeed = 3.5;
private var moveDirection : Vector3;
private var Rotator : GameObject;

function Update () {
	
	//Identify CameraRotator for Direction Information
	Rotator = GameObject.Find("CameraRotator");
	
	//MOVING
	//Identify Movement Buttons
	var w =  Input.GetAxis("Forward");
	var s =  Input.GetAxis("Backward");
	var a =  Input.GetAxis("Left");
	var d =  Input.GetAxis("Right");
	var j = Input.GetAxis("Jump");
	
	//Move Player
	if (Physics.Raycast (transform.position, -transform.up, 1.1)) {
		
		moveDirection = Vector3((d-a)*CurrentMoveSpeed*Time.deltaTime,0,(w-s)*CurrentMoveSpeed*Time.deltaTime);
		
		//Backwards Speed Setup
		if (Input.GetButton("Backward")){
			CurrentMoveSpeed = BackwardsSpeed;
			
		}else{
			CurrentMoveSpeed = MoveSpeed;
			
		}
	}
	
	transform.Translate(moveDirection, Rotator.transform);
	
}
//END OF MOVING


//JUMPING
function FixedUpdate () {
	//Identify CameraRotator for Direction Information
	Rotator = GameObject.Find("CameraRotator");
	
	//Jump
	if(Input.GetButtonDown("Jump")){
		if(Physics.Raycast (transform.position, -transform.up, 1.1)){
			rigidbody.AddForce (Vector3.up * JumpForce);
			
		}
	}
}

You are translating the object in your update function, and adding force to it in a fixedUpdate function. Because you might be holding both a navigation key and jump key at the same moment, it is likely the two are conflicting with each other - resulting in the jump not always being what you want.

Again, in a character controller example, the upward jumping movement is also added to the moveDirection variable, so the direction of movement is shared in one place - when your transform.Translate is occurring, it is overriding the result of the Addforce.
Perhaps if your " if (Input.GetButtonDown(“Jump”)) " were integrated with the other input conditions in the Update function, you could intercept the inputs better so that the jump is done without interference of the other inputs.

you made the point that you want to learn by making your own script, which is fine, but still looking at other examples such as the third person controller or maybe some other rigidbody controllers would help.

I am having trouble figuring out how exactly the character motor and fps controller work together to make movement XD

How could I make my jump integrated with the update function, I have tryed using a boolean to check but it still has the same effect. I wanted to keep the fixed update sence it applied physics to the jump but i cant get same result in normal update… I am sorta torn on what to do.

I also was wondering if their was a way to make it so that the way I can make my self move same pace even when pressing both forward and sideways and not go twice as fast because the side and forward speeds are together atleast that is how it feels in my head XD

Currently I use this for my w,s,a,d movement inputing

moveDirection = Vector3((d-a)CurrentMoveSpeedTime.deltaTime,0,(w-s)CurrentMoveSpeedTime.deltaTime);