- Home /
Transform.localPosition Script Problem.
Hello. I have created a script that, when a certain object is touched, the parent of several object would have moved along the Z axis. The script I made is
using UnityEngine;
using System.Collections;
public class Expands : MonoBehaviour {
public Transform parent;
public float expandAmount = 1.5f;
public float localPosition = 1f;
void OnMouseDown()
{
parent.localScale *=expandAmount;
parent.localPosition *= localPosition;
for(int i =0;i<parent.childCount;i++)
{
parent.GetChild(i).localScale =new Vector3(1/parent.localScale.x,1/parent.localScale.y, 1/parent.localScale.z);
parent.GetChild(i).localPosition = new Vector3(0, 0, -1f);
}
}
}
The focus is the localPosition ( Just stating ). So part of the script does what I want, when the object the script is on gets touched, the parent moves along the axis line. However, the children in the script change their position as well. Since the part ( parent.GetChild(i).localPosition = new Vector3(0, 0, -1f); ) states that the x and y axis be 0, it moves all the children to the 0 mark for the x and y axis.
How can it be modified that only the parent gets moved and not the children.
Sorry for the messy writing I am in a rush to type this. If there is any questions, please ask. All help is appreciated
Side note: This game is for the android and is 2D.
You could just try saving the childrens position in a variable and then set the childrens position after you change the parent...
Answer by Eluate · Mar 14, 2015 at 10:51 PM
This temporarily removes the attached parent:
void OnMouseDown()
{
Transform[] childrenTransforms = GetComponentsInChildren<Transform>();
for(int i =0;i<parent.childCount;i++)
{
childrenTransforms[i].parent = null;
}
parent.localScale *=expandAmount;
parent.localPosition *= localPosition;
for(int i =0;i<parent.childCount;i++)
{
childrenTransforms[i].parent = parent.transform;
}
}
Answer by el-pepi · Mar 15, 2015 at 02:27 AM
using UnityEngine;
using System.Collections;
public class Expands : MonoBehaviour {
public Transform parent;
public float expandAmount = 1.5f;
public float localPosition = 1f;
void OnMouseDown()
{
Vector3 oldpos = parent.position;
parent.localScale *=expandAmount;
parent.localPosition *= localPosition; //this is a bit weird?
for(int i =0;i<parent.childCount;i++)
{
parent.GetChild(i).localScale =new Vector3(1/parent.localScale.x,1/parent.localScale.y, 1/parent.localScale.z);
parent.GetChild(i).position -= (parent.position - oldpos);
}
}
}
There you go.
Alright so I took a shot at yours, however it doesn't move the objects at all.
Answer by siaran · Mar 14, 2015 at 11:07 PM
Well, the easiest way to not have child objects move when you move the parent, is by not making them child objects. Are you sure they should be child objects if you are trying to move their parent without moving them?
If I look at your code, I get the sense that you might actually want to reverse the parent-child relation.
That said, there are a few ways to solve your actual problem. Easiest might be to put a script on your child objects that constantly assign it it's 'correct' position - something like
Vector3 worldPos;
void Start(){
worldPos = transform.position;
}
void Update(){
transform.position = worldPos;
}
That's an incredibly ugly way to solve it, and completely stupid too - if you need to write a script like that you should just unparent your objects - but afaik it should work. Alternatively, you might try moving each child object in the reverse direction and amount you moved the parent object in your script. I'd give you an example but looking at your code your movement makes no sense. You're...multiplying parent.localPosition with localPosition? Where localPosition is just 1? Umm, then your parent object isn't moving at all? And even if it's not 1, multiplying with a scalar is not how you move a point. The more I look at it the more I am starting to wonder about your object hierarchy. Could you describe it and why it's like that?
From what I see you aren't moving your parent object at all, you are just making it bigger, so you could probably also simply resolve this by removing all your movement-related code from that script...
General Coding Tip: write some comments in your code. Not only does that make it easier for others to read and debug it, I find that it often helps me get a clearer image of what is happening when I write down above each line what exactly it should do. Especially useful when I'm coding something complex or in an unfamiliar language.
Sorry for the way I have said it, I was incredibly busy at the time of the post. When I meant for the child object to not move, I was just refering to the X and Y axis. I want both the parent and the child to move forward along the Z axis.
Hmm, well, you may still want to think about removing or reversing the parent-child relation, having 2 objects move at the same speed isn't very difficult after all. Or you could temporarily remove the attached parents like the answer above me suggests, but I'd find that a very strange way of doing things.
Well the reason I have the parent there in the first play is because for the other part of the script it allows the children to spread out in a way that I want it to, without the parent the other part of the script would be completely messed up.
Your answer
Follow this Question
Related Questions
Expand object on touch? 1 Answer
Transform.LocalScale Script Problem. Please help. 2 Answers
Expand Object On Touch? Someone please help me 1 Answer
Collision On Specific Object= Destroy not working. 2 Answers
Scaling Script? 1 Answer