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
1
Question by Twinny · Sep 02, 2013 at 04:48 AM · not workingbox collider

Colliders not working

im currently working my way through the recently released "Classic Game Design" and for some reason the colliders just aren't working. This is the 2nd game in the book and they didn't work for the first one either (which was pong). The paddle just ends up clipping through the boundaries when you move it towards them. All of the objects have a box collider so i have no idea what could be causing this and whether or not this was an intentional decision from the author.

screen:

alt text

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

1 Reply

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

Answer by MrProfessorTroll · Sep 02, 2013 at 05:00 AM

Updated answer :)

 public class PlayerScript : MonoBehaviour 
 {
 
 
     void Start () 
     {
        Screen.lockCursor = true;
     }
 
 
     void Update () 
     {
        if(Input.GetKey("left"))
        {
          transform.Translate(-20 * Time.deltaTime, 0, 0);
        }
 
        if(Input.GetKey("right"))
        {
          transform.Translate(20 * Time.deltaTime, 0, 0);
        }
 
        float h = 30.0f * Time.deltaTime * Input.GetAxis ("Mouse X");
        transform.Translate (h, 0, 0);
        transform.position = new Vector3(Mathf.Clamp(transform.position.x, -5f, 5f), transform.position.y, transform.position.z);

     }
 
     void OnTriggerEnter (Collider other)
     {
        BallScript.yspeed = -BallScript.yspeed;
 
        if(other.collider.gameObject.transform.position.x > gameObject.transform.position.x)
        {
          BallScript.xspeed = Mathf.Abs(BallScript.xspeed);
        }
 
        else
        {
          BallScript.xspeed = - Mathf.Abs(BallScript.xspeed);
        }
 
        BallScript.collflag = true;
 
     }
 
 }
Comment
Add comment · Show 11 · 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 Twinny · Sep 02, 2013 at 05:08 AM 0
Share

yes but the ball isn't the problem. Everything works like its supposed to. The only problem is that when i move the paddle left or right or it travels straight through the field's borders.

avatar image MrProfessorTroll · Sep 02, 2013 at 05:11 AM 1
Share

Ok, then do you know about this: http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.Clamp.html

It clamps the position of anything. Do you need an example script?

avatar image Twinny · Sep 02, 2013 at 05:30 AM 0
Share

i don't think its time based but i should be more specific. when i press left or right the paddle translates perfectly along the X axis but when it hits a border it goes straight through ins$$anonymous$$d of stopping.

avatar image MrProfessorTroll · Sep 02, 2013 at 05:34 AM 1
Share

Yes, that is what you need. In the example they use time. That is what they are limiting. Here, try this:

 transform.position = new Vector3($$anonymous$$athf.Clamp(transform.position.x, -5f, 5f), transform.position.y, transform.position.z);

You can change '5' and '-5' to different values. Also, this is in C#. Do you need a Javascript version? Also, please trust me on this. Just plug in the code and keep moving until it stops.

And, put this on your paddle script AFTER you finish moving it.

avatar image MrProfessorTroll · Sep 02, 2013 at 05:42 AM 1
Share

Updated the answer :), you might have to change the '5' and '-5' to get the right number. An easy way is to play the game, move your paddle to a point where you want it to stop, record the X position, then replace it for '5' and '-5'

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

18 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

Related Questions

Colliders not working when used with a Rigidbody2D 1 Answer

Trigger for reloading level won't work! 1 Answer

Event Trigger is not firing (Unity 5) 0 Answers

Unity keeps crashing whenever I try to do anything! Please Help! 0 Answers

Why is root motion reduced once I apply the animation? 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