Hello,
I creating top down tank multiplayer game and I using UNet method. My client doesnt syncing moving and shooting to server but server syncing it to client.
My player (Tank) prefab has components: MoveTank Script, NetworkIdentity, NetworkTransform, Rigidbody 2D, Box Colider 2D
My player (Tank) prefab has child: Turret
And Turret has child: BulletPosition
My Bullet prefab has components: Shoot Script, NetworkIdentity, NetworkTransform, Rigidbody 2D
There is MoveTank Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class TankMove : NetworkBehaviour
{ public Transform firepoint;
public GameObject bulletPrefab;
[SerializeField]
private float TankSpeed = 4;
private float fireRate = 8;
private float timeUntilFire = 0;
private void FixedUpdate()
{
if (isLocalPlayer)
{
// Moving
transform.Translate(0f, TankSpeed * Time.deltaTime, 0f);
// Setting Mouse Position
Vector3 mousePosition = Input.mousePosition;
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
// Rotating
Vector2 direction = new Vector2(mousePosition.x - transform.position.x, mousePosition.y - transform.position.y);
transform.up = direction;
// Shooting
if (Input.GetButtonDown("Fire1") && Time.time > timeUntilFire)
{
timeUntilFire = Time.time + 1 / fireRate;
CmdShoot();
}
}
}
[Command]
void CmdShoot()
{
GameObject bullet = Instantiate(bulletPrefab, firepoint.position, firepoint.rotation);
NetworkServer.Spawn(bullet);
}
private void OnMouseOver()
{
TankSpeed = 0;
}
private void OnMouseExit()
{
TankSpeed = 4;
}
}
firepoint is BulletPosition
bulletPrefab is Bullet Prefab
There is Shoot Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shoot : MonoBehaviour
{
public float bulletSpeed = 15;
private void FixedUpdate()
{
transform.Translate(0f, bulletSpeed * Time.deltaTime, 0f);
}
}
Please help me,
Thanks.