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 udaanparvaz · Jul 23, 2012 at 04:31 PM · positionlimit

how to record the Vector3 position on condition?

I have implemented a touch and drag on GameObject on Iphone/Android. I am using if statements and "OR" gates for detecting gameObject.transform.position for each axis and when any condition is met then i am setting the gameObject transform position to Vector3.zero.

But what i want is to avoid the gameObject from crossing the Boundary Conditions. I don't want to use localPosition since the Object needs to remain independent in hierarchy.

the code that I am using is :

 var object : GameObject;



function LateUpdate()

{

if (Input.GetKeyDown(KeyCode.Escape)) { Application.Quit(); }

var theTouch : Touch = Input.GetTouch (0);

var ray = Camera.main.ScreenPointToRay(theTouch.position);

var hit : RaycastHit;

if (Physics.Raycast (ray))

{

gameObject.animation.Play();

dragmethod();

}

}

function dragmethod() { //var targetlimit =imageTarget.transform.position; //var target = object.transform.position; //var limit = new Vector3(0,0,0);

for (var touch : Touch in Input.touches)

{

if(touch.phase == TouchPhase.Moved && Input.touchCount==1)

{

var cameraTransform = Camera.main.transform.InverseTransformPoint(0, 0, 0); var touchPosition = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, cameraTransform.z )); var origin : Vector3;

object.transform.position = touchPosition; origin = Vector3 (0,0,0);

if(object.transform.position.x > 0.50 ||object.transform.position.x < -0.50 ||object.transform.position.y > 0.50 || object.transform.position.y < -0.50 || object.transform.position.z > 0.50 || object.transform.position.z < -0.50)

{ object.transform.position = new Vector3 (0,0,0);

} } } }

Is there any way to limit the gameObject inside a "Virtual Boundary" without making it a child of any other Object?

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 fafase · Jul 23, 2012 at 04:34 PM

Somehow maybe:

 if(transform.position.x < minX)transform.position.x=minX;
 else if(transform.position.x > maxX)transform.position.X=maxX;
 
 if(transform.position.y < minY)transform.position.y=minY;
 else if(transform.position.y > maxX)transform.position.y=maxY;

 if(transform.position.z < minZ)transform.position.z =minZ;
 else if(transform.position.z > maxZ)transform.position.z =maxZ;
 

If your position exceeds the boudaries, the position is the bounadaries.

Comment
Add comment · Show 3 · 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 udaanparvaz · Jul 24, 2012 at 04:45 AM 0
Share

In my code the transform.position is following the touchPosition // var touchPosition = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, cameraTransform.z ))

Thus the gameObject is following a Vector3 value am i right? Your code suggests to place the gameObject at specific values of $$anonymous$$axX $$anonymous$$axY and $$anonymous$$axZ(3 dimensions are must coz I am implementing this in Augmented Reality)...

Sorry if I am wrong but please tolerate my lack of knowledge.Is there any other way,I think I am implementing it the wrong way.

avatar image fafase · Jul 24, 2012 at 09:34 AM 0
Share

ok, you pass the value of the touch to the transform so you have a vector3 for instance (3,11,14). Your object is about to be positioned at this position but in the same frame before rendering you apply:

 if(transform.position.x < $$anonymous$$X)transform.position.x=$$anonymous$$X;
 else if(transform.position.x > maxX)transform.position.x=maxX;

 if(transform.position.y < $$anonymous$$Y)transform.position.y=$$anonymous$$Y;
 else if(transform.position.y > maxX)transform.position.y=maxY;

 if(transform.position.z < $$anonymous$$Z)transform.position.z=$$anonymous$$Y;
 else if(transform.position.y > maxX)transform.position.z=maxY;

Let's say all $$anonymous$$ are 0 all max are 10. As a result your new vector will be (3,10,10) and your object is inbound.

avatar image udaanparvaz · Jul 25, 2012 at 08:01 AM 0
Share

Thank you for the help!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to reach limit line, keep moving, even past over the touch position? 0 Answers

How to reach limit line, keep moving, even past over the touch position? 0 Answers

Limiting Game Object position by rect 1 Answer

Position Limit 2 Answers

How to limit position x between (-10, 10) ? 2 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