- Home /
How to go about having two seperate Game Objects follow eachother?
So I have player Game Object, and a "beam" Game Object. The player is the white square in the bottom right, half covered by the "beam". The "beam is the large blue rectangle.
Upon moving your mouse around the screen, the "beam" will spin around the player and point toward your mouse.
The player cannot move through those trees, but the beam can. Because I put them on separate layers, so the player and tree layer can collide, but the beam and tree layer cannot collide.
This beam will follow the player around with this code
this.GetComponent<Rigidbody2D>().velocity = owner.GetComponent<Rigidbody2D>().velocity;
"owner" is the player object.
Both player and beam have their own rigidbodies and colliders. They are not a parent or child of eachother.
So the problem is. If our player here holds "w"(up) and runs into the trees, and can no longer move. The beam will keep going up. Or two players both have beams extending from them and walk toward each other. The beams will stop and collide(as they should), but the players will keep moving toward each other, passing through the beams( I do not want this to happen).
I cannot find a solution that lets the beam follow the player while abiding by the players movement, and opposite, the player abides by the beams movement. Basically, if the beam cant move, nor should the player, if the player cant move, nor should the beam.
Any ideas on how I go about coding/making this interaction would be very helpful. Thanks
Answer by jihadkhawaja · Jul 27, 2021 at 11:10 PM
Hey there @safetyfirststudios,
There is multiple solution for your issue, I'll suggest one of them:
Add the beam as a child for the player then replace your code
this.GetComponent<Rigidbody2D>().velocity = owner.GetComponent<Rigidbody2D>().velocity;
with
Vector3 mousePos = Input.mousePosition;
Vector3 mousePosWorld = Camera.main.ScreenToWorldPoint(mousePos);
//beam transform
//Vector3.left or Vector3.up depends on ur local transform beam direction
transform.LookAt(mousePosWorld, Vector3.left);
Now the beam will always rotate towards the mouse without moving when the player collide.
So I made the beam a child of the player object, but the beam still isnt following the player. Could it be because the beam has its own rigidbody and colldier? Also when I was trying to post this question yesterday, the website kept crashing and I didnt think it ever got posted, so thats why there are so many duplicates lol, i feel like a fool
No problem, i thought ur a troll or a bot spam.
Anyways, did u made make the beam all rigidbody constraints checked on the X, Y, Z in the inspector?
if it didn't work u can add after transform.LookAt(mousePosWorld, Vector3.left); this transform.position = owner.position; //if you want to add offset you can do so by using this instead transform.position = owner.position + Vector.forward * 2);
So I have all the constraints check, but now when I press W(up) or S(down), the beam will fly off the screen either up or down, depending on the button I press. Does the transform.Translate give it some kind of insane velocity?
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
isKinematic object doesnt collide with wall 3 Answers
Distribute terrain in zones 3 Answers
Enemy shouldn't walk with the player 2 Answers