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
0
Question by nathanvs · Jun 10, 2014 at 05:59 AM · javascriptmovementcars

How to make a car not go off the screen?

Hi,

I have been having this issue for a long time. I have a car which moves left and right which works fine but the car can go off the screen which I don't want, how can I define the area's in which it can move inside? I have tried lots of other help questions and none of them have helped. I am making this as a 2D game. Here's my car javascript. (gravity is 0)

 #pragma strict
 var moveLeft : KeyCode;
 var moveRight : KeyCode;
 
 var speed : float = 10;
 
 
 function Update () {
 
  if (Input.GetKey(moveLeft))
      {
       rigidbody2D.velocity.x = speed;
      }
      
  else if (Input.GetKey(moveRight))
 {
 rigidbody2D.velocity.x = speed * -1;
 }
 
  else
      {
       rigidbody2D.velocity.x = 0;
  
      }
 
 
 rigidbody2D.velocity.y=0;
 
 
 }


Thanks in advance -Nathan

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
0

Answer by TobiUchiha · Jun 10, 2014 at 06:36 AM

There are 2 things that can be done:

1) Attach a boxcollider to the car and make 2 invisible quads that can act as an invisible wall and attach a boxcollider to them as well.

2)Use Mathf.Clamp to Clamp the position of the car on the x axis.

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 Noob_Vulcan · Jun 10, 2014 at 06:13 AM

Attach Box Colliders at the corner Edges . I have a template . Just convert it to your convenience ...

         public BoxCollider topWall;
         public BoxCollider botWall;
         public BoxCollider leftWall;
         public BoxCollider rightWall;
 
 
 
         // Use this for initialization
         void Start ()
         {
                 //localscale is used because of NGUI Camera
                 topWall.transform.localScale = new Vector3 (Screen.width * 5, 30f, 70f);
                 topWall.transform.localPosition = new Vector3 (0f, Screen.height / 2f + 15f, 0f);  //+15 is done because of  the width of collider
         
                 botWall.transform.localScale = new Vector3 (Screen.width * 5, 30f, 70f);
                 botWall.transform.localPosition = new Vector3 (0f, -Screen.height / 2f - 15f, 0f);
         
                 rightWall.transform.localScale = new Vector3 (30f, Screen.height * 5, 70f);
                 rightWall.transform.localPosition = new Vector3 (-Screen.width / 2f - 15f, 0f, 0f);
         
                 leftWall.transform.localScale = new Vector3 (30f, Screen.height * 5, 70f);
                 leftWall.transform.localPosition = new Vector3 (Screen.width / 2f + 15f, 0f, 0f); 
 
         }


If your game is portrait Then you might want colliders at the right and left side colliders ..Thats wat m assuming

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 Noob_Vulcan · Jun 10, 2014 at 06:14 AM 0
Share

Convert The colldiers to BoxCollider2D

avatar image nathanvs · Jun 11, 2014 at 12:53 AM 0
Share

So what would I attach the code to? And, do I have to make box colliders as well?

avatar image Noob_Vulcan · Jun 13, 2014 at 04:20 AM 0
Share

no u dont have to make colliders , this code will do it automatically

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

23 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

Related Questions

Multiple Cars not working 1 Answer

How to make a gameObject move from it's current position in a different direction after a set amount of time 1 Answer

Problems In Flocking Algorithm 1 Answer

My controls are inverted when facing sideways. 1 Answer

Setting Scroll View Width GUILayout 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