I’m using c#.
Okay so I’m making a 2d online shooter game. I’m completely new on the topic of building an online game. But I need to share the correct information between computers. I already got sharing position working. I don’t want all the weapons to rotate towards the local players mouse position. I want every player to rotate their own weapon, and then “network transform/share” that to the other game instances.
My player is a game object with network transform and identity components. It has a child-component with the weapon, which has a script that rotates it.
Rotation script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ArmRotation : MonoBehaviour {
void Update () {
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
difference.Normalize();
float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rotZ - 90);
}
}
Player script:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class PlayerController : NetworkBehaviour {
public float moveSpeed;
private float currentMoveSpeed;
private Rigidbody2D myRigidbody;
void Start () {
if (!isLocalPlayer)
{
Destroy(this);
return;
}
myRigidbody = GetComponent<Rigidbody2D>();
}
void Update () {
// Player movement
if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f){
myRigidbody.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * currentMoveSpeed, myRigidbody.velocity.y);
}
if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f){
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, Input.GetAxisRaw("Vertical") * currentMoveSpeed);
}
if (Input.GetAxisRaw("Horizontal") < 0.5f && Input.GetAxisRaw("Horizontal") > -0.5f){
myRigidbody.velocity = new Vector2(0f, myRigidbody.velocity.y);
}
if (Input.GetAxisRaw("Vertical") < 0.5f && Input.GetAxisRaw("Vertical") > -0.5f){
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, 0f);
}
if (Mathf.Abs (Input.GetAxisRaw("Horizontal")) > 0.5f && Mathf.Abs (Input.GetAxisRaw("Vertical")) > 0.5f){
currentMoveSpeed = moveSpeed * .75f;
} else {
currentMoveSpeed = moveSpeed;
}
}