Hi guys I am really struggling getting my character controller script going on TNet server, I spent 9am to 3am on it yesterday :shock:
and got nowhere, im not the best programmer at all. I have been talking in the TNet forums but am not getting the help I need as I dont understand as I am a novice. I have scrapped my script a hundred times, I dont particularly want to use the script I have written for using unitys built in networking, it is probably a very bad way to do it to begin with. I just want to understand how to sync movements and sync animation using RFC.
This is the code I was using for unity’s built-in networking
using UnityEngine;
using System.Collections;
public class PlayerInput : MonoBehaviour {
public enum CharacterState {
idle,
walking
}
public CharacterState _state;
public string idleAnimName;
public string walkAnimName;
public int turnSpeed;
public int moveSpeed;
void Awake() {
if (!networkView.isMine) {
GetComponentInChildren<Camera>().enabled = false; // disable the camera of the non-owned Player;
GetComponentInChildren<AudioListener>().enabled = false; // Disables AudioListener of non-owned Player - prevents multiple AudioListeners from being present in scene.
}
}
void Start() {
animation.wrapMode = WrapMode.Loop;
}
void Update() {
if (networkView.isMine) {
InputMovement();
CheckKey();
} else {
SyncedMovement();
}
}
void InputMovement() {
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
transform.Rotate( 0, h * turnSpeed * Time.deltaTime, 0 );
Vector3 moveAmount = transform.forward * v * moveSpeed;
rigidbody.MovePosition( transform.position + moveAmount * Time.deltaTime );
rigidbody.velocity = moveAmount + Vector3.Scale(rigidbody.velocity, new Vector3(0,1,0));
}
void CheckKey()
{
if(Input.GetKeyDown(KeyCode.W)) {
_state = CharacterState.walking;
} else if (Input.GetKeyUp(KeyCode.W)) {
_state = CharacterState.idle;
}
PlayAnimation();
}
void PlayAnimation() {
switch(_state)
{
case CharacterState.idle:
animation.CrossFade(idleAnimName);
break;
case CharacterState.walking:
animation.CrossFade(walkAnimName);
break;
}
}
void OnCollisionEnter() {
Debug.Log("hit the ground");
}
private float lastSynchronizationTime = 0f;
private float syncDelay = 0f;
private float syncTime = 0f;
private Vector3 syncStartPosition = Vector3.zero;
private Vector3 syncEndPosition = Vector3.zero;
private void SyncedMovement() {
syncTime += Time.deltaTime;
rigidbody.position = Vector3.Lerp(syncStartPosition, syncEndPosition, syncTime / syncDelay);
}
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info) {
Vector3 syncPosition = Vector3.zero;
Vector3 syncVelocity = Vector3.zero;
Quaternion syncRotation = Quaternion.Euler(0,0,0);
if (stream.isWriting)
{
syncPosition = rigidbody.position;
stream.Serialize(ref syncPosition);
syncVelocity = rigidbody.velocity;
stream.Serialize(ref syncVelocity);
syncRotation = transform.rotation;
stream.Serialize(ref syncRotation);
}
else
{
stream.Serialize(ref syncPosition);
stream.Serialize(ref syncVelocity);
syncTime = 0f;
syncDelay = Time.time - lastSynchronizationTime;
lastSynchronizationTime = Time.time;
syncEndPosition = syncPosition + syncVelocity * syncDelay;
syncStartPosition = rigidbody.position;
stream.Serialize(ref syncRotation);
transform.rotation = syncRotation;
}
}
}
Can anyone help me that has TNet or understands it to either help me convert this to work or use help with a better Character controller and animation script? Only need a simple script just to see how it works