- Home /
how can i change the x position while jumping on air using c#?
have a character controller which jumps but while jumping i want to change the x position of the character so basically he can turn while jumping, this is my attempt so far
//start of character controller
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded) {
//get the player vector movement vector
movePlayer = new Vector3(Input.acceleration.x,0,1);
//float h = Input.acceleration.x;
//translate the players movement
transform.Translate(movePlayer * moveSpeed * Time.deltaTime);
//the run animation
animation.CrossFade("run");
if (Input.GetButton("Jump")){
//my problem is here, the x axis on the vector3 is not happening
movePlayer =transform.TransformDirection(new Vector3(Input.acceleration.x,jumpSpeed,forwardJumpSpeed));
}
}
// attach the gravity and move controller
movePlayer.y -= gravity * Time.deltaTime;
controller.Move(movePlayer * Time.deltaTime);
my problem here is commented in the code.
by the way code works fine, its just in the jump section on the transform.TransformDirection(x is not working,y is good, z is good) :((
its funny because when i put a float like this it works, it changes the direction
if (Input.GetButton("Jump")){
movePlayer =transform.TransformDirection(new Vector3(2.5f,jumpSpeed,forwardJump));
}
but when i put this:
if (Input.GetButton("Jump")){
//my problem is here, the x axis on the vector3 is not happening
movePlayer =transform.TransformDirection(new Vector3(Input.acceleration.x,jumpSpeed,forwardJumpSpeed));
}
it deosnt work, its so wierd, i think thier is a bug in unity, is thier an email i can contact.
Answer by Bunny83 · Mar 22, 2012 at 07:40 PM
I'm just wondering. Input.acceleration returns the realworld acceleration of a mobile device (iPhone). Are you sure that's what you're after?
Usually you would use something like Input.GetAxis ("Horizontal")
i tried both of them, they both don't work!!! :(( thanks hough
Ehmm, i actually asked (indirectly) for clarification. Do you want to develop for iPhone / Android or Standalone (Win / $$anonymous$$ac) or Webplayer?
Have you tried usng Debug.Log() to print out the values to see what is happening? We can't do this for you, we don't have your project.
Another thing is it seems you move your character two times. A charactercontroller is not a physic object. It should only be moved via the .$$anonymous$$ove()
function. transform.Translate
bypasses the collision detection.
btw just read it again. You said you want the player "can turn while jumping". You only move your player sideways, you never turn it or was that the expected behaviour?
Your answer
Follow this Question
Related Questions
a better movement code C# 3 Answers
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Implement the equation as a code? 1 Answer