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 Guigondi · Dec 13, 2016 at 06:42 PM · scripting problemjumpjumping

problem with jump script

In this Script it seems i can't use the OnCollisionStay the way i did. How do i check the tag of the Object my player collides with every frame then? i'm trying to use that to set the "grounded" boolean to true everytime he touches something with the tag "Ground" How do i fix this script?

using UnityEngine; using System.Collections;

public class Movement : MonoBehaviour {

 public Rigidbody rb;
 public float speed = 10.0F;
 public float rotationSpeed = 100.0F;
 public bool grounded = true;

 // Use this for initialization
 void Start ()
 {
     rb = GetComponent<Rigidbody> ();

 }



 // Update is called once per frame
 void Update ()
 {

     float translation = Input.GetAxis ("Vertical") * speed;
     float rotation = Input.GetAxis ("Horizontal") * rotationSpeed;
     translation *= Time.deltaTime;
     rotation *= Time.deltaTime;
     transform.Translate (0, 0, translation);
     transform.Rotate (0, rotation, 0);
 


     if (Input.GetKeyDown (KeyCode.Space)) {
         if (grounded == true) {

             rb.AddForce (0, 400, 0);
             grounded = false;
 
         }
     }



     void OnCollisionStay (Collision col)           //in this part i can't use OncollisionStay it seems.
     {
         if (col.gameObject.tag == "Ground") {

             grounded = true;
         }

     }

     Debug.Log (grounded);
 }


}

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by JaredHD · Dec 14, 2016 at 08:34 PM

I would make the grounds collider slightly higher than the ground and make sure it is a trigger collider then add the following code to the ground (Not the player)

Also note you would need to make the grounded variable a static variable or access grounded by using the .GetComponent() in the ground script

 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.tag == "Player")
     {
         Movement.grounded = true;
     }
 }

More simply (Just thought of this) you can determine your current height

  void Update()
  {
      if(transform.position.y > 0.5f)
      {
        grounded = false;
      }
      else
      {
        grounded = true;
      }
  }

Comment
Add comment · 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
0

Answer by Guigondi · Dec 15, 2016 at 12:47 AM

@JaredHD I'd rather do the first method you suggested since using the Y position like you did would limit the height variety of the platforms i could use to jump.

I tried followingyour first suggestion and i'm pretty sure i've made a few obvious mistakes (i'm still learning code).I'm not sure how to properly use GetComponent.

here is the script i did for the ground based on what you showed.

using UnityEngine; using System.Collections;

public class colisao_com_chao : MonoBehaviour {

 GameObject Mov;

 // Use this for initialization
 void Start () {
     Mov = Player.GetComponent<Movement> ();
 }

 void OnTriggerEnter (Collider colisao)
 {
     if (colisao.gameobject.tag == "Player") 
     {
         Movement.grounded = true;

     }

 }

 // Update is called once per frame
 void Update () {
 
 }

}

Comment
Add comment · Show 4 · 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 JaredHD · Dec 15, 2016 at 05:39 AM 1
Share

The .GetComponent(); is used to retrieve a script (On a Gameobject) that you wrote to access it's public variables. ins$$anonymous$$d of .GetComponent(); you could make all the variables static to access them but that's not always best.

 GameObject $$anonymous$$ov;
 
 void Start()
 {
     //$$anonymous$$ake sure the player is named "Player" no quotes or add a player Tag and use this commented code ins$$anonymous$$d
     // $$anonymous$$ov = GameObject.FindWithTag("Player").GetComponent<$$anonymous$$ovement>();
     $$anonymous$$ov = GameObject.Find("Player");
 }
 void OnTriggerEnter(Collider colisao)
 {
     if (colisao.tag == "Player")
     {
         //$$anonymous$$ov edits the grounded variable, $$anonymous$$ake sure grounded is a public bool
         $$anonymous$$ov.GetComponent<$$anonymous$$ovement>().grounded = true;
     }
 }

Please tell me if there are any errors

avatar image Guigondi JaredHD · Dec 15, 2016 at 04:26 PM 0
Share

it shows an error on line 12 the line with

"$$anonymous$$ov = GameObject.FindWithTag("Player").GetComponent ();"

it says it's not possible to convert implicitly the "$$anonymous$$ovement" type in UnityEngine.GameObject. I don't know what that means.

By the way i also tried with "$$anonymous$$ov = GameObject.Find ("Player");" and while it didn't gave any errors the grounded variable still won't become true when i touch the ground after i jump.

here is the current code:

using UnityEngine; using System.Collections;

public class colisao_com_chao : $$anonymous$$onoBehaviour {

 GameObject $$anonymous$$ov;

 // Use this for initialization
 void Start () {
     $$anonymous$$ov = GameObject.FindWithTag("Player").GetComponent<$$anonymous$$ovement> ();
 }

 void OnTriggerEnter (Collider colisao)
 {
     if (colisao.tag == "Player") 
     {
         $$anonymous$$ov.GetComponent<$$anonymous$$ovement> ().grounded = true;

     }

 }

 // Update is called once per frame
 void Update () {
 
 }

}

avatar image Havax Guigondi · Dec 15, 2016 at 09:46 PM 1
Share

$$anonymous$$ovement is a class, $$anonymous$$ov is a GameObject. Your setting the GameObject $$anonymous$$ov equal to a component of a GameObject, not the GameObject itself, therefore, $$anonymous$$ov needs to be of similar type to the component you are setting it to. In this case, it's "$$anonymous$$ovement mov;" ins$$anonymous$$d of "GameObect mov".

Show more comments

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

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

Related Questions

Jump Force/Velocity is Wrong and Strange. Help 1 Answer

Unity 2D Enemy jump Question (Frog jump) 1 Answer

Multi-touch buttons 0 Answers

Need help with Character Jump! 0 Answers

* A simple script where every single time my character jumps it will increase speed *, * A needed script where every time you perform a jump and land your speed increases * 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