On clicking the button, I call the Shoot () function, which should create a bullet in the place of the character, but creates in the center of the scene.
At the Start() in the PlayerShooter code, I checked that it returns the correct trasform.position value, but when the button is pressed in the Shoot () function it returns 0,0,0
I figured out that bullet spawns on prefab’s position, not on instantiated object from prefab position, how can i fix this?
Shoot code:
using Photon.Pun;
using UnityEngine;
public class PlayerShooter : MonoBehaviour
{
[SerializeField] private GameObject _bullet;
[SerializeField] private float _distanceMultiplier;
public void Shoot()
{
if (PhotonNetwork.PlayerList.Length > 1)
{
PhotonNetwork.Instantiate(_bullet.name, transform.position, transform.rotation);
}
}
}
Character moving script:
using UnityEngine;
using Photon.Pun;
[RequireComponent(typeof(Rigidbody2D), typeof(CapsuleCollider2D))]
public class PlayerMover : MonoBehaviour
{
[SerializeField] private float _speepMoving, _speedRotation;
private Rigidbody2D _rigidbody;
private Vector2 _direction;
private PhotonView _view;
[HideInInspector] public FixedJoystick joystick;
private void Start()
{
_view = GetComponent<PhotonView>();
_rigidbody = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
if (_view.IsMine && PhotonNetwork.PlayerList.Length > 1)
{
_rigidbody.velocity = new Vector2(joystick.Horizontal * _speepMoving, joystick.Vertical * _speepMoving);
RotatePlayer();
}
}
private void RotatePlayer()
{
_direction = new Vector2(joystick.Horizontal, joystick.Vertical);
if (_direction != Vector2.zero)
{
Quaternion toRotation = Quaternion.LookRotation(Vector3.forward, _direction);
_rigidbody.MoveRotation(Quaternion.Lerp(transform.rotation, toRotation, _speedRotation * Time.deltaTime));
}
}
}