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;
}
}
}
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612144630im_/https://answers.unity.com/themes/thub/images/avi.jpg)