- Home /
How do i make precision as the key to the outcome?
Hello everyone. Can anyone tell me if it is possible to make some kind of area where, if your character hits a button within this area(lets say spacebar, to jump), then the jump would be higher. Is that possible?
Lets say it was a TRAMPOLINE, if you press at the beginning, you wouldn't get that good of at jump, but if you were at the center, you would get the max height of the jump. hope you guys can help me :D
Answer by alireza97 · Sep 24, 2019 at 11:10 AM
You can put a Trigger collider in that area and give it some tag (for example 'TRAMPOLINE') and when the user hits the button you should check if the character 'OnTriggerStay' function says that character is in that place you should increase the power of the jump.
something like this:
bool OnTRAMPOLINE = false;
float jumpPower = 10.0f;//for example
float TRAMPOLINE_jump_multiple = 1.5f;//for example
float final_Jump_Power;
void Update()
{
if (Input.GetKey(KeyCode.Space))
{
if (OnTRAMPOLINE)
{
final_Jump_Power = jumpPower * TRAMPOLINE_jump_multiple ;
}
else
{
final_Jump_Power = jumpPower;
}
}
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "TRAMPOLINE")
{
OnTRAMPOLINE = true;
}
}
void OnTriggerStay(Collider other)
{
if (other.tag == "TRAMPOLINE")
{
OnTRAMPOLINE = true;
}
} // This one is NOT necessary
void OnTriggerExit(Collider other)
{
if (other.tag == "TRAMPOLINE")
{
OnTRAMPOLINE = false;
}
}
Thanks man. This community is just the best. Thank you for your time.
This would work just fine, but do you know if it is possible to make the multiplier increasingly bigger. lets say the further he gets to the center, the better the jump. i would guess that this would be more complicated.
You can save position of center and when character enter the collider and press 'space', calculate the distance of character and position of center and further distance get smaller 'TRA$$anonymous$$POLINE_jump_multiple' became bigger.
(something like this)
Vector3 center;
float maxDistance;
void Update()
{
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space))
{
if (OnTRA$$anonymous$$POLINE)
{
float distance = Vector3.Distance(transform.position, center);
TRA$$anonymous$$POLINE_jump_power = maxDistance - distance;
final_Jump_Power = jumpPower * TRA$$anonymous$$POLINE_jump_power;
}
else
{
final_Jump_Power = jumpPower;
}
}
}
Yea, that is just the thing i needed. Thanks a lot @alireza97
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
2D C# Jump Script Addforce 2 Answers
Jump/grounding bugs, advice appreciated 1 Answer
i want to get my to cube to rotate 90 degrees on the x-axis as it jumps 0 Answers