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 dovster1 · Dec 07, 2018 at 01:06 AM · guigameobjectpositionpositioningx-axis

Hi everone! I am new to Unity so sorry if this question stupid. But basically can you help me fix the Code?

For now, it will debug to the log Not Hit every time the objects collide instead of debugging hit when its less than zero. Can you please help me fix the code? Thanks!! This is the code: using UnityEngine; using System.Collections; public class PlayerMovement : MonoBehaviour {

 public float moveSpeed = 05f;
 public VJHandler jsMovement;

 private Vector3 direction;
 private float xMin, xMax, yMin, yMax;

 public GameObject BlueX;


 void Start()
 {
     //Initialization of boundaries, used to be 50
     xMax = Screen.width - 15;
     xMin = 15;
     yMax = Screen.height - 15;
     yMin = 15;
 }

 void Update()
 {

     direction = jsMovement.InputDirection; //InputDirection can be used as per the need of your project

     if (direction.magnitude != 0)
     {

         transform.position += direction * moveSpeed;
         transform.position = new Vector3(Mathf.Clamp(transform.position.x, xMin, xMax), Mathf.Clamp(transform.position.y, yMin, yMax), 0f);//to restric movement of player
     }
 }

 
 private void OnTriggerEnter2D(Collider2D collision)
 {


          if (collision.tag == "BLUEPLAYER")
     {
         if (BlueX.transform.position.x <= 0)
         {
             Debug.Log("HIT");
             //return;
         }


         else if (BlueX.transform.position.x >= 0)
         {
             Debug.Log("Not Hit");
         }
     }
 }

}

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 Vega4Life · Dec 07, 2018 at 02:28 AM 0
Share

Well, code doesn't lie. If "Not hit" is being debugged, then BlueX object's X position is always >= 0. So you need to figure out why that is.

avatar image dovster1 · Dec 07, 2018 at 02:32 PM 0
Share

Ya, that's exactly why I need help. LOL

1 Reply

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

Answer by darkStar27 · Dec 07, 2018 at 04:25 AM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [System.Serializable]
 public class Boundary {
     public float xMin, xMax, yMin, yMax;
 }
 public class PlayerMovement : MonoBehaviour {
 
     public float moveSpeed = 05f;
     public VJHandler jsMovement;
     public GameObject blueX;
 
     private Vector3 direction;
     private Boundary boundary;
 
     void Start () {
         //Initialization of boundaries, used to be 50
         boundary.xMax = Screen.width - 15;
         boundary.xMin = 15;
         boundary.yMax = Screen.height - 15;
         boundary.yMin = 15;
     }
     void Update () {
         direction = jsMovement.InputDirection; //InputDirection can be used as per the need of your project
         if (direction.magnitude != 0)
         {
             transform.position += direction * moveSpeed;
             transform.position = new Vector3(
                 Mathf.Clamp(transform.position.x, boundary.xMin, boundary.xMax), 
                 Mathf.Clamp(transform.position.y, boundary.yMin, boundary.yMax), 
                 0f
             );                                                                                //to restric movement of player
         }
     }
 
     private void OnTriggerEnter2D (Collider2D collision) {
         if (collision.tag == "BLUEPLAYER")
         {
             if (blueX.transform.position.x <= 0)
             {
                 Debug.Log("HIT");
                 //return;
             }
             else if (blueX.transform.position.x >= 0)
             {
                 Debug.Log("Not Hit");
             }
         }
     }
 }
 


I've rewritten a code above with some modifications.

First of all you can use a separate class for your boundary, this way the code will be reusable and you can also refer to the class to set boundaries to another GameObjects say you enemies.

Now from above code, it does not have any errors, but from the logic I see that you use OnTriggerEnter2D on the player. What i don't understand where are you phasing the problem.

Please explain where exactly are the errors, hat the errors are, what behavior do you expect that is not happening.

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 dovster1 · Dec 07, 2018 at 02:29 PM 0
Share

@darkStar27 Thank you for helping with the code. So there aren't errors per se, it's just that it will debug to the log not hit whether or not it's below or above zero. What I want it to do is debug to the log Hit if its X-axis position is less than zero and Not Hit if its position is more than zero. Right now it will say Not Hit either way. So I need help with that.

avatar image darkStar27 dovster1 · Dec 10, 2018 at 04:34 AM 0
Share

You can try checking for the conditions in the update function like this:

 void Update()
 {
              if (blueX.transform.position.x <= 0)
              {
                  Debug.Log("HIT");
                  //return;
              }
              else if (blueX.transform.position.x >= 0)
              {
                  Debug.Log("Not Hit");
              }
         
 }


If it works then there is some problem with OnTrigger function.

avatar image dovster1 darkStar27 · Dec 10, 2018 at 05:12 PM 0
Share

@darkStar27 Thanks for the help but I actually got the answer myself. Basically, I debugged to the log the position of the GameObject and turns out the object wasn't anywhere near zero. So I fixed it. Thanks anyways!

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

222 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 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 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

Positioning gameobjects by a corner, not by center 0 Answers

Change GUI button position in code 1 Answer

Is it possible to position a GameObject as relative to a GUI? 2 Answers

GUI Button and a gameObject's position 3 Answers

How to implement a system for "Road-Construction"? 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