- Home /
Question by
DecroixNicolas · Feb 07, 2017 at 05:33 PM ·
scripting problemjumping object
bumper script
Can you help me to improve this script. It works but are coding is quite barbaric ^^ ty.
public float speed; public Vector3 force; private bool stopPlayer; private float timer; private Rigidbody rb;
private void Start()
{
stopPlayer = false;
}
public void Update()
{
if (stopPlayer)
{
MooveBumper();
}
}
private void OnTriggerEnter(Collider other)
{
rb = other.GetComponent<Rigidbody>();
stopPlayer = true;
timer = 0f;
}
void MooveBumper()
{
if (timer < 1f)
{
rb.transform.position += force * Time.deltaTime * speed;
timer += Time.deltaTime;
}
else
{
stopPlayer = false;
}
}
Comment
Answer by herDev · Feb 08, 2017 at 10:00 AM
I haven't actually tested this (writing it blind), but hopefully it works :)
bool stopPlayer;
Rigidbody rb;
float force;
Vector3 speed;
private void Start()
{
stopPlayer = false;
}
public void Update()
{
if (stopPlayer) rb.transform.position += force * Time.deltaTime * speed;
}
private void OnTriggerEnter(Collider other)
{
rb = other.GetComponent<Rigidbody>();
StartCoroutine(EventOutcome(1f));
}
IEnumerator EventOutcome(float delay)
{
stopPlayer = true;
yield return new WaitForSeconds(delay);
stopPlayer = false;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612110445im_/https://answers.unity.com/themes/thub/images/avi.jpg)