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 sn24 · Jun 21, 2017 at 07:41 AM · collider2dtransform.position

Stop transform.position by collider

Hi all, Is there any way to stop an object from going further by a collider even if the script makes its position update every frame to increase? Like this:

transform.position = new Vector2(100, transform.position.y); How to stop it from going to 100 if there is a collider in between?

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
1

Answer by FlaSh-G · Jun 21, 2017 at 10:58 AM

Give your GameObject a Rigidbody and perform a SweepTest.

 var rb = GetComponent<Rigidbody>();
 
 var distance = 100f;
 RaycastHit hit;
 if (rb.SweepTest(direction, out hit, distance))
 {
   distance = hit.distance;
 }
 transform.Translate(direction * distance);

As you can see, you need to specify a direction rather than a target position, as your collision test is based on going a direction rather than arriving at a position ("if there is a collider inbetween").

If needed, you can calculate the direction by subtracting the current position from the targetPosition:

 var direction = targetPosition - transform.position;

By the way, if your object does not have a collider but rather is a point, replace the SweepTest with a regular Raycast and don't add a Rigidbody.

Comment
Add comment · Show 5 · 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 sn24 · Jun 23, 2017 at 07:45 AM 0
Share

Well I guess I can't do it without specifying a position because here is a script:

void FixedUpdate () { if (!is$$anonymous$$ovable) return; float pos = Input.mousePosition.x; if (pos > maxX) pos = maxX; if (pos < $$anonymous$$X) pos = $$anonymous$$X; transform.position = new Vector2 (pos, transform.position.y); }

This script is on the object which has to move. Right now I am using the maximum and $$anonymous$$imum X value as maxX and $$anonymous$$X respectively but I would like to use colliders to limit it because the values may change.

avatar image FlaSh-G · Jun 23, 2017 at 07:47 AM 0
Share

It seems like you didn't understand my answer.

If needed, you can calculate the direction by subtracting the current position from the targetPosition

avatar image sn24 · Jun 23, 2017 at 07:48 AM 0
Share

The size of the object may also change

avatar image sn24 · Jun 23, 2017 at 08:29 AM 0
Share

The object has a Rigidbody2D which does not have SweepTest. Please help me I am in a fix.

avatar image FlaSh-G · Jun 23, 2017 at 10:46 AM 0
Share

In 2D, this might be quite a challenge. If you don't need to be 100% exact, you could use a CircleCast ins$$anonymous$$d.

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

68 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

Related Questions

Colliders not working 2 Answers

Moving an object to a random position between two objects 1 Answer

Bug - Able to pass through rigidbody/collider when using Lerp/SetPosition 0 Answers

OnTriggerEnter2D calculate wrong collision when Object is scalling 0 Answers

Can you get polygon collider 2d shape from a sprite? 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