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 /
avatar image
0
Question by tarekhfahmi · Nov 08, 2017 at 11:29 AM · colliderjumpboolean

having some trouble creating conditionals in unity.

so im trying to make it so that if the player is touching something and spacebar is pressed, the player jumps.

 if (Input.GetKeyDown(KeyCode.Space) && PlayerController.OnTriggerEnter (collider))
             rb.AddForce(new Vector3(0, 10, 0), ForceMode.Impulse);
 


the code im using. i know it shouldnt work in the state its currently in, but im not sure how to do the collider part. (my player is a ball btw.)

Comment
Add comment · Show 2
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 tmalhassan · Nov 08, 2017 at 02:51 PM 0
Share

So you're trying to restrict the player from jumping while in mid air?

avatar image tarekhfahmi tmalhassan · Nov 08, 2017 at 03:27 PM 0
Share

Yeah exactly

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by William4458 · Nov 08, 2017 at 03:11 PM

You can't do it like that. First of all OnTriggerEnter is the complete wrong fuction. You need to use OnCollisionEnter (). Secondly, these are Unity callback functions like void Update or void Start and you can't call nor create functions in an if statement. Here's a way to go about this:

bool isTouchingSomething; float jumpForce = 10.0f

void Update (){ if (Input.GetKeyDown (KeyCode.Space) && isTouchingSomething){ GetComponent (typeof(Rigidbody)).AddForce (new Vector3(0, jumpForce, 0)); isTouchingSomething = false; }

} void OnCollisionEnter (){ isTouchingSomething = true; }

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 tarekhfahmi · Nov 08, 2017 at 03:32 PM 0
Share

Thanks! Should i put the bool at the top of the code? and also where should i put the rest of it lol im completely new to coding and unity so im pretty lost.. this is the code im using:

using UnityEngine; using UnityEngine.UI; using System.Collections; using UnityEngine.Scene$$anonymous$$anagement;

public class PlayerController : $$anonymous$$onoBehaviour {

 public float speed;
 public Text countText;
 public Text winText;
 public float jump;

 private Rigidbody rb;
 private int count;

 void Start()
 {
     rb = GetComponent<Rigidbody>();
     count = 0;
     SetCountText();
     winText.text = "";
 }

 void FixedUpdate()
 {
     float moveHorizontal = Input.GetAxis("Horizontal");
     float moveVertical = Input.GetAxis("Vertical");

     Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

     rb.AddForce(movement * speed);

     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
         rb.AddForce(new Vector3(0, 10, 0), Force$$anonymous$$ode.Impulse);


 }


 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.CompareTag("Pick Up"))
     {
         other.gameObject.SetActive(false);
         count = count + 1;
         SetCountText();
     }
 }

 void SetCountText()
 {
     countText.text = "Count: " + count.ToString();
     if (count >= 12)
     {
         winText.text = "You Win!";

       
     }
 }

}

avatar image
0

Answer by tmalhassan · Nov 08, 2017 at 03:46 PM

Well, it is just as @William4458 suggested. I used your code to write the script and to show you where to position them exactly:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using UnityEngine.SceneManagement;
 
 public class PlayerController : MonoBehaviour
 {
 
     public float speed;
     public Text countText;
     public Text winText;
     public float jump;
 
     private Rigidbody rb;
     private int count;
 
     private bool jumpIsEnabled = true;
 
     void Start()
     {
         rb = GetComponent<Rigidbody>();
         count = 0;
         SetCountText();
         winText.text = "";
     }
 
     void FixedUpdate()
     {
         float moveHorizontal = Input.GetAxis("Horizontal");
         float moveVertical = Input.GetAxis("Vertical");
 
         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
 
         rb.AddForce(movement * speed);
 
         if (Input.GetKeyDown(KeyCode.Space) && jumpIsEnabled == true)
         {
             rb.AddForce(new Vector3(0, 10, 0), ForceMode.Impulse);
             jumpIsEnabled = false;
         }
     }
 
     private void OnCollisionEnter(Collision collision)
     {
         if (collision.gameObject.tag == "Ground")
         {
             jumpIsEnabled = true;
         }
     }
 }

Keep in mind that you will need to add the tag Ground to your ground object (tags are case sensitive). Also, make sure that both the player and the ground have colliders and they're both not set as Is Trigger.

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

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

88 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

Related Questions

Objects stops if it lands in between two colliders 1 Answer

My Player keeps launching into the air 4 Answers

(C#) A better way to limit actions to once per button press? 2 Answers

Making a "vault over an obstacle" script 0 Answers

Change Boolean with collision problem 2 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