- Home /
Do I overcomplicate something in this code?
I would like to attain it by the cube being an object only,let him jump then when I ovecome it W button. When I keep it pushed though W button,let the cube not be hopping though continuously then.
using UnityEngine;
using System.Collections;
public class CubePlayerJumpScript : MonoBehaviour {
private float jumpSpeed = 1100f; //moveSpeed = 5f;
private bool heClashed = false;
// Use this for initialization
void Start()
{
Physics.gravity = new Vector3(0, -70, 0);
}
// Update is called once per frame
void Update()
{
}
void OnCollisionEnter(Collision theCollision)
{
if(theCollision.gameObject.name == "Cube Soil")
{
heClashed = true;
rigidbody.constraints = RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotation;
}
}
void OnGUI()
{
Event e = Event.current;
if(e.isKey)
switch(e.keyCode)
{
case KeyCode.W:
{
if(heClashed && Input.GetKeyDown(e.keyCode))
{
rigidbody.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotation;
rigidbody.AddForce(Vector3.up * jumpSpeed);
heClashed = false;
}
break;
}
default:
break;
}
}
}
$$anonymous$$y question it,that overcomplicate this something and,how I shall solve the task by the usage of switch how so,how the switch should not be needed-et unnecessary one to do with this condition exa$$anonymous$$ation: Input.Get$$anonymous$$eyDown(e.keyCode)
I want to solve it by the usage of switch so,that if I keep it pressed W let my finger not be jumping then s$$anonymous$$dily the cube object.
The code what you see here it works perfectly only I have trouble with him,that the switch becomes unnecessary because of the condition exa$$anonymous$$ation in him.
But I would like to use it anyway the switch. Namely because of that because there will be many buttons then that down tooth to need to shake hands and 20 or 30 would not like it to write a condition exa$$anonymous$$ation.