Quantcast
Channel: Latest Questions on Unity Answers
Viewing all articles
Browse latest Browse all 171066

Player Controller and Jumping

$
0
0
I am currently following walker boys tutorials and am working on the Mario clone. I've done a lot of things different from him due to personal preference. I am having a really hard time getting my Mario to jump correctly. Instead of jumping up and then falling back down, it's more like he instantly teleports to the height based on the value of his jump speed. Then after he is at the top, he will either do one of 2 things, if my gravity is low he will very very slowly and at a constant rate float back down to the ground. Or in the case that my gravity is high, he will instantly teleport back to the ground. I have found no middle ground here. He either sinks down slowly at a constant rate that is completely unrealistic or he teleports down instantly which is horrible. According to the tutorial my Mario should also jump higher or lower, but up to the maximum height based on how long i hold the jump key. For now i'm assuming this doesn't happen because he instantly teleports to the max jump height and hope that if he didn't do that then that part would be functioning correctly. Anyway, here's my code, I hope someone can help me figure out what's wrong here: #pragma strict //X Script //Static Variables //Variables var walkSpeed :float = 1.0; //Speed of the player var runSpeedFactor :float = 1.2; //Factor for run speed var crouchSpeedFactor :float = 0.8; //Factor for crouch speed var airSpeedFactor :float = 0.8; //Factor for air speed var jumpSpeed :float = 6.0; //Jump height of the player var fallSpeed :float = 2.0; //How fast the player falls var runJumpSpeedFactor :float = 1.2; //Shift jump height factor var crouchJumpSpeedFactor :float = 1.5; //Crouch jump height factor var gravity :float = 10.0; //Gravity (downforce) on player var animate :sSpriteAnimation; //Reference to the players SpriteAnimator var direction :String = "right"; //The last direction the player was facing var timeManager :sTimeManager; //Time manager reference var controller :CharacterController; //Reference to the character controller var state :String = "IdleR"; //The current state of the player var isJumping :boolean = false; //Is the player currently jumping? var isRunning :boolean = false; //Is the player currently running? var isCrouching :boolean = false; //Is the player currently crouching? var isGrounded :boolean = false; //Is the player currently on the ground? //Private Variables private var velocity :Vector3 = Vector3.zero; //For Referencing function Awake() { controller = GetComponent(CharacterController); //Reference the player controller animate = GetComponent(sSpriteAnimation); //Reference the animation script timeManager = GameObject.FindGameObjectWithTag("timemanager").GetComponent(sTimeManager); //Reference the time manager } //For Data function Start() { } //Main Loop function Update() { if(state != "Dead") { velocity = Vector3(Input.GetAxis("Horizontal"), 0, 0); velocity = transform.TransformDirection(velocity); var newSpeed :float = walkSpeed; GetInputModifiers(); if(!isGrounded) { newSpeed *= airSpeedFactor; } else { isJumping = false; } newSpeed = GetState(newSpeed); Jump(); velocity *= newSpeed; velocity.y -= gravity * Time.deltaTime; controller.Move(velocity * Time.deltaTime); Animate(); } } //Handle jumping function Jump() { if(isGrounded) { if(Input.GetButtonDown("Jump")) { if(isRunning) { velocity.y += jumpSpeed * runJumpSpeedFactor; } else if(isCrouching) { velocity.y += jumpSpeed * crouchJumpSpeedFactor; } else { velocity.y += jumpSpeed; } isJumping = true; } } else { if(Input.GetButtonUp("Jump")) { velocity.y -= fallSpeed; } } } //Capture input modifiers function GetInputModifiers() { if(controller.isGrounded) { isGrounded = true; } else { isGrounded = false; } if(Input.GetKey(KeyCode.LeftShift) && isGrounded) { isRunning = true; } else { isRunning = false; } if(Input.GetKey(KeyCode.C)) { isCrouching = true; } else { isCrouching = false; } } //Determine state and speed function GetState(newSpeed:float) : float { if(velocity.x > 0) { if(isRunning) { state = "RunningR"; newSpeed *= runSpeedFactor; } else if(isCrouching) { state = "CrouchingR"; newSpeed *= crouchSpeedFactor; } else { state = "WalkingR"; } direction = "right"; } else if(velocity.x < 0) { if(isRunning) { state = "RunningL"; newSpeed *= runSpeedFactor; } else if(isCrouching) { state = "CrouchingL"; newSpeed *= crouchSpeedFactor; } else { state = "WalkingL"; } direction = "left"; } else if(velocity.x == 0) { if(direction == "right") { if(isCrouching) { state = "IdleCrouchingR"; } else { state = "IdleR"; } } else { if(isCrouching) { state = "IdleCrouchingL"; } else { state = "IdleL"; } } } return(newSpeed); } //Animate the sprite based on state function Animate() { if(isJumping) { if(direction == "right") { animate.AnimateSprite(16, 16, 11, 2, 4, 16); } else { animate.AnimateSprite(16, 16, 11, 3, 4, 16); } } else { if(state == "WalkingR" || state == "WalkingL") { if(direction == "right") { animate.AnimateSprite(16, 16, 0, 2, 10, 16); } else { animate.AnimateSprite(16, 16, 0, 3, 10, 16); } } if(state == "CrouchingR" || state == "CrouchingL") { if(direction == "right") { animate.AnimateSprite(16, 16, 0, 6, 16, 16); } else { animate.AnimateSprite(16, 16, 0, 7, 16, 16); } } if(state == "RunningR" || state == "RunningL") { if(direction == "right") { animate.AnimateSprite(16, 16, 0, 4, 16, 16); } else { animate.AnimateSprite(16, 16, 0, 5, 16, 16); } } if(state == "IdleCrouchingR" || state == "IdleCrouchingL") { if(direction == "right") { animate.AnimateSprite(16, 16, 0, 8, 16, 16); } else { animate.AnimateSprite(16, 16, 0, 9, 16, 16); } } if(state == "IdleR" || state == "IdleL") { if(direction == "right") { animate.AnimateSprite(16, 16, 0, 0, 16, 16); } else { animate.AnimateSprite(16, 16, 0, 1, 16, 16); } } } }

Viewing all articles
Browse latest Browse all 171066

Trending Articles