Moving and rotating object to specific point (3D)


I hope there are some great people here who can help give me some tips on how to reach my goal

Right now in my game I have a RailroadTrack prefab, where I have placed two tracks on my scene as you can see on the screenshot below. On this prefab I have 6 gameobjects called connectors.

  • Two blue connectors in the center (these are rigidbody and has a sphere collider with a trigger). I’m using this one to test when I’m moving (with my mouse) a track near another track
  • Four yellow connectors (one at each end of the iron-part of the track). My vision is that I’ll use these to figure out how to rotate the track

Right now I’m seeing two challenges that I’m unsure how to handle

  1. When one of the blue connectors is near another blue connector, I need the position of the blue connector of the other track to be the position where I’ll be moving my track to (so the two blue connectors are in the exact same position. Obviously if I’m just setting the transform.position of track I’m moving to the blue connector on the other track, it’s not placed correctly. I’m thinking I might need to use the distance I’m getting from subtracting the position of one of the connectors from the other, but I’m not sure how (also taking into consideration that I could connect the track on both sides and the tracks can be in weird angles)

  2. When the track I’m dragging around has moved to the other tracks connector, I need to calculate how much it needs to rotate to have the same angle as the other track. Currently I have no idea how to calculate this, so I have created a way that it will rotate 1 degree at a time until the yellow connectors hits each other (unfortunately due to challenge 1, I can’t test if this actually works yet, but this is probably still a very bad way of doing it)

I found the solution for challenge 1 by using the below code when I have the two center connectors that is touching:

RotateHelper.transform.position = otherCenterConnector.transform.position;
Vector3 distance =  otherCenterConnector.transform.position - centerConnector.transform.position;
transform.position+= distance;

After playing around with distance I noticed something weird

I have a method that receives both blue center connectors when they are inside trigger area. I then added the following code to see their locations

Debug.Log("This: " + centerConnector.transform.position.y);
Debug.Log("Other: " + otherCenterConnector.transform.position.y);

This gives me the following log when they are as close to each as I can do by moving the item with my mouse

I did not expect these results at all from position.y. Z and X positions are ok as far as I can see, but the Y is far away. Now my project looks like this. The GhostRailStraight is the one I’m moving around with ConnectTop touch ConnectBottom of TurnedRail. As you can see they are all on the same level and the Y position is set to 0 for both TurnedRail and GhostRailStraight and also for all their sub items (like CenterConnectors, ConnectTop and ConnectBottom)

Where does this -5.9 come from if everything is set to 0? From my understanding transform.position should give the world position. I can see in my sceneview that both center connectors are located at the same place, no matter from where I’m looking at it



Seems like a Unity problem. I created a new object with exactly the same settings, then deleted the old object. Now it works… Weird