- Home /
Spawning Gameobject at seemingly right position
Hi again, So although i thought i knew already, i got a great explanation about how positioning works yesterday here ,
Now i felt like going back to my project since everything should be clear. Im simply trying to spawn an object at the location of the playercapsule. But even if i copy the position and create a new cube at the given position, its somewhere completely else. Any hints why? (see pictures attached)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerShoot : MonoBehaviour
{
public GameObject coin;
public GameObject PlayerCapsule;
private Vector3 PlayerPosition;
private PlayerInput playerInput;
private InputAction shoot;
private void Awake()
{
playerInput = new PlayerInput();
}
private void OnEnable()
{
shoot = playerInput.Player.Shoot;
shoot.Enable();
}
private void OnDisable()
{
shoot = playerInput.Player.Shoot;
shoot.Disable();
}
void Update()
{
CoinPosition = PlayerCapsule.transform.localPosition;
if (Keyboard.current.gKey.isPressed)
{
Instantiate(coin, CoinPosition, coin.transform.rotation);
}
}
}
Answer by oscarAbraham · Sep 26, 2021 at 06:06 AM
The position shown in the inspector is the local position. Two objects with the same local position, but with different parents in different positions, will be in different places. In a practical use case, you'll probably copy the position in code with transform.position; it should be fine in that case, because that position is a world position.
thanks! for some reason even after reading so many posts about it, i still thought the opposite was true. it works now!
Your answer
Follow this Question
Related Questions
Spawning GameObject contained within another 1 Answer
Spawn an object with additional parameter 1 Answer
Spawn items around GameObject 3 Answers
Stop object spawning on top of each other? 1 Answer
object not spawning but no error message 2 Answers