I got problem when set position of child to as it parent position.
Below the screenshot :
My script :
RightNext.transform.GetChild (0).gameObject.transform.SetParent (TemporaryPage.transform,false); // TemporaryPage = Cherry Cake
RightNext.transform.GetChild (0).GetComponent<RectTransform> ().localScale = new Vector3 (1, 1, 1);
RightNext.transform.GetChild (0).gameObject.transform.parent = RightNext.transform;
RightNext.transform.GetChild (0).transform.position = RightNext.transform.position;
And i put it in update to make sure the position is not changing.
Why my child position is not inside and same position with parent even i have set it to the same position with parent ?
Thanks
Dennis
You need to change this line:
RightNext.transform.GetChild (0).transform.position = RightNext.transform.position;
to this:
RightNext.transform.GetChild(0).GetComponent<RectTransform>().localPosition = Vector2.zero;
OR this:
RightNext.transform.GetChild(0).GetComponent<RectTransform>().position = RightNext.transform.GetComponent<RectTransform>().position;
Edit after your comment: the two lines I suggested are used to make the child pivot point coincide with the parent pivot point, I thought you wanted that and not to always place the child object pivot at the center of the parent object regardless of its pivot.
With the line you suggested (you can use the 2D version since we're dealing with UI):
RightNext.transform.GetChild(0).GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
you put the child pivot at the center of the parent object, so you need to set the child pivot at (0.5,0.5).
However, I strongly suggest to get references to the RectTransform of both parent and child in the Start method (or after instantiating a new child), to avoid the constant use of the GetComponent method inside Update, since it's a slow method.
Related
I have a simple parent object {Buttons Holder} that has a Horizontal Layout Group component attached to it, so when the game starts I instantiate multiple buttons in the Buttons Holder so that they line up perfectly.
And just for example lets say that I have 2 buttons instantiated and I want at some point these 2 buttons to switch positions so that the first button goes to the second button's position and the second button's switches to the first button's position.
And heres what i'm trying to do:
Vector3 pos1 = buttonPrefabs[0].transform.position;
Vector3 pos2 = buttonPrefabs[1].transform.position;
buttonPrefabs[0].transform.position = Vector3.Lerp(pos1, pos2, Time.deltaTime * 2);
I mean I don't want to snap them to their new positions I just want them to smoothly travel to their new positions using Vector2.lerp or something.
Since the parent object has a Horizontal Layout Group attached to it i'm not able to do that.
Help please.
You can not lerp them to their new position while the position is controlled by the Layout Group. Only thing I can think of is
temporarily disable the Layout Group
then move the child objects smoothly via e.g. MoveTowards
update their child index values to reflect their new order accordingly
enable the layout group again
A more sophisticated approach would require you writing your own Layout Group that offers functionality to let child objects switch positions. That way your own Layout Group could still keep all other children properly positioned, even while two children switch positions.
I have 2 gameobjects within my character say right_obj and left_obj, I want them to ignore parent rotation, like if player change its direction (left, right), these 2 gameobjects stay on their position and rotation, so far I use 2 approaches but fail. here is my code, I use this script on my two gameobjects, but they ignour this lines
// first one approch
Quaternion rotation;
void Awake()
{
rotation = transform.rotation;
}
void LateUpdate ()
{
transform.rotation = rotation;
}
// 2nd approch
void LateUpdate ()
{
transform.rotation = Quaternion.identity;;
}
Do you really need this type of relationship?
If you don't really need the father/child relationship you can spawn an empty GameObject at 0,0,0 and spawn the actual parent and child as childs of the GameObject in the middle of the scene, this way they would still be grouped in some sort of relationship but instead of a father/child you'd get a brother/brother relationship, making the first independent from the second.
If you need to make two objects dependent from each other but not in every single way as it's supposed to be in father/child try using Parent Constraints
This way you'll obtain this type of hierarchy:
- Father GO (at 0,0,0)
-- Child1 (your actual father)
-- Child2 (your actual child)
Child1 and Child2 are syncing position by their ParentConstraint but not their rotation.
Else there is no other way than applying the opposite rotation to the child.
If you really don't want destory parent-child hierarchy, you could rotate child every frame to counteract parent rotation.
Let's say we have the follow scene,
Add the follow RestoreOriginRotation.cs to child object, we can get a stable child rotation.
What we can get.
****If you don't concern the underlying math, you chould stop here. ***
LeviathanCode's "Parent Constraints" solution only works with Unity 2018.1+ because it's a new addition.
My suggestion isn't a "pretty" solution but it'll also work with older versions if you want to prevent imitating rotations while still copying movement:
Get rid of the parent/child relationship, add a new parent GameObject (as LeviathanCode suggested) and a script to it that simply copies the player's movement each frame:
public GameObject player; //Drag the "player" GO here in the Inspector
public void LateUpdate() {
transform.position = player.transform.position;
}
This way your "right_obj" and "left_obj" won't rotate like the player but still move "with" it.
You could also use a Rotation Constraint component. This is similar to the Parent Constraint mentioned above, but it's simpler to understand in my opinion - it just uses the Rotation and only the rotation of whichever GameObjects you slot in.
You just have to make an empty game object and set the rotation to 0,0,0 , then set this empty game object as the source of the rotation constraint. I have this exact setup in my game, and it overrides the rotation of the actual parent, which is what I want.
Perhaps not the absolute cleanest solution, but it's something to consider.
Here is the docs link - https://docs.unity3d.com/Manual/class-RotationConstraint.html
As #aaronrums stated, a Rotation Constraint is an easy way to achieve this.
Here, I have 3 game objects. The main parent object container ("Player Seat") which holds all the children will always be set to a rotation of 0 on the Z axis. The GO I'm trying to keep positioned vertically (so the text and avatar doesn't rotate) is "DrewPlayer." I have it offset on the X-axis by 100 in its parent "Player Position." I added a rotation constraint on the item I didn't want to rotate. (a.k.a "DrewPlayer") Once the constraint was on it, I dragged over the "Player Seat" to the source list and when I rotated "Player Seat," I was able to achieve the desired effect in the gif below.
Thanks #aaronrums! Saved me some effort here. :)
I feel like this is a noob question but, I can't seem to find an example anywhere. Basically, what I am doing is I have a root object at the zero point and whenever I click on one of its children I scale up the entire object a certain ratio. What I am trying to do next is take the center of the child object that the user clicked on and put it at the zero point but also keeping it in the same local position with the root object.
I thought it would be as simple as:
root.transform.position = root.transform.position - obj.transform.localPosition;
that seems to get it close but, it is not quite the zero point.
EDIT:
To Clarify I want to move the parent but, make it so that the child is at 0,0,0 world position whereas the parent was at the 0,0,0 point previously. Think of it like this:
You have a small car model, the user clicks on the door. The car scales up and the whole car moves so that the door is at the zero point, offset by the distance that the door is away from the center of the car.
If the parent object itself is not a child of another entity, then simply set its world position to minus the child object's world position:
root.transform.position = -(child.transform.position);
The transformation from local to world position automatically takes into account the scaling.
I'm not sure if I understand completly your question. Do you want to put the child on the 0,0,0 position of the parent or the screen position? or do you want to be moved with the scaling of the parent?
If you want the child to move to the cero coordinates of the parent:
child.transform.localePosition = new Vector3(0,0,0);
Where "child" is the object child inside the object you want to scale.
You can see an example of it here.
I have an object in my scene's sky and want to prevent it from getting cut out of the view, no matter the screen ratio. So I set up a canvas and a PlaceHolder RectTransform on it, that - as being UI element - gets scaled and repositioned in respect to the screen aspect ratio. My goal is to use this position to determine where to put my scene object by using the following script:
Camera mainCam;
RectTransform placeHolder;
private void Start()
{
mainCam = GameObject.FindGameObjectWithTag("MainCamera")
.GetComponent<Camera>();
placeHolder = GameObject.FindGameObjectWithTag("SkyObjectPlaceholder")
.GetComponent<RectTransform>();
var screenPos = mainCam.ScreenToWorldPoint(
new Vector3(placeHolder.rect.x, placeHolder.rect.y, 0));
transform.position = new Vector3(screenPos.x, screenPos.y,
transform.position.z);
}
The issue is that my sky object is always placed in the center of the screen. It is not a child of anything on the scene, I just put it there. The placeholder is to the upper right in the canvas. Note that I tried playing with anchors for the UI element, set it to be left bottom, right top, no change, the scene object is always in the center of the screen.
What am I doing wrong? How to make a Scene element match up the position of a Canvas element, so it gets right behind it?
Note: I'm using a perspective camera. I don't know if it matters, but I thought I mention it, just in case.
Yet another edit: after messing around with the issue a bit, I found that the problem is possibly linked to the Z input I give the ScreenToWorldPoint, in my case it's 0. I couldn't get a viable Z to input though.
Hmmm... i think you should change the sorting layer of that object
I am instantiating an object with following code
public Object ball;
Instantiate(ball, hit.point , Quaternion.identity) ;
Where hit.point is a position and ball is a prefab I created, but unfortunately this prefab has a Position animation - jumping ball - and because of that as it animates it stays in animation's position. I can't even Translate it.
How can I move it or change animation somehow ?
There is more than one way to solve this problem, depending on your other goals/constraints if any.
One simple approach is to separate this problem into two spaces, via the introduction of an empty parent node.
If you construct..
[Empty Parent Node] (created dynamically)
|- [Ball] (created from your prefab)
..then you can still apply the animation to [Ball], now within a "local space" defined by [Empty Parent Node]. Further, you can now specify an arbitrary position for the parent node, which acts to place the ball overall.
Another possible solution is to change your animation from a position animation to a localPosition animation. Then, you'll be able to change its Transform.position attribute with scripts.