Question by
Daniel80012345 · Oct 10, 2017 at 12:12 PM ·
spritenetworkflip
Network flip
What is wrong with the flip?
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking;
public class Player : NetworkBehaviour {
private Rigidbody2D _MyRigidbdoy;
private float _SpeedMove = 5;
public static bool _FacingRight;
public GameObject _BulletPrefab;
public Transform _BulletSpawn;
// Use this for initialization
void Start () {
_MyRigidbdoy = GetComponent<Rigidbody2D> ();
_FacingRight = true;
}
// Update is called once per frame
void FixedUpdate () {
if (!isLocalPlayer) {
return;
}
float _Horizontal = Input.GetAxis ("Horizontal");
HandleMovement (_Horizontal);
CmdFlip (_Horizontal);
if (Input.GetKeyDown (KeyCode.Space)) {
CmdFire ();
}
}
void HandleMovement(float _Horizontal){
_MyRigidbdoy.velocity = new Vector2 (_Horizontal * _SpeedMove, _MyRigidbdoy.velocity.y);
}
[Command]
void CmdFlip(float _Horizontal){
RpcFlip (_Horizontal);
}
[ClientRpc]
void RpcFlip(float _Horizontal){
if (_Horizontal > 0 && !_FacingRight || _Horizontal < 0 && _FacingRight) {
_FacingRight = !_FacingRight;
Vector3 _TheScale = transform.localScale;
_TheScale.x *= -1;
transform.localScale = _TheScale;
}
}
[Command]
void CmdFire(){
var _Bullet = (GameObject)Instantiate(
_BulletPrefab,
_BulletSpawn.position,
_BulletSpawn.rotation);
if (_FacingRight)
_Bullet.GetComponent<Rigidbody2D> ().velocity = _Bullet.transform.right * 6;
else
_Bullet.GetComponent<Rigidbody2D> ().velocity = _Bullet.transform.right * -6;
NetworkServer.Spawn (_Bullet);
Destroy(_Bullet, 2.0f);
}
public override void OnStartLocalPlayer()
{
GetComponent<SpriteRenderer> ().color = Color.red;
}
}
Comment
Your answer
Follow this Question
Related Questions
Flipping a sprite with an attached gameobject 0 Answers
How do i get my character to move 3 Answers
How can I rotate a Sprite with the mouse? Like terraria or Starbound 0 Answers
Flip sprite sync Network problem 0 Answers
2D Multiplayer Character Flip 0 Answers