вот код: но удар не работает именно в мультиплеере
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
public class Player : NetworkBehaviour
{
private Rigidbody2D player;
public float speed;
private Vector2 input;
public Rigidbody2D ball;
private Vector2 moveVelocty;
private void Start()
{
player = GetComponent<Rigidbody2D>();
ball = ball.GetComponent<Rigidbody2D>();
}
private void Update()
{
if (!isLocalPlayer) return;
if (Input.GetKeyDown(KeyCode.Space))
{
float dist = Vector2.Distance(ball.position, player.position);
if (dist <= 5.15f)
{
var heading = ball.position - player.position;
var distance = heading.magnitude;
var direction = heading / distance;
ball.AddForce(direction * Time.deltaTime * 500f, ForceMode2D.Impulse);
}
}
if (!isLocalPlayer) return;
input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
}
private void FixedUpdate()
{
if (!isLocalPlayer) return;
player.MovePosition(player.position + input * speed / 100);
if (Input.GetAxisRaw("Horizontal") > 0)
{
GetComponent<SpriteRenderer>().flipX = true;
}
if (Input.GetAxisRaw("Horizontal") < 0)
{
GetComponent<SpriteRenderer>().flipX = false;
}
}
}
using System.Collections.Generic;
using UnityEngine;
using Mirror;
public class Player : NetworkBehaviour
{
private Rigidbody2D player;
public float speed;
private Vector2 input;
public Rigidbody2D ball;
private Vector2 moveVelocty;
private void Start()
{
player = GetComponent<Rigidbody2D>();
ball = ball.GetComponent<Rigidbody2D>();
}
private void Update()
{
if (!isLocalPlayer) return;
if (Input.GetKeyDown(KeyCode.Space))
{
float dist = Vector2.Distance(ball.position, player.position);
if (dist <= 5.15f)
{
var heading = ball.position - player.position;
var distance = heading.magnitude;
var direction = heading / distance;
ball.AddForce(direction * Time.deltaTime * 500f, ForceMode2D.Impulse);
}
}
if (!isLocalPlayer) return;
input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
}
private void FixedUpdate()
{
if (!isLocalPlayer) return;
player.MovePosition(player.position + input * speed / 100);
if (Input.GetAxisRaw("Horizontal") > 0)
{
GetComponent<SpriteRenderer>().flipX = true;
}
if (Input.GetAxisRaw("Horizontal") < 0)
{
GetComponent<SpriteRenderer>().flipX = false;
}
}
}