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 Damo12345 · Aug 21, 2014 at 06:17 PM · c#mathf.clampboundary

NEED HELP SETTING BOUNDERYS !!

Hi, I am very VERY new to c# programming, I almost know nothing at all. I am creating a game called 'breakout' And i have been able to manage to get the paddle moving left and right but i cant seem to set a boundry. I have watched many youtube videos and searching the web has turned up nothing. If someone out there can point me in the right direction or even create the code for me that would be awesome ! Here is my code

 /// <summary>
 /// Paddle.cs ALWAYS AT 0 ON Z AXIS ALWAYS ON Y POSTION -6.5
 /// 
 /// </summary>
 
 using UnityEngine;
 using System.Collections;
 
 public class Paddle : MonoBehaviour {
     public static float zPosition = 0;
     public float yPosition = -6.5;
     public float speed = 9;
     public float xBoundryL = -8.3;
     public float xBoundryR = 8.7;
     private Transform _t;
 
     void Awake() {
         _t = transform;
     }
 
     //start
     void Start () {
             _t.position = new Vector3( 0, yPosition, zPosition);
     }
     //update is once per frame
     void Update () { 
         //player input
         if (Input.GetAxis ("Paddle") != 0) {
 
 
             //moving paddle according to Xaxis
             _t.position = new Vector3( _t.position.x + Input.GetAxis( "Paddle" ) * speed * Time.deltaTime, yPosition, zPosition );
 
             
             //place boundry code here (got not clue lol)
 
             _t.position.x = new Vector3(Mathf.Clamp (xBoundryL, xBoundryR, 0));
 
                                              
             }
         }
     }
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 Landern · Aug 21, 2014 at 06:17 PM 0
Share

You can't modify a struct(vector3) in c# this way:

 _t.position.x = new Vector3($$anonymous$$athf.Clamp (xBoundryL, xBoundryR, 0));

Plus the constructor of Vector3 expects more than a float.

 //_t.position.x = new Vector3($$anonymous$$athf.Clamp (xBoundryL, xBoundryR, 0));
     
 Vector3 pos = _t.position;
 pos.x = $$anonymous$$athf.Clamp (Time.time, xBoundryL, xBoundryR);
 
 _t.position = pos;

That is proper.

avatar image Damo12345 · Aug 21, 2014 at 09:55 PM 0
Share

Thanks Landern, But now the paddle flickers towards the positive point. (8.7) Here is my code:

 /// <summary>
 /// Paddle.cs ALWAYS AT 0 ON Z AXIS ALWAYS ON Y POSTION -6.5
 /// 
 /// </summary>
 
 using UnityEngine;
 using System.Collections;
 
 public class Paddle : $$anonymous$$onoBehaviour {
     public static float zPosition = 0;
     public float yPosition = -6.5f;
     public float speed = 9f;
     public float xBoundryL = -8.3f;
     public float xBoundryR = 8.7f;
     private Transform _t;
 
     void Awake() {
         _t = transform;
     }
 
     //start
     void Start () {
             _t.position = new Vector3( 0, yPosition, zPosition);
     }
     //update is once per frame
     void Update () { 
         //player input
         if (Input.GetAxis ("Paddle") != 0) {
 
             Vector3 pos = _t.position;
             pos.x = $$anonymous$$athf.Clamp ( Time.time, -8.3f, 8.7f );
             _t.position = pos;
 
 
             //moving paddle according to Xaxis
             _t.position = new Vector3( _t.position.x + Input.GetAxis( "Paddle" ) * speed * Time.deltaTime, yPosition, zPosition );
 
             
             //place boundry code here (got not clue lol)
 
                                              
             }
         }
     }

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Little problem with Mathf.Clamp as used in Space Shooter Tutorial 8 Answers

Mathf.Clamp Negative min not negative? 1 Answer

Help clamping player movement boundaries 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