- Home /
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.
format your code.
Press the edit and then there is button along the top of the text box. One of the buttons is 101 010. Press that button and insert the code in that. HT$$anonymous$$L auto formats and HT$$anonymous$$L formatting doesnt look anything like code.
thats just info for next time though, answer is posted.
Answer by latsushi · Nov 19, 2012 at 10:56 PM
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");
}
you dont need the rigid body. your not doing anything with it even.
Remove the line Rigidbody body...
it does nothing.
your mistake which I should have caught my bad, was you were using a CharacterController. The collisions for those are special.
Hence the need for OnControllerColliderHit
ins$$anonymous$$d of OnCollisionEnter
Answer by sparkzbarca · Nov 19, 2012 at 10:09 PM
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.
sparkzbarca, I'm grateful for your quick response but you have some typos, so I'm confused about what to put
Here's my code: void OnCollisionEnter (Collision OtherObj) { if (OtherObj.collider.tag == "Collision") { Debug.Log("We've collided"); }
I'm still not getting collision detection.
are you sure your just not tagged.
try
void OnCollisionENter(collision OtherObj) {
Debug.Log(OtherObj);
}
Sorry about the bad formatting. I can't seem to format code properly in the comments section. :-/
let me know if that gives the name of the other object.
If it does the collision is being detected its just not matching because tag != "Collision"
also i'm not sure if a colliders tag is the same thing as the game objects tag.
if the other object is what is tagged try OtherObj.tag == "Collision"
Here's my code:
void OnCollisionEnter (Collision OtherObj) {
if (OtherObj.collider.tag == "Collision") { Debug.Log(OtherObj); }
Nothing appears on the console. I've tried OtherObj.tag and I get an error that says:
`UnityEngine.Collision' does not contain a definition for `tag' and no extension method `tag' of type `UnityEngine.Collision' could be found (are you missing a using directive or an assembly reference?)
Answer by latsushi · Nov 19, 2012 at 11:01 PM
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.
thats easy enough
void Jumpfunction(){
//code to jump }
invoke(JumpFunction,1.3);
invoke takes an optional second argument which tells how long to delay in seconds more information on invoke can be found here.
http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.Invoke.html
more information on how to delay lots of other things and delaying in general can be found at.
http://unitygems.com/mistakes1/
you want to click on
"make something happen in the future co-routines and invoke"
and read what it shows when the topic is expanded.
I would suggest you just check out unity gems in general it has lots of good content.
Notice also my comment on your answer. :)