Hi
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
-
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)
-
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)
EDIT 1:
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;
gameObject.transform.SetParent(RotateHelper.gameObject.transform);
Vector3 distance = otherCenterConnector.transform.position - centerConnector.transform.position;
transform.position+= distance;