Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by tayyab43 · Oct 01, 2015 at 07:52 PM · c#movementjumpbooleantranslate

Why doesn't the collider work?

I'm currently trying to fix it so that when the cube is on the ground it can jump but after that cannot jump until it hits the ground again, however its just not jumping and am unsure why. Here is my code:

     public GameObject Box;
     public Vector3 BoxRot;
     public float BoxX;
     public float BoxY;
     public float BoxZ;
     public float JumpHeight=100f;
     public Vector3 JumpTo;
     public GameObject floor;
     public bool jumping=false;
     public float CurX;
     public string FloorTag = "Floor";
 
     void OnCollisionEnter(Collision Col)
     {
         Debug.Log("collision started");
         if (floor.gameObject.tag == FloorTag)
         {
             jumping = false;
             Debug.Log("jumping=" + jumping.ToString());
         }
     }
     void OnCollisionStay (Collision Col)
     {
         Debug.Log("colliding");
         if (floor.gameObject.tag == FloorTag)
         {
             Debug.Log("jumping=" + jumping.ToString());
         }
     }
     void OnCollisionExit (Collision Col)
     {
         Debug.Log("exiting");
         if (floor.gameObject.tag == FloorTag)
         {
             jumping = true;
             Debug.Log("jumping=" + jumping.ToString());
         }
     }
     void Sideways()
     {
         if (Input.GetKeyDown(KeyCode.LeftArrow))
         {
             CurX = Box.transform.position.x;
             Box.transform.position = Vector3.Lerp(new Vector3(CurX,Box.transform.position.y, Box.transform.position.z), new Vector3(CurX-35, Box.transform.position.y, Box.transform.position.z),2*Time.deltaTime);
         }
         if (Input.GetKeyDown(KeyCode.RightArrow))
         {
             CurX = Box.transform.position.x;
             Box.transform.position = Vector3.Lerp(new Vector3(CurX, Box.transform.position.y, Box.transform.position.z), new Vector3(CurX + 35, Box.transform.position.y, Box.transform.position.z), 2 * Time.deltaTime);
         }
 
 
     }
 
     void Update () 
     {
         Debug.Log("jumping=" + jumping.ToString());
         Sideways();
         Box.transform.Translate (0,0, 10 * Time.deltaTime);
         BoxY = Box.transform.position.y;
         JumpTo = new Vector3 (0, JumpHeight, Box.transform.position.y);
         if ((Input.GetKeyDown ("space"))&&(jumping=false))
         {
             Debug.Log("jumping=" + jumping.ToString());
              Box.transform.Translate(Vector3.up * 100 * Time.deltaTime, Space.World);
         }
          
     }
 
 
 
 
 }
Comment
Add comment · Show 1
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 Suddoha · Oct 01, 2015 at 09:04 PM 0
Share

I haven't checked the complete code, but first of all you should fix line 62:

 if ((Input.Get$$anonymous$$eyDown ("space"))&&(jumping=false))

You missed a '=' there. It should read:

 if (Input.Get$$anonymous$$eyDown ("space") && jumping==false) 

or even better:

 if (Input.Get$$anonymous$$eyDown ("space") && !jumping)

1 Reply

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

Answer by Cherno · Oct 01, 2015 at 09:19 PM

You don't check if the object you collided with is a floor, you always check against the pre-defines floor object from your script.

This:

 void OnCollisionEnter(Collision Col)
      {
          Debug.Log("collision started");
          if (floor.gameObject.tag == FloorTag)
          {
              jumping = false;
              Debug.Log("jumping=" + jumping.ToString());
          }
      }

Needs to be this:

 void OnCollisionEnter(Collision Col)
      {
          Debug.Log("collision started");
          if (Col.gameObject.tag == FloorTag)
          {
              jumping = false;
              Debug.Log("jumping=" + jumping.ToString());
          }
      }

The same goes true for the other Collision function in your script.

Comment
Add comment · Show 3 · 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 tayyab43 · Oct 04, 2015 at 04:10 PM 0
Share

OF COURSE! I make stupid mistakes like this all the time, the problem is I think I must have missed something as it still isn't working.

my code now looks like this:

 public class $$anonymous$$ovement : $$anonymous$$onoBehaviour {
     public GameObject Box;
     public Vector3 BoxRot;
     public float BoxX;
     public float BoxY;
     public float BoxZ;
     public float JumpHeight=100f;
     public Vector3 JumpTo;
     public GameObject floor;
     public bool jumping=false;
     public float CurX;
     public string FloorTag = "Floor";
 
     void OnCollisionEnter(Collision Col)
     {
         Debug.Log("collision started");
         if (Col.gameObject.tag == FloorTag)
         {
             jumping = false;
             Debug.Log("jumping=" + jumping.ToString());
         }
     }
     void OnCollisionStay (Collision Col1)
     {
         Debug.Log("colliding");
         if (Col1.gameObject.tag == FloorTag)
         {
             Debug.Log("jumping=" + jumping.ToString());
         }
     }
     void OnCollisionExit (Collision Col2)
     {
         Debug.Log("exiting");
         if (Col2.gameObject.tag == FloorTag)
         {
             jumping = true;
             Debug.Log("jumping=" + jumping.ToString());
         }
     }
     void Sideways()
     {
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftArrow))
         {
             CurX = Box.transform.position.x;
             Box.transform.position = Vector3.Lerp(new Vector3(CurX,Box.transform.position.y, Box.transform.position.z), new Vector3(CurX-35, Box.transform.position.y, Box.transform.position.z),2*Time.deltaTime);
         }
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.RightArrow))
         {
             CurX = Box.transform.position.x;
             Box.transform.position = Vector3.Lerp(new Vector3(CurX, Box.transform.position.y, Box.transform.position.z), new Vector3(CurX + 35, Box.transform.position.y, Box.transform.position.z), 2 * Time.deltaTime);
         }
 
 
     }
 
     void Update () 
     {
         Debug.Log("jumping=" + jumping.ToString());
         Sideways();
         Box.transform.Translate (0,0, 10 * Time.deltaTime);
         BoxY = Box.transform.position.y;
         JumpTo = new Vector3 (0, JumpHeight, Box.transform.position.y);
         if (Input.Get$$anonymous$$eyDown ("space") && !jumping)
         {
             Debug.Log("jumping=" + jumping.ToString());
              Box.transform.Translate(Vector3.up * 100 * Time.deltaTime, Space.World);
         }
          
     }
 
 
 
 
 }

avatar image tayyab43 · Oct 04, 2015 at 04:45 PM 0
Share

never $$anonymous$$d I fixed it, thanks for the help bro

avatar image Cherno tayyab43 · Oct 06, 2015 at 09:56 PM 0
Share

If you want to help others, I might want to say what you did to fix it :)

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

30 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how do I make my character jump while running without losing all your speed 1 Answer

While (starting jumping or in jump) holding W key makes game object jump high and fall slow. 1 Answer

Why is my bool being set to true when program is run? 1 Answer

jump code character can jump twice please any one fix this 0 Answers

C# Code won't ket me jump 0 Answers


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