- Home /
Question by
Damastasam · Jan 22, 2021 at 11:20 PM ·
2dmultiplayermultiplayer-networkingtop down shootertop-down
Top Down 2D Network Transform
Hey there!
What I'm working on
I am using Mirror Networking, creating a multiplayer top down 2D point and shoot.
What I want to happen
I want my script to rotate the gameObject "PlayerAiming" and sync the rotation to all clients.
What is currently happening
The rotation of PlayerAiming works locally only, it doesnt sync up to other clients. It also locks on startup at a weird angle for other clients.
Movement works great, everything is synced. But rotation wont work.
Code Movement Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
namespace MD.Player
{
public class PlayerMovement : NetworkBehaviour
{
[SerializeField] public float moveSpeed = 5f;
[SerializeField] private Rigidbody2D rigidbody = null;
private Vector2 previousInput;
private Controls controls;
private Controls Controls
{
get
{
if (controls != null) { return controls; }
return controls = new Controls();
}
}
public override void OnStartAuthority()
{
enabled = true;
Controls.Player.Move.performed += ctx => SetMovement(ctx.ReadValue<Vector2>());
Controls.Player.Move.canceled += ctx => ResetMovement();
}
[ClientCallback]
private void OnEnable() => Controls.Enable();
[ClientCallback]
private void OnDisable() => Controls.Disable();
[ClientCallback]
private void FixedUpdate() => Move();
[Client]
private void SetMovement(Vector2 movement) => previousInput = movement;
[Client]
private void ResetMovement() => previousInput = Vector2.zero;
[Client]
private void Move()
{
Vector2 vertical = rigidbody.transform.up;
Vector2 horizontal = rigidbody.transform.right;
vertical.x = 0f;
horizontal.y = 0f;
Vector2 movement = horizontal.normalized * previousInput.x + vertical.normalized * previousInput.y;
rigidbody.MovePosition(rigidbody.position + movement * moveSpeed * Time.fixedDeltaTime);
}
}
}
Aiming Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
namespace MD.Player
{
public class PlayerAim : NetworkBehaviour
{
[SerializeField] private Camera playerCamera;
[SerializeField] private GameObject playerChild;
private Vector2 previousMousePosition;
private Controls controls;
private Controls Controls
{
get
{
if (controls != null) { return controls; }
return controls = new Controls();
}
}
public override void OnStartAuthority()
{
playerCamera.gameObject.SetActive(true);
enabled = true;
Controls.Player.Look.performed += ctx => SetMousePosition(ctx.ReadValue<Vector2>());
Controls.Player.Look.canceled += ctx => ResetMovement();
}
[ClientCallback]
private void OnEnable() => Controls.Enable();
[ClientCallback]
private void OnDisable() => Controls.Disable();
[ClientCallback]
private void Update() => Look();
[Client]
private void SetMousePosition(Vector2 mousePosition) => previousMousePosition = mousePosition;
[Client]
private void ResetMovement() => previousMousePosition = Vector2.zero;
[Client]
private void Look()
{
Vector3 mouseWorldPosition = playerCamera.ScreenToWorldPoint(previousMousePosition);
Vector3 targetDirection = mouseWorldPosition - playerChild.transform.position;
float angle = Mathf.Atan2(targetDirection.y, targetDirection.x) * Mathf.Rad2Deg;
playerChild.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, angle + -90));
Debug.Log("mouseWorldPosition: " + mouseWorldPosition + "| targetDirection: " + targetDirection + "| angle: " + angle + "|");
}
}
}
Thanks!
unityprob.png
(12.1 kB)
Comment