Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by latsushi · Nov 19, 2012 at 10:02 PM · c#collisionjumpingdelay

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sparkzbarca · Nov 19, 2012 at 10:05 PM 0
Share

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.

avatar image latsushi · Nov 19, 2012 at 10:06 PM 0
Share

I formatted it.

avatar image sparkzbarca · Nov 19, 2012 at 10:14 PM 0
Share

i answered it. :)

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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");    
         }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sparkzbarca · Nov 19, 2012 at 11:56 PM 0
Share

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

avatar image
0

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image latsushi · Nov 19, 2012 at 10:18 PM 0
Share

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.

avatar image sparkzbarca · Nov 19, 2012 at 10:21 PM 0
Share

are you sure your just not tagged.

try

void OnCollisionENter(collision OtherObj) {

     Debug.Log(OtherObj);
     
     }
avatar image latsushi · Nov 19, 2012 at 10:21 PM 0
Share

Sorry about the bad formatting. I can't seem to format code properly in the comments section. :-/

avatar image sparkzbarca · Nov 19, 2012 at 10:23 PM 0
Share

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"

avatar image latsushi · Nov 19, 2012 at 10:32 PM 0
Share

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?)

Show more comments
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sparkzbarca · Nov 19, 2012 at 11:54 PM 0
Share

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. :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Can't figure out how to detect ground (2D) 1 Answer

Character Fails To Jump Sometimes 1 Answer

OnTriggerEnter2D/OnCollisionEnter2D - delay 1 Answer

Can someone help me with the jumping in this Player Controller script? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges