Delay Jumping

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.

void OnCollisionEnter (Collision collider) { if (OtherObj.collider.tag == “Collision”) { Debug.Log(“We’ve collided”); } }

OtherObj isn’t defined. OtherObj isn’t the object you collided with. thats called collider

so this is what you need

collider.collider.tag = “Collision”
Except thats kind of confusing so i’d suggest

void OnCollsionEnter(collion OtherObj)

Then keep your original code inside.

Have a nice day and mark as answered.

Okay so I tried a different method and it works now.

Here’s my code:

void OnControllerColliderHit (ControllerColliderHit OtherObj) {
		Rigidbody body = OtherObj.collider.attachedRigidbody;
		if (OtherObj.collider.tag == "Collision")
		{
			Debug.Log("We've collided");	
		}

So, 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;
		
	
	// 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 OnControllerColliderHit (ControllerColliderHit OtherObj) {
		Rigidbody body = OtherObj.collider.attachedRigidbody;
		if (OtherObj.collider.tag == "Collision")
		{
			Debug.Log("We've collided");
		}
	}

	}

What do I have to put in my if statement to get my jumping delayed by 1.3 seconds. That’s the big problem I need to figure out.