• 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 jadaith · Oct 07, 2013 at 06:10 PM · c#scritping

Space shooter problem

I'm creating a space shooter and I'm having a problem with the player moving outside of a zone along the Y-axis. I'm having my camera move so I can not clamp the the y-axis as the player will only stay in a certain area. I've also tried messing with the ontriggerevents where I will set the player speed to 0 but that doesn't work due to ontriggerexit not working. In my level as well I have boxes that are set to triggers so I want to have it when the player reaches those triggers he can not go past that point on the Y-axis. How can I implement this into my game?

Here is my code so far. I took out the trigger events because they are not working the way I wanted them to.

 public float playerSpeed ;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () 
     {
 
 
         float x_direction = Input.GetAxis("Horizontal") ;
         float y_direction = Input.GetAxis("Vertical");
         
         Vector3 direction = new Vector3(x_direction,y_direction,0);
         
         if(direction.magnitude > 1)
         {
             direction.Normalize();
         }
         
         direction *= Time.deltaTime *playerSpeed;
         
         transform.Translate(direction);
         
         float x_clamped = Mathf.Clamp (transform.position.x, -6.2f, 6.2f);
         
         
         transform.position = new Vector3(x_clamped, transform.position.y ,0);
 
 
     }
Comment
Add comment · Show 3
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 jmatthews · Oct 07, 2013 at 10:38 PM 0
Share

I notice you're clamping your .x transform which is working for you. You say you're "having your camera move" so you can't clamp your y axis. Could you explain this part in more detail?

avatar image jadaith · Oct 08, 2013 at 03:27 PM 0
Share

Sorry for the late response. But the clamping the x- axis because there is a set width of the game. But the y is constantly changing due to the camera movement that's why clamping the y did not work for me.

avatar image jmatthews · Oct 10, 2013 at 04:17 AM 0
Share

An easy hack would be to just find the center Y value and then make sure that your player Y value doesn't exceed YCenter + Top or YCenter + Bottom.

Another way would be to have a bounding box at the top and bottom and just move it along as you move your Y values. Then ignore either up or down commands if its colliding with the top or bottom box.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Increase difficulty Space Shooter 2 Answers

Changing light Intensity though script does nothing 1 Answer

Interaction between Unity and Universal Windows 10 apps 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