- Home /
Multi-player Moving Script Help
Hi. I'm going to get straight to the point. I've been working on a multi-player game, and I got the server to work and everything. But I can't seem to figure out how to get the character to follow the actual character's direction it is pointing. All I can get is w is forward, a is left, s is back, and d is right. I have added the sample mouse look script, so all I need help with is how to get the movement follow the way the camera is pointing. Here is the code:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
public float speed = 10f;
public float rotateSpeed = 10f;
private float lastSynchronizationTime = 0f;
private float syncDelay = 0f;
private float syncTime = 0f;
private Vector3 syncStartPosition = Vector3.zero;
private Vector3 syncEndPosition = Vector3.zero;
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
Vector3 syncPosition = Vector3.zero;
Vector3 syncVelocity = Vector3.zero;
if (stream.isWriting)
{
syncPosition = rigidbody.position;
stream.Serialize(ref syncPosition);
syncPosition = rigidbody.velocity;
stream.Serialize(ref syncVelocity);
}
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;
}
}
void Awake()
{
lastSynchronizationTime = Time.time;
}
void Update()
{
if (networkView.isMine)
{
InputMovement();
InputColorChange();
}
else
{
SyncedMovement();
}
}
private void InputMovement()
{
if (Input.GetKey(KeyCode.W))
rigidbody.MovePosition(rigidbody.position + Vector3.forward * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.S))
rigidbody.MovePosition(rigidbody.position - Vector3.forward * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.D))
rigidbody.MovePosition(rigidbody.position + Vector3.right * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.A))
rigidbody.MovePosition(rigidbody.position - Vector3.right * speed * Time.deltaTime);
}
private void SyncedMovement()
{
syncTime += Time.deltaTime;
rigidbody.position = Vector3.Lerp(syncStartPosition, syncEndPosition, syncTime / syncDelay);
}
private void InputColorChange()
{
if (Input.GetKeyDown(KeyCode.R))
ChangeColorTo(new Vector3(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f)));
}
[RPC] void ChangeColorTo(Vector3 color)
{
renderer.material.color = new Color(color.x, color.y, color.z, 1f);
if (networkView.isMine)
networkView.RPC("ChangeColorTo", RPCMode.OthersBuffered, color);
}
}
Please help! Any feedback will be greatly appreciated!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Making a bubble level (not a game but work tool) 1 Answer
Distribute terrain in zones 3 Answers
Character doesn't stop and sits on top of target. 1 Answer
Player Jumps instead of moving forward 0 Answers