Question by
Str1987 · Sep 19, 2017 at 11:45 AM ·
updatefixedupdategetbutton
GetButtonUp works but not always
Hello, people!
Some script for manipulation of ball. There is condition:
if (Input.GetButtonUp ("Jump") & touchGround)
{
// Если еще не красный шар то обычный прыжок
if (r < 0.7f) {
rb.AddForce (Vector3.up * jump);
touchGround = false;
playerSoud.PlayOneShot (jumpBall, 1f);
ballSkin.color = new Color (0.19f, 0.08f, 0.08f, 1.0f);
}
// Если красный то высокий прыжок
if (r >= 0.7f)
{
rb.AddForce (Vector3.up * bigJump);
touchGround = false;
playerSoud.PlayOneShot (bigJumpBall, 1f);
ballSkin.color = new Color (0.19f, 0.08f, 0.08f, 1.0f);
}
And problem that GetButtonUp works but not always, maybe you have some experience with this?
If ball on the ground and color.r of skin ball more or less 0.7f, ball do BigJump or Jump/ Below all code.
public class ControlPlayer : MonoBehaviour {
public float speed;
public float jump;
public float bigJump;
public AudioClip pickUpCollected;
public AudioClip jumpBall;
public AudioClip bigJumpBall;
public bool touchGround;
public Material ballSkin;
private Rigidbody rb;
private AudioSource playerSoud;
private float r;
private float g;
private float b;
void Start ()
{ // Присваиваем переменной rb доступ к Rigidbody
rb = GetComponent<Rigidbody> ();
playerSoud = GetComponent<AudioSource> ();
}
void Update ()
{
// Если переменная Фалсе (шар в полете)то обнуляем rb.AddForce для того что бы нельзя было управлять шариком в полёте
if (touchGround == false)
{
speed = 0;
}
// Шар на земле, значит им моджно манипулировать, вар speed присваиваем 5
if (touchGround == true)
{
speed = 5f;
}
// При нажатом левом шифте, умножаем скорость на 2
if (Input.GetButton ("Fire3"))
{
speed *= 2;
}
// Смена цвета шара до красного, что бы было понятно что шар готов в большому прыжку
if (Input.GetButton ("Jump") & touchGround)
{
// Смена цвета SkinBall
r = ballSkin.color.r + Time.deltaTime;
g = ballSkin.color.g + -Time.deltaTime;
b = ballSkin.color.b + -Time.deltaTime;
if (r >= 0.78f)
{
r = 0.78f;
}
if (g <= 0.0f)
{
g = 0.0f;
}
if (b <= 0.0f)
{
b = 0.0f;
}
ballSkin.color = new Vector4 (r, g, b, 1.0f);
}
}
void FixedUpdate ()
{
// Управление движения вперед назад вправо лево
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0f, moveVertical);
rb.AddForce (movement * speed);
// Управление прыжок
if (Input.GetButtonUp ("Jump") & touchGround)
{
// Если еще не красный шар то обычный прыжок
if (r < 0.7f) {
rb.AddForce (Vector3.up * jump);
touchGround = false;
playerSoud.PlayOneShot (jumpBall, 1f);
ballSkin.color = new Color (0.19f, 0.08f, 0.08f, 1.0f);
}
// Если красный то высокий прыжок
if (r >= 0.7f)
{
rb.AddForce (Vector3.up * bigJump);
touchGround = false;
playerSoud.PlayOneShot (bigJumpBall, 1f);
ballSkin.color = new Color (0.19f, 0.08f, 0.08f, 1.0f);
}
}
}
void OnTriggerEnter(Collider other)
{ // При столкновени с объектом с триггером PickUp - этот объект выключается
if (other.gameObject.CompareTag ("PickUp"))
{
other.gameObject.SetActive (false);
playerSoud.PlayOneShot (pickUpCollected, 1f);
}
// При столкновени с объектом с триггером Ground - булевой переменной touchGround присваиваем true
// Для того что бы можно было управлять ашриком на земле
if (other.gameObject.CompareTag ("Ground"))
{
touchGround = true;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How to handle FixedUpdate() and Input 0 Answers
Transform.translate -> Update or FixedUpdate 1 Answer
Addforce in fixedupate 0 Answers
Only Disable One Update on a Script 1 Answer
Score, FixedUpdate and Update 0 Answers