- Home /
My Player flys off away through the X axis when I press play.
What's going on? I want the player to be able to move the x and Z and jump, but I've had some trouble getting the Z to work.
@script RequireComponent(Rigidbody); //forces this gameObject to also have a rigibody component
//public variables
var maxAirJumpCount : int = 1; // 1 for a double jump
var normalJumpForce : float = 8; //strength of the normal jump's impulse
var airJumpForce : float = 8; //strength of the double jump's impulse
var raycastDistance : float = 1; //distance between the characters origin and the ground, 1 for a 2m high character
var xSpeed : float = 5; // horizontal speed of the character\
var zSpeed : float = 5; //zspeed
/* These values are all ideal settings that worked for me */
//private variables
private var airJumpCount : int = 0; //how many more times can the player jump?
private var xMove : float; // horizontal input (Input.GetAxisRaw("Horizontal")) *
// horizontal speed modifier (xSpeed)
private var isGrounded : boolean = false; //is the player on the ground?
function Update() {
if(Physics.Raycast(transform.position, -Vector3.up, raycastDistance)) {
isGrounded = true;
airJumpCount = maxAirJumpCount;
}
else {
isGrounded = false;
}
if(Input.GetKeyDown(KeyCode.Space)) {
if(isGrounded) {
JumpNormal();
}
else if(airJumpCount > 0) {
airJumpCount -= 1;
JumpInAir();
}
}
xMove = 0;
xMove = Input.GetAxisRaw("Vertical") * xSpeed;
z = Input.GetAxisRaw("Horizontal") * zSpeed;
transform.Translate(0,0,0);
}
function FixedUpdate () {
rigidbody.AddForce(Vector3.right*xMove);
}
function JumpNormal () { rigidbody.AddForce(Vector3.up * normalJumpForce, ForceMode.Impulse); }
function JumpInAir () { rigidbody.AddForce(Vector3.up * airJumpForce, ForceMode.Impulse); }
Answer by codestage · Jul 18, 2014 at 09:44 AM
I can't see how you use z variable in your code. Looks like it should be used in Translate(). Are you sure you really wish to make such translate:
transform.Translate(0,0,0);
? It does nothing.
I just need to edit the code to make Z move like X, by pressing the horizontal buttons. but I thought what I had was correct.
Answer by Fearthefrozt · Jul 18, 2014 at 04:35 PM
First you don't need to do this:
xMove = 0;
The function for getting the axis will return 0 if nothing is being pressed so you don't really need it.
Next you're using transform.Translate which you don't want to do if you're moving with physics so you can take out the line:
transform.Translate(0,0,0);
What you're really interested in is this:
function FixedUpdate () { rigidbody.AddForce(Vector3.right*xMove); }
this line is what is actually moving your player, so you want to do a similar thing with the z, so since you're using the vertical axis I'm assuming you want to move forward and backward with the z axis. This would be the line to add inside of FixedUpdate():
rigidbody.AddForce(Vector3.forward*z);
This will move forward or backward.
Thanks man! Is there anyway to make him move forward backward and left and right?
@script RequireComponent(Rigidbody); //forces this gameObject to also have a rigibody component
//public variables
var maxAirJumpCount : int = 1; // 1 for a double jump
var normalJumpForce : float = 8; //strength of the normal jump's impulse
var airJumpForce : float = 8; //strength of the double jump's impulse
var raycastDistance : float = 1; //distance between the characters origin and the ground, 1 for a 2m high character
var xSpeed : float = 5; // horizontal speed of the character\
var zSpeed : float = 5; //zspeed
/* These values are all ideal settings that worked for me */
//private variables
private var airJumpCount : int = 0; //how many more times can the player jump?
private var x$$anonymous$$ove : float; // horizontal input (Input.GetAxisRaw("Horizontal")) *
// horizontal speed modifier (xSpeed)
private var isGrounded : boolean = false; //is the player on the ground?
function Update() {
if(Physics.Raycast(transform.position, -Vector3.up, raycastDistance)) {
isGrounded = true;
airJumpCount = maxAirJumpCount;
}
else {
isGrounded = false;
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) {
if(isGrounded) {
JumpNormal();
}
else if(airJumpCount > 0) {
airJumpCount -= 1;
JumpInAir();
}
}
x$$anonymous$$ove = Input.GetAxisRaw("Vertical") * xSpeed;
z = Input.GetAxisRaw("Horizontal") * zSpeed;
}
function FixedUpdate () {
rigidbody.AddForce(Vector3.right*x$$anonymous$$ove);
rigidbody.AddForce(Vector3.forward*x$$anonymous$$ove);
}
function JumpNormal () { rigidbody.AddForce(Vector3.up * normalJumpForce, Force$$anonymous$$ode.Impulse); }
function JumpInAir () { rigidbody.AddForce(Vector3.up * airJumpForce, Force$$anonymous$$ode.Impulse); }
Ah sorry I made a mistake in my response, it should be:
rigidbody.AddForce(Vector3.forward*z);
switch out the x$$anonymous$$ove in the second line to the z variable that's taking the horizontal axis, this should allow for movement all around. You might want to switch the x$$anonymous$$ove and z variables depending on which keys you want to move side to side vs forward, I'll edit my answer to reflect this.
I want Z to control the horizontal movement while X is the vertical movement. Picture the game like a first person platformer. You would move Z with "A" and "D", and move X with "W" and "S". I tried adding your code, is this right? Because I seem to be getting an error saying "$$anonymous$$ Identifier 'Z'" Your help has been great man btw :D
@script RequireComponent(Rigidbody); //forces this gameObject to also have a rigibody component
//public variables
var maxAirJumpCount : int = 1; // 1 for a double jump
var normalJumpForce : float = 8; //strength of the normal jump's impulse
var airJumpForce : float = 8; //strength of the double jump's impulse
var raycastDistance : float = 1; //distance between the characters origin and the ground, 1 for a 2m high character
var xSpeed : float = 5; // horizontal speed of the character\
var zSpeed : float = 5; //zspeed
/* These values are all ideal settings that worked for me */
//private variables
private var airJumpCount : int = 0; //how many more times can the player jump?
private var x$$anonymous$$ove : float; // horizontal input (Input.GetAxisRaw("Horizontal")) *
// horizontal speed modifier (xSpeed)
private var isGrounded : boolean = false; //is the player on the ground?
function Update() {
if(Physics.Raycast(transform.position, -Vector3.up, raycastDistance)) {
isGrounded = true;
airJumpCount = maxAirJumpCount;
}
else {
isGrounded = false;
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) {
if(isGrounded) {
JumpNormal();
}
else if(airJumpCount > 0) {
airJumpCount -= 1;
JumpInAir();
}
}
x$$anonymous$$ove = Input.GetAxisRaw("Vertical") * xSpeed;
z = Input.GetAxisRaw("Horizontal") * zSpeed;
}
function FixedUpdate () {
rigidbody.AddForce(Vector3.forward*z);
rigidbody.AddForce(Vector3.forward*x$$anonymous$$ove);
}
function JumpNormal () { rigidbody.AddForce(Vector3.up * normalJumpForce, Force$$anonymous$$ode.Impulse); }
function JumpInAir () { rigidbody.AddForce(Vector3.up * airJumpForce, Force$$anonymous$$ode.Impulse); }
function FixedUpdate () {
rigidbody.AddForce(Vector3.forward*z);
rigidbody.AddForce(Vector3.forward*x$$anonymous$$ove);
Shouldn't this be:
function FixedUpdate () {
rigidbody.AddForce(Vector3.forward*z);
rigidbody.AddForce(Vector3.right*x$$anonymous$$ove);//the difference can be found in this argument
I think changing this up will provide the desired outcome described when you say, "I want Z to control the horizontal movement while X is the vertical movement." Your current implementation is basically saying that for either Input.GetAxisRaw("Vertical")
or Input.GetAxisRaw("Horizontal")
move forward by speeds z
or x$$anonymous$$ove
...if I understand it right (I know 0 java)