- Home /
How to reset character orientation based on direction the camera is facing
A friend of mine and I are attempting to create a first person multiplayer game. We currently have our characters able to load and join the server, as well as a camera that moves with the mouse. However, the characters orientation is consistent no matter what direction the camera is facing. (I.E. When facing backwards and trying to move forwards, the character is going backwards in perspective to the camera.) What code can I add in C# to set my characters orientation to the cameras facing direction?
EDIT: Here's my current code that I have attached to the player. It is a multiplayer network. The code did not originate from me, we used a tutorial to get to this point. Now, we are attempting to edit it from a third person view where the camera was immobile into a first person view. I am using the default MouseLook script to move the camera.
public class Player1 : MonoBehaviour
{
public float speed = 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);
if (Input.GetKey (KeyCode.Space))
rigidbody.MovePosition (rigidbody.position + Vector3.up * speed * Time.deltaTime);
{
transform.forward = Camera.main.transform.forward;
}
}
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);
}
}
Welcome to UA, Please show us code and/or tell us what you've tried when posting questions. Where prudent, screenshots or diagrams always help.
I don't understand the scenario you're trying to describe, and it's likely others won't either. Please re-phrase your question, either by editing if you can, or posting a comment here.
Are you using the "first person controller" script and CharacterController component from StandardAssets?. Are you using the stock $$anonymous$$ouseLook script to drive the camera? Take a look at the FPS prefab from StandardAssets for an example of a working FPS controller.
Gotta be super descriptive around here. ;)
Answer by Cherno · Mar 17, 2015 at 04:55 PM
Change Vector3.forward (... right, up etc.) to transform.forward (... right, up etc.).
Amazing, thank you! Worked beautifully. As you can probably tell by how simple that seems, I am a beginner. Only reason I got that far was the tutorial told me what to use.
Glad to be of help. Feel free to mark the answer aas accepted if it indeed helped you with your problem.