- Home /
Question by
Vuoripeikko · Oct 31, 2012 at 09:33 PM ·
c#movementrigidbodyjumpgetbuttondown
GetButtonDown is unresponsive
I have been using Catlike Coding tutorial to make a platformer game. After finishing the initial tutorial, I decided to try and give the player a bit more free movement rather than just jumping, but the player character occasionally misses button-presses. Also the player seems to occasionally get stuck and unable to jump, while still able to move left and right.
using UnityEngine;
using System.Collections;
public class Runner : MonoBehaviour {
public static float distanceTraveled;
public float maxSpeed;
public float acceleration;
public float unacceleration;
public Vector3 boostVelocity, jumpVelocity;
public float gameOverY;
private bool touchingPlatform;
private Vector3 startPosition;
private static int boosts;
void Start (){
GameEventManager.GameStart += GameStart;
GameEventManager.GameOver += GameOver;
startPosition = transform.localPosition;
gameObject.active = false;
}
// Update is called once per frame
void Update () {
if(Input.GetButtonDown ("Jump")){
if(touchingPlatform) {
rigidbody.AddForce (jumpVelocity, ForceMode.VelocityChange);
touchingPlatform = false;
}
else if(boosts > 0){
rigidbody.AddForce (boostVelocity, ForceMode.VelocityChange);
boosts -= 1;
GUIManager.SetBoosts (boosts);
}
}
if(Input.GetAxis ("Horizontal") == -1){
MoveLeft ();
}
if(Input.GetAxis ("Horizontal") == 1){
MoveRight ();
}
distanceTraveled = transform.localPosition.x;
GUIManager.SetDistance(distanceTraveled);
if(transform.localPosition.y < gameOverY){
GameEventManager.TriggerGameOver();
}
}
void FixedUpdate() {
if(rigidbody.velocity.magnitude > maxSpeed)
{
rigidbody.velocity = rigidbody.velocity.normalized * maxSpeed;
}
}
void MoveLeft(){
rigidbody.AddForce (unacceleration, 0f, 0f, ForceMode.Acceleration);
}
void MoveRight(){
rigidbody.AddForce (acceleration, 0f, 0f, ForceMode.Acceleration);
}
void OnCollisionEnter () {
touchingPlatform = true;
}
void OnCollisionExit () {
touchingPlatform = false;
}
private void GameStart () {
boosts = 0;
GUIManager.SetBoosts(boosts);
distanceTraveled = 0f;
GUIManager.SetDistance(distanceTraveled);
transform.localPosition = startPosition;
rigidbody.isKinematic = false;
gameObject.active = true;
enabled = true;
}
private void GameOver (){
rigidbody.isKinematic = true;
enabled = false;
}
public static void AddBoost(){
boosts += 1;
GUIManager.SetBoosts(boosts);
}
}
Comment
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
rigidbody.Addforce the force doesnt apply? 1 Answer
Interpolate problem? Jump isn't smooth... 1 Answer
Jump Code Not Working (2D C#) 2 Answers
Multiple Cars not working 1 Answer