Hi guys,
following issue:
I have a pretty basic unet multiplayer game, with multiple players running around… all working so far. but now i want each player’s legs to face the direction which he’s walking. i’m doing this by using transform.localEulerAngles and it works well in for each player (no matter if host or client), but one player doesn’t see other player’s legs rotating, so there is no synchronization.
the legs are a child object of the player, who has a network identity and network transform and a network transform child component attached (with the legs attached as the child target).
The player has a PlayerMovement script, where the walkingRight, walkingLeft, walkingForward etc. variables of the LegsRotation script are adjusted to the current movement. the LegsRotation script should then handle the rest of it:
PlayerMovement script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class PlayerMovement : NetworkBehaviour
{
//Movement Variables
public float speed;
public float sensitivity = 3f;
private float yaw = 0f;
public GameObject legs;
public Animator anim;
public Transform tf;
Rigidbody rb;
private void Start()
{
tf = GetComponent<Transform>();
rb = GetComponent<Rigidbody>();
anim = GetComponent<Animator>();
}
private void Update()
{
[COLOR=#000000] //Only for local player...
if (!isLocalPlayer)
{
return;
}
//Movement
legs.GetComponent<LegsRotation>().walkingRight = false;
legs.GetComponent<LegsRotation>().walkingLeft = false;
legs.GetComponent<LegsRotation>().walkingForwards = false;
legs.GetComponent<LegsRotation>().walkingBackwards = false;
if (Input.GetKey(KeyCode.W))
{
walk(0, 0, 1);
legs.GetComponent<LegsRotation>().walkingForwards = true;
}
if (Input.GetKey(KeyCode.S))
{
walk(0, 0, -1);
legs.GetComponent<LegsRotation>().walkingBackwards = true;
}
if (Input.GetKey(KeyCode.D))
{
walk(1, 0, 0);
legs.GetComponent<LegsRotation>().walkingRight = true;
}
if (Input.GetKey(KeyCode.A))
{
walk(-1, 0, 0);
legs.GetComponent<LegsRotation>().walkingLeft = true;
}[/COLOR]
//Mouse View
yaw += sensitivity * Input.GetAxis("Mouse X");
transform.eulerAngles = new Vector3(0f, yaw, 0f);
}
void walk(int x, int y, int z)
{
tf.Translate(x*speed, y*speed, z*speed);
}
}
LegsRotation script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LegsRotation : MonoBehaviour
{
public bool walkingRight = false;
public bool walkingLeft = false;
public bool walkingForwards = false;
public bool walkingBackwards = false;
private void Update()
{
transform.localEulerAngles = new Vector3(270f, 0f, 0f);
if (walkingForwards && !walkingBackwards)
{
transform.localEulerAngles = new Vector3(270f, 0f, 0f);
if (walkingRight && !walkingLeft) transform.localEulerAngles = new Vector3(270f, 45f, 0f);
if (walkingLeft && !walkingRight) transform.localEulerAngles = new Vector3(270f, 315f, 0f);
}
if (walkingBackwards && !walkingForwards)
{
transform.localEulerAngles = new Vector3(270f, 180f, 0f);
if (walkingRight && !walkingLeft) transform.localEulerAngles = new Vector3(270f, 135f, 0f);
if (walkingLeft && !walkingRight) transform.localEulerAngles = new Vector3(270f, 225f, 0f);
}
if (walkingRight && !walkingForwards && !walkingBackwards && !walkingLeft)
{
transform.localEulerAngles = new Vector3(270f, 90f, 0f);
}
if (walkingLeft && !walkingForwards && !walkingBackwards && !walkingRight)
{
transform.localEulerAngles = new Vector3(270f, 270f, 0f);
}
}
}
do you have any idea, why it isn’t working? i thought that every transformation/rotation gets synchronized, when you do network transform, no matter how the transformation is initiated… do i have to run the code for this on the server?
Thanks for your ideas and also for studying my case