Hello Unity Community,
I’m trying to create a slight delayed jump at 1.3 seconds after the player has collided with specific objects. For example, when the user collides with certain objects that have a tag called “Collision” and then they jump, it should take 1.3 seconds before the player jumps.
Here’s my code:
using UnityEngine;
using System.Collections;
public class MoveForward : MonoBehaviour {
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
public float i;
public int MaxSpeed = 300;
public int MinSpeed = 12;
public GameObject OtherObj;
// Update is called once per frame
void Update () {
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded) {
moveDirection = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"),(.09f*i)/100);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
if (i < MaxSpeed)
{
i++;
//print (i);
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
void OnCollisionEnter (Collision collider) {
if (OtherObj.collider.tag == "Collision")
{
Debug.Log("We've collided");
}
}
}
I did a quick Debug.Log test to see if collision is being detected (as you’ll see in the script) and it isn’t detecting it. I’ve tagged the object my player runs into with “Collision”. I’m supposed to see “We’ve collided” in the console but nothing shows up. I’ve also attached a rigidbody to the object and a box collider.
So my questions are:
- Why isn’t collision being detected?
- How do I delay jump?
Any help is very much appreciated. Thank you for your time.