- Home /
How Do I Give Two Objects the Same Transform?
Hello, I'm trying to make a modular vehicle and I've had some troubles. Every time I load the game, the modules fly apart. I created my modules and added an anchor point. At the anchor points, I got the parent (Module) and set the position with an offset and the rotation with an offset. I think the problem is that the values keep getting added, but I want the vehicle to stick together in-game.
using System.Collections; using UnityEngine;
public class AnchorScript : MonoBehaviour {
public bool inGame = false;
private Collider[] nearestAnchor;
public Vector3 offset;
private Vector3 calculatedOffset;
private Quaternion rotation;
public Quaternion rotOffset;
private Quaternion calculatedRotOffset;
void Update () {
rotation = transform.rotation;
nearestAnchor = Physics.OverlapSphere(transform.position, 1f);
foreach (Collider collider in nearestAnchor)
if (collider.gameObject.GetComponent<AnchorScript>() != null) {
calculatedOffset = transform.position + offset + collider.gameObject.GetComponent<AnchorScript>().offset;
calculatedRotOffset = new Quaternion(rotOffset.x + rotation.x, rotOffset.y + rotation.y, rotOffset.z + rotation.z, rotOffset.w + rotation.w); //XYZW
collider.gameObject.transform.parent.transform.position = calculatedOffset;
collider.gameObject.transform.parent.transform.rotation = calculatedRotOffset;
}
}
}
If anyone can fix this or has a better way, that would be greatly appreciated.
Answer by TonicMind · Nov 20, 2018 at 12:26 AM
I think adding the position to itself every update is causing this. Try using transform.localPosition, and don't add the position back into the offset calculation.
Thanks for the help! I did a LOT of thinking and I changed my code quite a bit. I compressed it and it worked a LOT better! There is just one more problem. With my new code, when I rotate the main anchor, the other modules move with it, but they lag behind a bit. If I turn it too much (about 5 degrees on the Y), the anchors lose connection and the system quits working. If you could help me further (sorry), here's the new code:
using System.Collections; using UnityEngine;
public class AnchorScript : $$anonymous$$onoBehaviour {
public bool inGame = false;
private Collider[] nearestAnchor;
public Vector3 offset;
void Update () {
nearestAnchor = Physics.OverlapSphere(transform.position, 0.1f);
foreach (Collider collider in nearestAnchor) {
if (collider.gameObject.transform.name == "Anchor" || collider.gameObject.transform.name == "Anchor (F)" || collider.gameObject.transform.name == "Anchor (B)") {
if (collider.gameObject != this.gameObject) {
collider.gameObject.transform.parent.transform.position = transform.position + offset;
collider.gameObject.transform.parent.transform.rotation = transform.rotation;
}
}
}
}
}
Your answer
Follow this Question
Related Questions
Rotate Parent using Child as Pivot 0 Answers
How to restrict rotation 2 Answers
Rotate object around object with fixed value. 1 Answer
Possible way to improve performance moving large hierarchies? 1 Answer