I wanted to create some evolution like "game". I make some bones, muscles, etc. When I hit play the creature moved but when I've checked the distance it stayed 0. What I've discovered was that all individual child elements moved but the containing GameObject was stil at the same position.
What is the (correct) way to make changes of a child object affect the parent object? Like moving legs to move the person.
The question seemed clear in my head but after reading your questions, remarks and comments I needed elaborate more:
I understand the basic idea of walking animations and such (i think). But here I'm trying to move a creature by itself. At first purely random movement and hopefully later on via a neural network.
In the sample screenshot below you see a creature, the spheres are colored for its weight and the beams between the spheres are the bone/muscle. And my creature limbs do move by pulsating the bone/muscles... but the position of the parent object stay the same. The magenta line is the position of the parent. I would like that the position of the parent would follow the center of its elements.
Your question is a bit unclear to what exactly it is you are after. However, if you want the child-parent behaviour become reveresed in a way that child becomes parent and parent becomes child just for function A() , you can simply swap the child and parents relation in A() itself,
As an example,
void A()
{
child.transform.SetParent(parent.transform.parent);
parent.transform.SetParent(child.transform);
... // your A function
parent.transform.SetParent(child.transform.parent);
child.transform.SetParent(parent.transfotm);
}
Related
Is there a way to have a parent object follow the combined bounds box of the child objects?
I have child objects which I apply force to for movement (via `AddForceAtPosition). The problem is that they leave the parent behind in world space. I understand that children are relative to the parents position. But is there anything I can do to address this?
I'd really like to keep to keep the force being applied to the child object's rigid bodies as it gives me the physics behaviour I need.
I have experimented with getting the bounds of all child objects, then setting the transform of the parent to the children. But this causes jittery behaviour since moving the parent also moves the children.
There are two ways of doing this, the best way is to change the structure following:
A (parent)
B (child 1)
C (child 2)
to:
Root (new parent with nothing on it)
A (old parent)
B (old child 1)
C (old child 2)
Then move A the way you tried before. You might also want to look into Joints, and see if this might suit your need better. If you don't want two colliders to interact with each other, you might also want to look into Layer Based Collision Detection and Physics.IgnoreCollision().
An alternative that I really wouldn't recommend, is counteracting the movement of the parent with the children. So whenever you move the parent you move the local position of the child you want to stay still the opposite amount. This however could possibly make your physics wonky.
I'm trying to scale a UI when mouse is over the UI but in my code, when mouse is over a UI, all the UI elements are scaling and I only to scale the UI that my mouse is over. Here's my code:
public void StartScale(){
foreach (GameObject button in buttons){
LeanTween.scale(button, new Vector2(scaleAmount, scaleAmount), scaleSpeed);
}
}
I have an array of game objects that I want to scale. I want them to scale only when my mouse is over but I can't make it work.
There are two things at play here. One is how you populate your buttons list, no information was provided for us to work with. The other is the transform hierarchy. If you scale an object, anything that is a child will also scale as all transformations are relative to the parent.
In this case my suspicion is that you've accidentally added an object too far up your hierarchy tree to the list, or have made certian GameObjects children of the object you intended to scale.
Hey I'm trying to figure out how I can achieve this effect where
left ball lowers/raises the end of the rectanggle.
and the right ball does the same on the other side.
Here's what it would look like
https://gyazo.com/7d17da64ece3e89c7ac375446c547d1d
I've been messing around this for quite a while but I can't get stable resulsts and I am confused on which hinge would be the best for this use case.
I'd imagine hinge joints but they tend to go crazy and spazz out
I need ball A to raise lower the rectangle over a pivot point above ball B
and vice versa.
There are many ways to achieve this:
1. Group both objects under the same parent and raise the parent instead of individual childs
2.Attach script to the second object that will update its position based on the first object or vice versa
Gameobject objA = GameObject.Find("ObjectA");
transform.position = objA.transform.position;
why is my Gameobject so far away from the Move Tool as you can see in the Picture down below?
I would like to have it centered at the Gameobject. How can I change this?
GameObject away from Move Tool
Screenshot of Hierarchy
Check the object in the scene hierarchy and make sure that the transform eg. x,y,z are set to 0.
If your object is a child of another object and they are not at the same position you are probably moving the parent and not the child object itself.
that Move Tool is the objects origin, and this might help:
"The easiest way is to create an empty object as a parent object. Then you put the corridor object as a child of the empty object. You can then alter the position of the corridor object so its positioned at the edge.
i found your answer here
note that its not exact, in your case, make an empty object, move it to where you would like your origin or move tool THEN add your other object to it as a child. you can then move it with the parents move tool(origin) right on top of your box.
This is a question regarding best-practice for implementation. For an example, I will reference a simple game called Pixel Claw.
Suppose I had an SKSpriteNode akin to the claw from Pixel Claw, in that what the SKSpriteNode might be ''holding'' is variable (but finite in possibilities, e.g. four different objects).
What the node SKSpriteNode is holding has no agency of itself.
Thus my question is: is it better to use different textures such as a claw, a claw holding object a, a claw holding object b, etc or have two SKSpriteNodes and position the SKSpriteNode with the claw texture to be next to the SKSpriteNode with an object texture?
I am not making a claw game, it was just the first example that popped to my head where both could be plausible solutions. The former being more simplistic - just switch the texture, the latter being more generalizable.
If the latter is the best solution all around, how can one ''pair'' the movements of the two sprites?
I would use the second approach especially if the objects a and b can fall out of the claw at some point.
You should make the texture of a claw, the texture of object a, and the texture of object b.
To make the objects move in sync with the claw, simply add the object as a child of the claw node. This way, moving the claw will cause the object to move as well!
Note that before adding the object as a child, you need to make sure that the object does not have a parent. If it does, you must remove it from its parent before adding it as a child of the claw. The same thing applies when you "release" the object from the claw: you need to first remove the object from its parent (the claw) and then add it as a child of the desired parent.