- Home /
The question is answered, right answer was accepted
Get child position in world space
In my 2d game I have a character walking around with arrow keys and a weapon as a child of my character GameObject. My weapon has an empty GameObject as child which represents the point where the bullets should be instantiated. My character allways rotates toward my mouse.
But now when I instantiate a bullet at the transform.position of my Empty..
(Instantiate(bullet, firePoint.transform.position, character.transform.rotation * Quaternion(0,0,90,-90))
)
.. the bullets allways spawn at the position which should be relative to my weapons position:
How can I instantiate my bullets at the position of my FirePoint in the scene view?
edit(moved script from comment)
projectile script:
#pragma strict
var speed : float;
function Start () {
var sp = Camera.main.WorldToScreenPoint(transform.position);
var dir = (Input.mousePosition - sp).normalized;
var rig = GetComponent(Rigidbody2D);
rig.AddForce(dir * speed);
}
function Update () {
}
function OnTriggerEnter2D(other : Collider2D) {
if (other.gameObject.name == "enemy") {
Destroy(other.gameObject);
}
if (other.gameObject.name == "Player") {
Destroy(this.gameObject);
}
}
I thought about juts adding the parents position together scince the childs positions are all relative to the parnets position but when I rotate my parent, the child doesnt 'know' it has to add its position relative to the rotation of its parent!!!
Is it possible that you have switched the inspector into "debug" mode? In debug mode a lot strange things can happen. This should not be the default. Unless you want to debug something specifically you should stick to the "normal view".
You selected your firePoint gameobject but we can't see anything selected in the sceenview. So my guess is that your firePoint object is not where you think it is. $$anonymous$$aybe you somehow messed up your parent child setup?
@Bunny83 how to I change to normal mode? But I think didn't change it..
And when you click on 'scene.png' below, it opens the image in fullscreen, there you should see 'FirePoint' next to this red arrow :)
@JanVog:
I just realised that you have your editor in 2d mode. i was expecting the usual arrows ^^, my bad.
You can switch an inspector into debug mode in the window menu at the top right of the inspector window. However it actually looks normal ^^.
Is there a script attached to the projectile? (I guess "projectile.js"?) what do you do inside that script?
Answer by JanVog · Sep 05, 2015 at 03:13 PM
First Comment contains solution.
Like i said in my answer you don't use the firePoint of your actual weapon, but the firePoint object of your prefab. In this line:
var equippedWeapon : eWeapon = new eWeapon(Instantiate(weaponGO), weaponGO.transform.Find("FirePoint").gameObject, weaponGO.transform.Find("FireballProjectile").gameObject, Time.time, weapon);
You create an instance of your "weaponGO" which references your prefab. The actual instance is returned by Instantiate and passed as first parameter to your eWeapon constructor. However the second parameter which should be your firePoint gameobject is obtained from the prefab reference and not from your actual instantiated weapon.
btw: You shouldn't post code as an answer if it doesn't answer the question. Since your script code belongs to your question you should have edit your question and include the code there. You can edit your question by clicking the "gear" symbol on the right of your question.
Please stop posting the same code over and over as answer. Add it to your question.
@Bunny83 Oh sry ok, I am quite new to this Forum and didn't know this was possible :D
Answer by wibble82 · Sep 05, 2015 at 09:43 AM
Hi JanVog
Your code doesn't look obviously wrong to me, though I'm not entirely convinced of the behaviour of 'Instantiate'. Can I suggest you try:
GameObject new_bullet = (GameObject)Instantiate(bullet);
new_bullet.transform.position = firePoint.transform.position;
I deliberately left the rotation bit off there cos it looks a little dodgy ([0,0,90,-90] is not a valid quaternion) but you can add it afterwards if you need to in the same way I did the position. It may even be the case that providing instantiate with an invalid rotation is causing problems.
The code above is slightly different, as I am now instantiating and then setting the position, so the 'Start' function of your bullet will get called before the position is set (I think). I'm fairly sure your code should work though, so I mainly present this as a test - it should definitely work. So if it doesn't, I'd suggest either you are inadvertently setting the bullet's position elsewhere, or firePoint.transform.position is not what you think it is for some reason.
I would also try adding 'Debug.Log(firePoint.transform.position)' just to make sure it is what you think it is. Similarly, if you have a script on your bullet, you could try printing out its own position in the 'start' function and every frame in the 'update' function to see if it changes in some way you don't expect.
(+1)
Well summary. And you addressed the quaternion issue as well. He might wanted to use Quaternion.Euler() ins$$anonymous$$d.
Transform.position will always return the world position of a GameObject, no matter how deep it's burried in the hierarchy. Transform.localPosition always is the position relative to the objects immediate parent. If there's no parent at all it equals "position".
Your 'test' doesn't quite work aswell somehow, just does the same as before.
Ok I changed 'Quaternion(0,0,90,-90)' to 'Quaternion.Euler(0,0,-90)' but it doesn't change the position at all.
(I placed my code in an answer below)
@wibble82 I aswell placed a script on my 'FirePoint' which prints the transform.position every frame and you are right, it shows the world space Coordinates of 'FirePoint'. The position showed when i print its transform.position on my Player-Script seems to be the position relative to its parent world position (the position of the actual weapon). Aswell it's quite interesting that the position of the weapon (parent of FirePoint) allways stay (0,0,0) when printing its transform.position. But when printing its transform.position in a script on the weapon GameObject, it shows the real world space Coordinates?!
Answer by getyour411 · Sep 05, 2015 at 07:25 AM
Try this http://answers.unity3d.com/questions/56452/instantiate-cloned-prefab-to-local-position-of-an.html
one of many found via Google/Search: Unity instantiate transform child
@getyour411 Didn't work, doesn't change anything at all!!
And well I searched for any sulotion on google about an hour before creation this question, but didn't get any working solution!!
Answer by Bunny83 · Sep 05, 2015 at 02:20 PM
Since it seems you get two different position value for the seemingly same object the only plausible answer is: Your "firePoint" variable doesn't reference the actual "firePoint" gameobject you have as child on your weapon in your scene.
Since the name of your weapon has the "(clone)" suffix i guess you instantiated the weapon as well at runtime. Where is the script attached that executes the bullet instantiate line? Is it on the instantiated weapon or on the player? If it's on the player, how do you initialize your firePoint variable? If you dragged the prefab on to that variable then it's still referencing the prefab which probably has it's default position at (0,0,0).
At runtime, select the object in the scene that holds the script which has the "firePoint" variable. If the variable is a public variable you can simply left click onto the object field and Unity will "flash" the actual object in the hierarchy or project view. If the variable is private you could switch the inspector to "debug mode" and you should see the variable.
If firePoint is no member variable at all but just a local variable inside a method you should check the way you obtain the reference to the firePoint object.
ps:
It's not really a good design when the bullet reads the users input itself and determines the direction itself. The direction in which the bullet should fly should be determined by the weapon that launches the projectile.
Follow this Question
Related Questions
child object does not show position change in editor even though it is rotating with its parent. 2 Answers
Find Child's Position Relative To World (Global Position) 1 Answer
Child coordinates misunderstanding. 0 Answers
Why is it moving locally instead of globally? 1 Answer
Local Lerp to origin 1 Answer