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 /
  • Help Room /
avatar image
0
Question by GregPDesUGD · Jul 03, 2016 at 08:44 PM · rigidbodyvector3transform.position

3D Pong Clone: Set position of a Rigidbody, or change how a speed vector is verified?

Hi there!

I am experimenting with mouse input as a way for me to learn more on what Unity can do, as I'm interested in learning on how to build games with it. Simply put, I'm a newbie to video game development.

There's one problem I'm having, and that is if I move my mouse up or down too fast, my player paddle (colored red) in my 3D Pong clone goes out of bounds. By that, I mean, rather than being stuck between two light green walls, as you see below:

Red paddle is between two light green walls

the paddle goes out of the space and thus the player can't bounce the ball back towards the opponent. Red paddle goes out of bounds

I tried to solve this on my own using range limits, overriding the Start function in the Player script (which inherits from the Paddle class, which in turn inherits from MonoBehaviour), but the only thing I really got was getting my red paddle stuck right at the top wall when I move too fast upwards.

Here's the code I got so far for my Player paddle (for brevity, I'm showing you my FixedUpdate function for now. Please let me know if you need to see more):

     // Update is called once per frame
     // Instead of floating-point values, movement should be controlled by a Vector3 object.
     void FixedUpdate () 
     {
         if (GameController.isTheGameOn())
         {
             float moveUpOrDown = 0.0f;
 
             // Get input from Player 1's input on keyboard
             if (typeOfPlayer == PlayerNumber.PLAYER_ONE && typeOfInput == InputType.KEYBOARD)
                 moveUpOrDown = Input.GetAxis("Player 1 Keyboard Input");
             // Get input from Player 2's input on keyboard
             else if (typeOfPlayer == PlayerNumber.PLAYER_TWO && typeOfInput == InputType.KEYBOARD)
                 moveUpOrDown = Input.GetAxis("Player 2 Keyboard Input");
             // Get input from the mouse (up or down)
             else if (typeOfInput == InputType.MOUSE)
                 moveUpOrDown = Input.GetAxis("Mouse Y");
             // Get input from the joystick
             else
                 moveUpOrDown = Input.GetAxis("Vertical Joystick");
 
 
             speedVector = new Vector3(0.0f, moveUpOrDown * Time.deltaTime * 2.5f, 0.0f);
 
 
             /* Problem with the mouse input: The paddle can go out of bounds in the game. */
             // This is where I initially had my boundary conditions checked for the speed vector,
             // and where I introduced the bug that the paddle gets stuck at the top wall and can't 
             // move from mouse input.
             if (speedVector.y + board.transform.position.z >= maxYPosition
                 || speedVector.y - board.transform.position.z <= minYPosition)
             {
                 speedVector = Vector3.zero;
             }

             // Translate is used here instead of setting the position explicitly, primarily 
             // for performance.
             board.transform.Translate(speedVector);
         }
     }


Right now, at optimal performance, the game runs at 50 frames per second. With mouse input, there definitely has to be strict limits such that even a strong flick won't make the paddle go out of bounds. I am thinking of either using board.MovePosition or set the position within my boundary check, or that I research myself and find other ways to limit my speed vector from my input.

I do remember coming across a question somewhere in this forum where someone suggested to use Mathf.Clamp and predict what the next transform position would be, but I can't remember what the question is from the top of my head.

Any suggestions?

cusersgregpdocumentsuniversity-of-waterloo-co-oper.png (37.3 kB)
cusersgregpdocumentsuniversity-of-waterloo-co-oper.png (36.0 kB)
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
0
Best Answer

Answer by KieveKRS · Jul 03, 2016 at 08:55 PM

Mathf.Clamp is exactly what you'd want to use. It's covered in more detail in the "Space Shooter" tutorial, here. Since you've already got your min / max Y positions, the code would look something like:

  Mathf.Clamp (rigidbody.position.y, minYPosition, maxYPosition)

I don't claim this is the exact code to use, but it should help as a starting point. I recommend following along with the linked tutorial, at least that section, since it explains the concepts in greater detail.

Comment
Add comment · Show 2 · 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 GregPDesUGD · Jul 06, 2016 at 12:29 PM 0
Share

Okay, I do have something that works, but I don't know if this is practical in a game coding sense.

This is what I got right now:

 void FixedUpdate () 
     {
         if (GameController.isTheGameOn())
         {
             float moveUpOrDown = 0.0f;
 
             // Get input from Player 1's input on keyboard
             if (typeOfPlayer == PlayerNumber.PLAYER_ONE && typeOfInput == InputType.$$anonymous$$EYBOARD)
                 moveUpOrDown = Input.GetAxis("Player 1 $$anonymous$$eyboard Input");
             // Get input from Player 2's input on keyboard
             else if (typeOfPlayer == PlayerNumber.PLAYER_TWO && typeOfInput == InputType.$$anonymous$$EYBOARD)
                 moveUpOrDown = Input.GetAxis("Player 2 $$anonymous$$eyboard Input");
             // Get input from the mouse (up or down)
             else if (typeOfInput == InputType.$$anonymous$$OUSE)
                 moveUpOrDown = Input.GetAxis("$$anonymous$$ouse Y");
             // Get input from the joystick
             else
                 moveUpOrDown = Input.GetAxis("Vertical Joystick");
 
 
             speedVector = new Vector3(0.0f, moveUpOrDown * Time.deltaTime * 2.8f, 0.0f);
 
 
             board.transform.position = new Vector3(board.transform.position.x,
                 board.transform.position.y, $$anonymous$$athf.Clamp(board.transform.position.z + speedVector.y, $$anonymous$$YPosition, maxYPosition));
         }
     }


Basically, I am binding $$anonymous$$athf.Clamp to what the new paddle's Z position would be in this 3D vector I'm assigning to the board's position, which is kind of similar to board.transform.Translate. I am doing this because I still want Unity's model to control the acceleration on the board.

I am pondering about program$$anonymous$$g this in a different way; don't game development companies care about how I code and what approach I use in a game engine? Otherwise, I remember posting the same problem onto a Facebook group, and one user said to me I should never use the position of a rigidbody's transform in FixedUpdate since FixedUpdate is primarily for physics.

avatar image GregPDesUGD · Jul 06, 2016 at 02:27 PM 0
Share

Let me try this out first, because I want to make a fair bit of progress on my Pong clone before I watch and follow another Unity tutorial. I still feel that there's a lot of work ahead of me as I want to take it on, but I'm taking it slow because I got a university term to deal with at the same time.

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

71 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

Related Questions

Gameobjects transform position minus range 1 Answer

How can I move an object in the direction another object is facing. 1 Answer

How do I make two objects share two of the same transform values? 1 Answer

Bullet shooting not working 0 Answers

how to change the velocity of a rigid body based on a forward 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