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 anilgautam · Dec 13, 2012 at 12:37 PM · cameratransformtranslateeffects

How To Limit A transform MOVEMENT in X axis

I am currently working on a game where Camera movement is around X axis only like angory birds. I am applying transform.translate to move the camera.

My problem is that

minoffset //minimum value of camera x position maxoffset //maximum value of camera x postion I using following code

`if (transform.position.x >= MinOffset && transform.position.x <= MaxOffset) {

do the movement stuff

}`

when I use transform. translate using touch The position of camera moves outside the offset values .and the code written in if satement never executes.

I have also tried with two another conditions to check is x position goes beyond the offset value. Reset its position to maximum value. But It produces a jerk .Please Help

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by criptoonita · Dec 19, 2012 at 10:37 PM

Maybe clamp would be more elegant to make limits transform.position = Vector3(Mathf.Clamp(pObject.x, min, max), pObject.y, pObject.z);

http://docs.unity3d.com/Documentation/ScriptReference/Mathf.Clamp.html

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 Zoomerland · Dec 26, 2013 at 02:53 PM 0
Share

Thanks for that answer! Realy what I need.

avatar image Christin2015 · Mar 16, 2017 at 09:10 PM 0
Share

Thanks a million. Just what I needed.

avatar image
0

Answer by CodeMasterMike · Dec 13, 2012 at 01:58 PM

I think that it jerks because you translate it, check if its out of limits, and if it is out of limits, you move it back to within the limits the next frame.

Better way to do it, is to check the final position, and if that is within your boundaries, then do the translate. Something like this pseudo code:

 private Vector3 translateVector = new Vector3(10.0f, 0.0f, 0.0f);
 
 update()
 {
     Vector3 newPosition = PlayerObject.Transform.Position + translateVector;
     
     if(newPosition.x >= MinOffset && newPosition.x <= MaxOffset)
     {
         PlayerObject.Transform.Translate(translateVector);
     }
 }

Good luck!

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 CodeMasterMike · Dec 17, 2012 at 11:36 AM 1
Share

Thats odd. Can you post the code where you do the translation together with the offset check?

avatar image anilgautam · Dec 17, 2012 at 11:56 AM 0
Share

Actually according to you, fist I need to store my new Position in another variable

And then 1st check the new position is less then offset. But I have done my movement stuff inside my Condition. ( if (transform.position.x >= $$anonymous$$inOffset && transform.position.x <= $$anonymous$$axOffset) {})..

But still I like your idea .I will try to change my conditions according to you. Thanks :)

avatar image CodeMasterMike · Dec 17, 2012 at 11:56 AM 0
Share

Yes, that is a part of it. But where and how do you set the translation vector? The code above only shows the $$anonymous$$/max check, and nothing more.

It's impossible to see if something is wrong if we can't see the code the problem is about. In my example above, you can see how and where I check the new position and how and where I set it, which makes it possible to see if there is anything that's wrong or should be changed.

avatar image anilgautam · Dec 17, 2012 at 11:58 AM 0
Share

thanks I will try this and get back to you. :)

avatar image anilgautam · Dec 19, 2012 at 02:06 PM 0
Share

Thats perfect I have tried with this float newPosition = myPositionX - (swipe$$anonymous$$ovement *2); if(newPosition > CameraZoomInX$$anonymous$$inPosition && newPosition < CameraZoomInX$$anonymous$$axPosition) { myCameraTransfrom.Translate(-swipe$$anonymous$$ovement *2, 0, 0, Space.Self); }

There is one more problem I am facing is that if I am doing the scroll thing more faster the camera again some times moves outside the $$anonymous$$inOffet /$$anonymous$$axOfset.

Please provide me a more better approach so that it can be strictly restricted to the boundaries..

Show more comments
avatar image
0

Answer by iuripujol · Nov 23, 2017 at 12:36 PM

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Move : MonoBehaviour { public Transform cube; public float dist2; Vector3 initPos; // Use this for initialization void Start () { initPos = transform.position; dist2 = cube.lossyScale.x / 2; }

 // Update is called once per frame
 void Update () {
     float h = Input.GetAxis ("Horizontal");
     float s = 0;
     if (h != 0) {
         s = h > 0 ? 1 : -1;
     }
     Vector3 newPos = transform.position + cube.right * s;

     if(Mathf.Abs (newPos.x) <= dist2)
         transform.Translate (cube.right *s * 0.08f);    
 }

}

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

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

14 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

Related Questions

How to translate a camera forward and back(basically a zoom) according to the distance of two objects? 1 Answer

Change camera position 1 Answer

Camera transform not responding after load 0 Answers

How do i lock the position of the camera above the player relative to the origin point? 0 Answers

move another object when 2 other objects collide 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