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 alexwolf737 · Dec 15, 2017 at 10:45 AM · 2dsprites

Sprite not aligned with transform,Sprite becoming distanced from children/box collider/transform?

In my 2D project, I just have a little boat that moves to where you click in a boat-like fashion.

However, as it gets farther from the center of the screen the main sprite of the boat's RigidBody2D seems to move ahead of the transform slightly, causing the children GameObject sprites appear to lag behind the main object. Also, it causes the box collider to be behind the main sprite.

Is there any reason a sprite can become distanced from its gameobject? If so how can it be realigned?

Some code from my "Ship" class:

 void FixedUpdate() {
         if (ship.rotation > 180) {
             ship.rotation -= 360;
         } else if (ship.rotation < -180) {
             ship.rotation += 360;
         }
         
         if (destination != ship.position) {
             move();
         }
     }
     
     private void move() {
         if (ship.position != destination) {
             rotate();
             thrust();
             //trail();
         }
     }
     
     private void rotate() {
         Vector2 destinationDiff = destination - ship.position; // Destination in relation to ship
         destinationAngle = Vector2.SignedAngle(Vector2.up, destinationDiff);
         
         float heading = destinationAngle - ship.rotation;
         
         // The flip from -180 to +180 flips the correct turn direction, below block fixes
         if (Mathf.Abs(heading) > 180) { // It crosses over -180 -> +180 flip
             heading *= -1; // Turn against the expected heading
         }
 
         // If super close to correct angle, just snap to correct angle
         if (ship.angularVelocity * heading < 0 && Mathf.Abs(heading) < 1.5) {
             ship.rotation = destinationAngle;
             ship.angularVelocity = 0;
         
         } else if (heading < 0) {
             // Turn negative (clockwise)
             ship.angularVelocity = -maxAngSpeed * (ship.velocity.magnitude / maxSpeed);
         } else if (heading > 0) {
             // Turn positive (counterclockwise)
             ship.angularVelocity = maxAngSpeed * (ship.velocity.magnitude / maxSpeed);;
         } else {
             ship.angularVelocity = 0;
         }
     }
     
     private void thrust() {
         // Calculate our rotationVector
         rotationVector.x = -Mathf.Sin(ship.rotation * Mathf.Deg2Rad);
         rotationVector.y = Mathf.Cos(ship.rotation * Mathf.Deg2Rad);
         rotationVector.Normalize();
         
         Vector2 destinationDiff = destination - ship.position; // Destination in relation to ship
         float distance = destinationDiff.magnitude;
         
         // If extremely close to destination, snap to destination and stop all motion
         if (distance < .01) {
             isThrust = false;
             ship.position = destination;
             ship.velocity = Vector2.zero;
             ship.angularVelocity = 0;
         
         // If close to destination, slow movement
         } else if (distance < 2) {
             isThrust = (distance > 1); // If distance is greater than one, thrust is on
             ship.velocity = ((.75f/2 * distance + .25f) * maxSpeed) * rotationVector;
 
         } else {
             // Move forward
             isThrust = true;
             if (ship.velocity.magnitude < maxSpeed) {
                 Vector2 newVelocity = ship.velocity.magnitude * rotationVector + (maxSpeed / accelFrames) * rotationVector;
                 if (newVelocity.magnitude > maxSpeed) {
                     newVelocity = maxSpeed * rotationVector;
                 }
                 ship.velocity = newVelocity;
             } else {
                 Vector2 newVelocity = maxSpeed * rotationVector;
                 ship.velocity = newVelocity;
             }
         }
     }
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 alexwolf737 · Dec 18, 2017 at 03:15 AM

Problem self-resolved! Turns out it was the trail that was the problem, not the boat itself. It had a different Z coordinate to place it under the boat, I did not realize this would have other consequences.

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

187 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

Related Questions

Unity 2d - Coordinate grid is way to big? 1 Answer

Unity 2D Sprite artifact/bug 1 Answer

Sprites showing through each other in 3D 0 Answers

How do I render Particlesystems in front of 2D sprites? 2 Answers

Trying to simulate SNES party snake, non-grid. 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