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 /
avatar image
0
Question by ijustincabral · Aug 15, 2018 at 03:23 PM · c#2d-platformerprogramming2d-physics2d-gameplay

Transform.position assign attempt not valid, and position is infinity on an object?

I'm working on a game mechanic where to objects move with the player with one being normal, and the other being the opposite. I have the normal part functioning fine, but when I tried to do the opposite in code it's giving me this weird error as my object flies off the screen.

"transform.position assign attempt for "Blue Box" is not valid. Input position is {-Infinity, -28888778970870709809, 454534534534534534543}"

Any idea what is going wrong? Is my opposite movement code wrong?

 if (moving)
         {
             if (isRed)
             {
                 targetPosition = player.transform.position + startPosition; // Red Box (Normal) **These two lines work fine**
                 transform.position += (targetPosition - transform.position);
             }
             else
             {
                 targetPosition = player.transform.position + startPosition; // Blue Box (Opposite) ** These two lines send object flying off screen with error **
                 transform.position += (targetPosition + transform.position);
             
             }
         }
         else
         {
             startPosition = transform.position - player.transform.position; // Resting
             transform.position = player.transform.position + startPosition;
         }
 
     } 
Comment
Add comment · Show 7
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 RShields · Aug 15, 2018 at 03:40 PM 0
Share

That code is equivalent to

 if (moving)
 {
     if (isRed)
     {
         targetPosition = player.transform.position + startPosition; // Red Box (Normal)
         transform.position = targetPosition - transform.position + transform.position;
     }
     else
     {
         targetPosition = player.transform.position + startPosition; // Blue Box (Opposite)
         transform.position = targetPosition + transform.position + transform.position;
     
     }
 }
 else
 {
     startPosition = transform.position - player.transform.position; // Resting
     transform.position = player.transform.position + startPosition;
 }

Note that you're adding the transform.position to itself.

Try switching the += to =

avatar image ijustincabral RShields · Aug 15, 2018 at 08:36 PM 0
Share

That doesn't seem to work. All that happens after that is the blue square starts flashing quickly in two different places like it wants to be in both places at the same time. And the direction I walk doesn't make the square go in the opposite direction.

Anyone else know how I can achieve this?

avatar image RShields ijustincabral · Aug 15, 2018 at 09:36 PM 0
Share

I think the bigger problem is that you're not sure how any of this works.

If I'm reading right, I can give you that that the correct code is

 if (moving)
 {
     if (isRed)
     {
         targetPosition = player.transform.position + startPosition; // Red Box (Normal)
     }
     else
     {
         targetPosition = player.transform.position - startPosition; // Blue Box (Opposite)
     }
     transform.position = targetPosition;
 }
 else
 {
     // Do nothing
     // Ins$$anonymous$$d, set startPosition somewhere else.
 }

but you need to figure out how that works

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Ermiq · Aug 15, 2018 at 09:51 PM

The problem is you're trying to set a direction vector targetPosition - transform.position as a position vector. Vectors in Unity can represent several things. They can be a position point in 3D space, and they can be a direction pointing to a specific direction from some position in 3D space. When you + or - two position vectors you'll get a direction vector. targetPosition + transform.position returns direction from targetPosition to transform.position. targetPosition - transform.position returns direction from transform.position to targetPosition. So, in your code for isRed Unity is trying to set cube position to its current position and then move it in the direction from its current position to target position. It seems like it works as intended, but it's not actually. Make cube's original position farther from the target and you'll see the cube jittering between these two positions. In second part for !isRed you place the cube on the position and set a direction which points 'from' the target, so the cube is going away from the scene. To make it work, you need to determine a distance which you want your cubes to be from the target and use something like this:

 float distance = 1f;

  if (moving)
  {
      if (isRed)
      {
          // posiition for a cube which is going to be in 1 unit in front of player
          // player's position plus a direction vector forward from player multiplied by a distance
          targetPosition = player.transform.position + player.transform.forward * distance;
          transform.position = targetPosition;
      }
      else
      {
          // the same for another cube but a direction should be back from player
          targetPosition = player.transform.position + player.transform.back * distance;
          // or if there's no back vector
          // targetPosition = player.transform.position + player.transform.forward * -distance;
          // or another way
          // targetPosition = player.transform.position - player.transform.forward * distance;
          transform.position = targetPosition;
      }
  }
Comment
Add comment · Show 9 · 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 ijustincabral · Aug 15, 2018 at 10:03 PM 0
Share

I'm getting an error in the !isRed section which is saying there is no .back definition for back on Transform. Tried putting .forward with - front but it just caused the cubes to crash into each other.

I also I commented out the else{} just to test the red square and no matter what it starts it right inside the player as soon as I begin to move. I even increased the distance with no luck.

avatar image Ermiq ijustincabral · Aug 15, 2018 at 10:33 PM 0
Share

Is it a 2D? Oh,yes< I see now. For 2D your forward vector will be player.transform.right (X axis) or player.transform.up(Y axis). Depends on your screen orientation. With using forward cubes just going "into the screen" on Z axis which is not visible in 2d. So, just try right or up and it will work.

avatar image ijustincabral Ermiq · Aug 15, 2018 at 11:03 PM 0
Share

Hey I feel like your code has got me super close, but the only problem is that the blue square does not go in the opposite direction of the player. It moves in lock step with the player like the red square. Here is the code I have adjusted from before that you posted. Thanks for the help by the way, your explanations have made things a whole lot clearer.

 if (moving)
         {
             if (isRed)
             {
                 // posiition for a cube which is going to be in 1 unit in front of player
                 // player's position plus a direction vector forward from player multiplied by a distance
                 targetPosition = player.transform.position + player.transform.right * distance;
                 transform.position = targetPosition;
             }
             else
             {
                 // the same for another cube but a direction should be back from player
                 targetPosition = player.transform.position + player.transform.right * -distance;
                 // or if there's no back vector
                 // targetPosition = player.transform.position + player.transform.forward * -distance;
                 // or another way
                 // targetPosition = player.transform.position - player.transform.forward * distance;
                 transform.position = targetPosition;
             }
 
         }

Only major changes were .forward and .back to .right and -distance in the else{} clause. Any idea on what to do for the blue square to go in the opposite direction of the player?

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

545 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 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 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 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 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 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 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 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 avatar image

Related Questions

Multiple Cars not working 1 Answer

How to store remaining durability of tiles? Tilemap 0 Answers

Bumper physics not working, 1 Answer

Hello Im looking for help creating a script for spike or fire damage! 1 Answer

How do I give my 2D ball a real ball behavior? 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