- Home /
Player movement
Hello i am new to programming and i've tried creating a character so far i've done the basic movement (W,A,S,D) but i'm having a hard time with creating the jump,sprint and crouch buttons can someone give me some tips how to make those actions work. This is what i have so far
var moveSpeed : float;
function Update () {
if(Input.GetKey("w")) {
transform.position.x+=moveSpeed;
}
if(Input.GetKey("s"))
{
transform.position.x-=moveSpeed;
}
if(Input.GetKey("d"))
{
transform.position.z-=moveSpeed;
}
if(Input.GetKey("a"))
{
transform.position.z+=moveSpeed;
}
There are millions of questions, forum threads, articles, tutorials all over the internet about this thing.
Go through some of them to read about the basics before you jump directly into coding.
If you want to move your player use rigidbody/charactercontroller components ins$$anonymous$$d of changing the position. Translating players position makes him immune to collisions. If you want to implement jump adequate to your movement technique then you have to apply some gravity force and in each physic step you have to add some force and subtract gravity force from it.
function Jump(force : float){
transform.position.y += (force - gravity);
}
Where force is totalForce / frames and gravity lets say 9.81f.
As you can see this method can be waste of time. When using rigidbody you can simply do something like thi:
function Jump(force : float){
rigidbody.AddForce(new Vector2( 0, force ));
}
Where force is a totalForce of your jump.
Hope it helped. :)
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
WheelColliders Bug Fix 0 Answers
Smooth Player Movement 1 Answer