Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 tomowale · Aug 11, 2015 at 06:45 PM · c#unity 5gameclamp

Clamps won't work!

I have 2 problems. I am trying to create a endless runner type game and I decided I needed to clamp the distance the player can move up , down , left and right on the road, so I made some code for clamping the left and right sides of the road , but the player only seems to acknowledge the clamp on the left side , and I tried to clamp how much the player can go up and down the road but it doesn't work. Please help.

alt text

Here is my code for the right and left clamp.

 void Update () {
         
         //moves left and right along x axis
         transform.Translate(Vector3.right * Time.deltaTime * Input.GetAxis("Horizontal")*movespeed);
         Vector3 tmpPos = transform.position;
         tmpPos.x = Mathf.Clamp(tmpPos.x, -8.0f, 9.0f);
         transform.position = tmpPos;    }
 }



Code for Up and down clamp that won't work.

 void Update () {
         Vector3 tmpPos = transform.position;
         tmpPos.y = Mathf.Clamp(tmpPos.y, -5.0f, 5.0f);
         transform.position = tmpPos;    }
 
     }
 


clamp.png (265.2 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
2

Answer by YoungDeveloper · Aug 11, 2015 at 06:47 PM

You should merge the two scripts.

  1. Cache the vector3 of the transform position

  2. Check for input (left,right,up,down,..) and apply for cached vector3

  3. clamp cached vector3

  4. apply cached vector 3 to transform position

Comment
Add comment · Show 6 · 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 ThePunisher · Aug 11, 2015 at 07:00 PM 0
Share

Nice Job. And yeah, the two scripts should be merged. No reason to have two separate scripts modifying a single objects position. That's when you start running into issues.

avatar image tomowale · Aug 11, 2015 at 07:19 PM 0
Share

Thanks guys. But I'm a bit confused YoungDeveloper are you basically saying I should keep the code how it is, merge the two scripts ,but just check for input?

avatar image YoungDeveloper · Aug 11, 2015 at 07:21 PM 1
Share

You should do it in order I wrote, your logic will not encounter any problems.

avatar image tomowale · Aug 11, 2015 at 07:44 PM 0
Share

Your wording is confusing me a bit. So Basically what you are trying to say is I should do something like this......( I am confused , so can you explain a bit better ( in depth) , I am also confused with the merging scripts)

 Vector3 tmpPos = transform.position;//cached Vector 3
 
         if (Input.GetAxis ("Horizontal"))// gave me some errors but I am just asking if this is what you are trying to get me to do
         tmpPos.x = $$anonymous$$athf.Clamp(tmpPos.x, -8.0f, 9.0f);// clamp cached Vector 3?
         transform.position = tmpPos;    // apply cached Vector 3 to transform position  }
 }
avatar image YoungDeveloper · Aug 12, 2015 at 06:54 AM 1
Share

$$anonymous$$y wording is confusing? $$anonymous$$aybe you should reread what I wrote more than 1 time. I wrote a script specifically for you. It does exactly what I wrote.

 using UnityEngine;
 
 public class Basic : $$anonymous$$onoBehaviour {
 
     private float _speedForward = 2f;
     private float _speedSide = 2f;
     private float _clampLeft = -1f;
     private float _clampRight = 1f;
     private Vector3 _cache;
 
     private void Update (){
         _cache = transform.position;
 
         //FORWARD
         _cache +=   Vector3.forward * _speedForward * Time.deltaTime;
         //SIDES
         if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)) _cache += Vector3.left * _speedSide * Time.deltaTime;
         if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)) _cache += Vector3.right * _speedSide * Time.deltaTime;
         //CLA$$anonymous$$P SIDES
         _cache.x = $$anonymous$$athf.Clamp(_cache.x, _clampLeft, _clampRight);
 
         transform.position = _cache;
     }
 }
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

how to make the sprite and the background color in a tile disappear? 0 Answers

Need help in typing project 0 Answers

LineRenderer static movement 1 Answer

(Game Build) Validating files and updating to latest version 0 Answers

Timer doesn't count 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