- Home /
Pushed back when applying NetworkTransform
Hello world,
I'm working on a FPS game, but i'm stuck on the networking part. This is what happens in the game: The hosts creates a game and can move without problems, also the network part goes right. But when a client joins, you can see the host running, etc.. But the client is always pushed back to the start position.
Scripts:
TransformMotion:
using UnityEngine;
[RequireComponent(typeof(TransformMotor))]
public class TransformMotion : MonoBehaviour
{
[SerializeField]
private float speed = 5f;
[SerializeField]
private float lookSensitivity = 3f;
private TransformMotor motor;
void Start()
{
motor = GetComponent<TransformMotor>();
}
void Update()
{
// Movement
float _xMov = Input.GetAxisRaw("Horizontal");
float _zMov = Input.GetAxisRaw("Vertical");
Vector3 _movHorizontal = transform.right * _xMov;
Vector3 _movVertical = transform.forward * _zMov;
Vector3 _velocity = (_movHorizontal + _movVertical).normalized * speed;
motor.Move(_velocity);
// Rotation (around)
float _yRot = Input.GetAxisRaw("Mouse X");
Vector3 _rotation = new Vector3(0f, _yRot, 0f) * lookSensitivity;
motor.Rotate(_rotation);
// Camera Rotation
float _xRot = Input.GetAxisRaw("Mouse Y");
Vector3 _cameraRotation = new Vector3(_xRot, 0f, 0f) * lookSensitivity;
motor.RotateCamera(_cameraRotation);
}
}
TransformMotor:
using System;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class TransformMotor : MonoBehaviour
{
private Vector3 velocity = Vector3.zero;
private Vector3 rotation = Vector3.zero;
private Vector3 cameraRotation = Vector3.zero;
private Rigidbody rb;
[SerializeField]
private Camera cam;
void Start()
{
rb = GetComponent<Rigidbody>();
}
public void Move(Vector3 _velocity)
{
velocity = _velocity;
}
void FixedUpdate()
{
PerformMovement();
PerformRotation();
}
void PerformMovement()
{
if(velocity != Vector3.zero)
{
rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
}
}
public void Rotate(Vector3 _rotation)
{
rotation = _rotation;
}
void PerformRotation()
{
rb.MoveRotation(rb.rotation * Quaternion.Euler (rotation));
if(cam != null)
{
cam.transform.Rotate(-cameraRotation);
}
}
public void RotateCamera(Vector3 _cameraRotation)
{
cameraRotation = _cameraRotation;
}
}
Answer by Matthewj866 · May 14, 2017 at 07:35 PM
You have a few problems here.
Firstly, you need to use NetworkBehaviour
and not MonoBehaviour
if the class is going to change things over the network.
Secondly, there is no check on the local authority of the object so once this works, any player will be able to move all players.
Did you add your NetworkIdentity
object?
Does the game know what prefab is the player prefab?
If this is your first time making anything with UNet I highly recommend having a flick through this tutorial to get the basic idea:
https://unity3d.com/learn/tutorials/topics/multiplayer-networking
Based on the information provided these are the only possible issues I can think of.
Hope this helps.
@$$anonymous$$atthewj866 I have another script for disabling script on join. $$anonymous$$y object has NetworkIDentity indeed. I think the problem is the NetworkTransform component. When i join as a client with my Editor, i'm being pushed back. When disabling the NetworkTransform compenent i can walk without problems, not syncing ofcourse. When i reactivate the component i'm being pushed back to the start position. Any help?
It definitely sounds like a permissions problem. I still think you should use NetworkBehaviour
and do a local authority check rather than disabling scripts on join because NetworkBehaviour
classes are allowed to manipulate things on the network.
Can I see the settings on your NetworkTransform
, as well as your Network$$anonymous$$anager
?
@$$anonymous$$atthewj866 I added local authority check in the Transform$$anonymous$$otor script but still i got pushed back as a client. Components: