I have this problem where my object will not spawn in the correct position when instantiated
public GameObject Weapon;
public GameObject bulletPrefab;
public GameObject spawnedWeapon;
public GameObject parent;
public Transform firePoint;
public float xpos;
public float ypos;
public float xposFire;
public float yposFire;
public int Swap = 0;
public float rateOfFire;
void Start()
{
xposFire = firePoint.transform.position.x;
yposFire = firePoint.transform.position.y;
}
void Update()
{
xpos = parent.transform.position.x;
ypos = parent.transform.position.y;
xposFire = firePoint.transform.position.x;
yposFire = firePoint.transform.position.y;
if (Input.GetKeyDown(KeyCode.Space))
{
if (Swap == 1)
{
SwapWeapon();
Debug.Log("im pooped");
}
if (Swap == 0)
{
Invoke("Shoot", rateOfFire);
Debug.Log("im pooped");
}
}
}
void SwapWeapon()
{
spawnedWeapon = Instantiate(Weapon, new Vector3(xpos + .5f, ypos, 0), Quaternion.identity);
spawnedWeapon.transform.parent = parent.transform;
Swap = 0;
}
void Shoot()
{
Instantiate(bulletPrefab, firePoint.Position, firePoint.rotation);
}
when I call the shoot function and instantiate the bullet prefab it spawns in the completely wrong place. Even when I change firePoint.Position to something else it spawns in that same wrong place. Any fixes?
This code doesn’t compile. The property you’re looking for is lowercase .position.
Also, adding onto what Praetor said above, make sure everything in your prefab structure is where you think it is. It’s super easy to make the top object be at (0,0,0) but then the next one is somewhere else.
ALSO: are there animations on your object? If they have root motion checked, they can drive the root position, which you likely don’t want.
This is not a working code example, just a few thoughts and a refactoring of yours:
using UnityEngine;
public class Player : MonoBehaviour
{
[SerializeField]
private Transform _player;
[SerializeField]
private GameObject _weaponGO = null;
[SerializeField]
private GameObject _bulletPrefab = null;
[SerializeField]
private Transform _firePoint = null;
[SerializeField]
private float _fireRate = 1f;
private float _coolDownTimer = 0f;
protected void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (_coolDownTimer == 0f)
{
Shoot();
_coolDownTimer = _fireRate;
}
else
{
_coolDownTimer -= Time.deltaTime;
}
}
else if (Input.GetKeyDown(KeyCode.Return))
{
SwapWeapon();
}
}
private void SwapWeapon()
{
var spawnedWeapon = Instantiate(_weaponGO, new Vector3(_player.position.x + .5f, _player.position.y, 0), Quaternion.identity);
// use an own Weapon class with fireRate, FirePoint property etc. and move shooting logic into that
//_firePoint = spawnedWeapon.GetComponent<Weapon>().FirePoint;
spawnedWeapon.transform.parent = _player.transform;
_weaponGO = spawnedWeapon;
}
private void Shoot()
{
Instantiate(_bulletPrefab, _firePoint.position, _firePoint.rotation);
}
}
I think you main problem is, that you are not setting a new firePoint of the new spawned weapon. Add an own component called Weapon to split you logic between player and weapon.
Keep in mind that all things done in Update() are expensive. Your are collecting positions even if no key is pressed.
Set everything to private as long as you are not accessing it from the outside. Use style guidelines in a C# manner, when your project becomes bigger it becomes confusing when you just add little snippets to solve particular problems.