- Home /
Wait for seconds on collision
So I'm trying to create a script that will, on collision wait for 1/10th of a second and then zero the player velocity, speed etc (I'm doing this because otherwise the player spins of in a random direction with the force applied beforehand). How do I do this in C#.
This is my script so far:
using UnityEngine;
using System.Collections;
public class Player1 : MonoBehaviour {
public float directionalSpeed = 7.5f;
public float rotationalSpeed = 5f;
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate(Vector3.right * (-Input.GetAxis("Vertical")) * Time.deltaTime * directionalSpeed);
transform.Translate(Vector3.down * Input.GetAxis("Horizontal") * Time.deltaTime * directionalSpeed);
transform.Rotate(Vector3.forward * 10 * Input.GetAxis("Rotate") * Time.deltaTime * rotationalSpeed);
}
void OnCollisionEnter(Collision c)
{
transform.Translate (Vector3.zero);
}
}
Any suggestions?
Thanks, also, I'm self-taught and still at high school so I still have a lot to learn about with not much free time.
I'm also self-taught(not at high school though). $$anonymous$$eep coding) Physics
Answer by Gardosen · Nov 11, 2014 at 09:09 AM
Hello Kuro,
for everything where you want to use a waiting timer, you can use this http://docs.unity3d.com/ScriptReference/Coroutine.html
also if i see it correctly, you are doing all 3 calls on the update, even when it can be that only one is really needed. i recommend you to make a ifelse or a switchcase before it to really only call the fuctions you want to get called, instead of calling all.
Greetings Gardosen
Thanks, the top 3 transforms are for the movement and rotation of the player, I just decided to put the two parts in one script.