В настоящее время у меня есть игра, в которой 2 игрока имеют Railguns, и всякий раз, когда один игрок стреляет другому, я хочу, чтобы они обоих возродились. Тем не менее, в настоящее время только игрок, который стрелял другому игроку, восстанавливается, и есть ошибка (я не знаю, вызывает ли это ошибка). Что мне изменить, чтобы заставить код работать? Вот 2-кодовые файлы, которые соответствуют: < /p>
//player spawner
using Fusion;
using UnityEngine;
using System.Collections.Generic;
public class PlayerSpawner : SimulationBehaviour, IPlayerJoined
{
public GameObject playerPrefab;
public Transform[] spawnPoints;
private List players = new List();
private List playerRefs = new List();
public void PlayerJoined(PlayerRef player)
{
if (Runner.LocalPlayer != player) return;
int spawnIndex = Mathf.Clamp(player.PlayerId - 1, 0, spawnPoints.Length - 1);
Transform spawnPoint = spawnPoints[spawnIndex];
players.Add(Runner.Spawn(playerPrefab, spawnPoint.position, Quaternion.identity, player));
playerRefs.Add(player);
}
public void RespawnPlayers()
{
for (int i = 0; i < players.Count; i++)
{
if (Runner.LocalPlayer != playerRefs) continue;
int spawnIndex = Mathf.Clamp(playerRefs.PlayerId - 1, 0, spawnPoints.Length - 1);
Transform spawnPoint = spawnPoints[spawnIndex];
Runner.Despawn(players);
players.RemoveAt(i);
players.Add(Runner.Spawn(playerPrefab, spawnPoint.position, Quaternion.identity, playerRefs));
}
}
}
< /code>
//combat script
using Fusion;
using UnityEngine;
using DG.Tweening;
using System.Collections;
using UnityEngine.InputSystem.HID;
public class Combat : NetworkBehaviour
{
public GameObject railgun;
private Transform railgunParticle;
private Transform smokingParticle;
public LineRenderer lineRenderer;
public Transform cameraTransform;
public AudioClip chargeSound;
public AudioClip fireSound;
public AudioSource audioSource;
public float shootCooldown;
private bool canShoot = true;
public LayerMask canBeHit;
// Start is called once before the first execution of Update after the MonoBehaviour is created
public override void Spawned()
{
railgunParticle = railgun.transform.Find("ChargingParticle");
railgunParticle.gameObject.GetComponent().Stop();
smokingParticle = railgun.transform.Find("SmokingParticle");
smokingParticle.gameObject.GetComponent().Stop();
}
// Update is called once per frame
void Update()
{
if (smokingParticle == null) return;
if (GetComponentInParent().Camera == null)
{
return;
}
if (cameraTransform == null)
{
cameraTransform = GetComponentInParent().Camera.gameObject.transform;
}
if (Input.GetKeyDown(KeyCode.Mouse0))
{ //railgunParticle.gameObject.GetComponent().Stop();
if (canShoot)
{
RpcPlayChargingParticle();
canShoot = false;
audioSource.clip = chargeSound;
audioSource.Play();
Invoke(nameof(RpcShoot), 1f);
Invoke(nameof(RpcResetShootCooldown), shootCooldown);
}
}
}
[Rpc(sources: RpcSources.InputAuthority, targets: RpcTargets.All)]
private void RpcResetShootCooldown()
{
canShoot = true;
//railgun.transform.DOLocalRotate(new Vector3(0f, 90f, 90f), 0.15f);
smokingParticle.gameObject.GetComponent().Stop();
}
[Rpc(sources: RpcSources.InputAuthority, targets: RpcTargets.All)]
private void RpcShoot()
{
railgunParticle.gameObject.GetComponent().Stop();
audioSource.Pause();
audioSource.clip = fireSound;
audioSource.Play();
RaycastHit hit;
if (Physics.Raycast(cameraTransform.position, cameraTransform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity))
{
RpcDoLaser(hit.point, true);
if (hit.transform.gameObject.layer == 7)
{
RpcRespawnAll();
}
}
else
{
RpcDoLaser(hit.point, false);
}
RpcShootAnim();
}
[Rpc (sources: RpcSources.InputAuthority, targets: RpcTargets.All)]
private void RpcShootAnim()
{
smokingParticle.gameObject.GetComponent().Play();
}
IEnumerator LaserFade()
{
yield return new WaitForSeconds(0.05f);
lineRenderer.material.SetColor("_Color", new Color(1f, 1f, 1f, 0.9f));
yield return new WaitForSeconds(0.05f);
lineRenderer.material.SetColor("_Color", new Color(1f, 1f, 1f, 0.8f));
yield return new WaitForSeconds(0.05f);
lineRenderer.material.SetColor("_Color", new Color(1f, 1f, 1f, 0.7f));
yield return new WaitForSeconds(0.05f);
lineRenderer.material.SetColor("_Color", new Color(1f, 1f, 1f, 0.6f));
yield return new WaitForSeconds(0.05f);
lineRenderer.material.SetColor("_Color", new Color(1f, 1f, 1f, 0.5f));
yield return new WaitForSeconds(0.05f);
lineRenderer.material.SetColor("_Color", new Color(1f, 1f, 1f, 0.4f));
yield return new WaitForSeconds(0.05f);
lineRenderer.material.SetColor("_Color", new Color(1f, 1f, 1f, 0.3f));
yield return new WaitForSeconds(0.05f);
lineRenderer.material.SetColor("_Color", new Color(1f, 1f, 1f, 0.2f));
yield return new WaitForSeconds(0.05f);
lineRenderer.material.SetColor("_Color", new Color(1f, 1f, 1f, 0.1f));
yield return new WaitForSeconds(0.05f);
lineRenderer.gameObject.SetActive(false);
lineRenderer.material.SetColor("_Color", new Color(1f, 1f, 1f, 1f));
}
[Rpc (sources:RpcSources.InputAuthority, targets: RpcTargets.All)]
//bro
private void RpcPlayChargingParticle()
{
railgunParticle.gameObject.GetComponent().Play();
}
[Rpc (sources:RpcSources.InputAuthority, targets: RpcTargets.All)]
private void RpcDoLaser(Vector3 WhatHit, bool DidjaHit)
{
lineRenderer.gameObject.SetActive(true);
if (DidjaHit)
{
lineRenderer.gameObject.transform.position = railgun.transform.position + new Vector3(0.0295999981f, 0.250699997f, 0);
lineRenderer.SetPosition(0, railgun.transform.position + new Vector3(0.0295999981f, 0.250699997f, 0));
lineRenderer.SetPosition(1, WhatHit);
StartCoroutine(LaserFade());
}
else
{
lineRenderer.gameObject.transform.position = railgun.transform.position + new Vector3(0.0295999981f, 0.250699997f, 0);
lineRenderer.SetPosition(0, railgun.transform.position + new Vector3(0.0295999981f, 0.250699997f, 0));
lineRenderer.SetPosition(1, cameraTransform.position + cameraTransform.forward * 500f);
StartCoroutine(LaserFade());
}
}
[Rpc (sources:RpcSources.All, targets: RpcTargets.All)]
private void RpcRespawnAll()
{
Runner.gameObject.GetComponent().RespawnPlayers();
}
}
< /code>
ошибка: < /p>
InvalidoperationException: поведение не инициализировано: объект не установлен. Fusion \ Fusion.Runtime \ Components \ NetworkBehaviourutils.cs: 298
at combat.rpcshootanim () [0x0002a] в Assets \ scripts \ combat.cs: 98
at combat.rpcshoot () [0x000c4] in arsets \ scripts \ combat.csshoot (). /> < /blockquote>
Я уже пробовал много вещей, однако все они либо ничего не сделали, либо не сделали его хуже, поэтому помощь ценится. < /p>
Подробнее здесь: https://stackoverflow.com/questions/796 ... to-respawn
Как заставить обоих игроков возродиться? ⇐ C#
Место общения программистов C#
1750047553
Anonymous
В настоящее время у меня есть игра, в которой 2 игрока имеют Railguns, и всякий раз, когда один игрок стреляет другому, я хочу, чтобы они обоих возродились. Тем не менее, в настоящее время только игрок, который стрелял другому игроку, восстанавливается, и есть ошибка (я не знаю, вызывает ли это ошибка). Что мне изменить, чтобы заставить код работать? Вот 2-кодовые файлы, которые соответствуют: < /p>
//player spawner
using Fusion;
using UnityEngine;
using System.Collections.Generic;
public class PlayerSpawner : SimulationBehaviour, IPlayerJoined
{
public GameObject playerPrefab;
public Transform[] spawnPoints;
private List players = new List();
private List playerRefs = new List();
public void PlayerJoined(PlayerRef player)
{
if (Runner.LocalPlayer != player) return;
int spawnIndex = Mathf.Clamp(player.PlayerId - 1, 0, spawnPoints.Length - 1);
Transform spawnPoint = spawnPoints[spawnIndex];
players.Add(Runner.Spawn(playerPrefab, spawnPoint.position, Quaternion.identity, player));
playerRefs.Add(player);
}
public void RespawnPlayers()
{
for (int i = 0; i < players.Count; i++)
{
if (Runner.LocalPlayer != playerRefs[i]) continue;
int spawnIndex = Mathf.Clamp(playerRefs[i].PlayerId - 1, 0, spawnPoints.Length - 1);
Transform spawnPoint = spawnPoints[spawnIndex];
Runner.Despawn(players[i]);
players.RemoveAt(i);
players.Add(Runner.Spawn(playerPrefab, spawnPoint.position, Quaternion.identity, playerRefs[i]));
}
}
}
< /code>
//combat script
using Fusion;
using UnityEngine;
using DG.Tweening;
using System.Collections;
using UnityEngine.InputSystem.HID;
public class Combat : NetworkBehaviour
{
public GameObject railgun;
private Transform railgunParticle;
private Transform smokingParticle;
public LineRenderer lineRenderer;
public Transform cameraTransform;
public AudioClip chargeSound;
public AudioClip fireSound;
public AudioSource audioSource;
public float shootCooldown;
private bool canShoot = true;
public LayerMask canBeHit;
// Start is called once before the first execution of Update after the MonoBehaviour is created
public override void Spawned()
{
railgunParticle = railgun.transform.Find("ChargingParticle");
railgunParticle.gameObject.GetComponent().Stop();
smokingParticle = railgun.transform.Find("SmokingParticle");
smokingParticle.gameObject.GetComponent().Stop();
}
// Update is called once per frame
void Update()
{
if (smokingParticle == null) return;
if (GetComponentInParent().Camera == null)
{
return;
}
if (cameraTransform == null)
{
cameraTransform = GetComponentInParent().Camera.gameObject.transform;
}
if (Input.GetKeyDown(KeyCode.Mouse0))
{ //railgunParticle.gameObject.GetComponent().Stop();
if (canShoot)
{
RpcPlayChargingParticle();
canShoot = false;
audioSource.clip = chargeSound;
audioSource.Play();
Invoke(nameof(RpcShoot), 1f);
Invoke(nameof(RpcResetShootCooldown), shootCooldown);
}
}
}
[Rpc(sources: RpcSources.InputAuthority, targets: RpcTargets.All)]
private void RpcResetShootCooldown()
{
canShoot = true;
//railgun.transform.DOLocalRotate(new Vector3(0f, 90f, 90f), 0.15f);
smokingParticle.gameObject.GetComponent().Stop();
}
[Rpc(sources: RpcSources.InputAuthority, targets: RpcTargets.All)]
private void RpcShoot()
{
railgunParticle.gameObject.GetComponent().Stop();
audioSource.Pause();
audioSource.clip = fireSound;
audioSource.Play();
RaycastHit hit;
if (Physics.Raycast(cameraTransform.position, cameraTransform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity))
{
RpcDoLaser(hit.point, true);
if (hit.transform.gameObject.layer == 7)
{
RpcRespawnAll();
}
}
else
{
RpcDoLaser(hit.point, false);
}
RpcShootAnim();
}
[Rpc (sources: RpcSources.InputAuthority, targets: RpcTargets.All)]
private void RpcShootAnim()
{
smokingParticle.gameObject.GetComponent().Play();
}
IEnumerator LaserFade()
{
yield return new WaitForSeconds(0.05f);
lineRenderer.material.SetColor("_Color", new Color(1f, 1f, 1f, 0.9f));
yield return new WaitForSeconds(0.05f);
lineRenderer.material.SetColor("_Color", new Color(1f, 1f, 1f, 0.8f));
yield return new WaitForSeconds(0.05f);
lineRenderer.material.SetColor("_Color", new Color(1f, 1f, 1f, 0.7f));
yield return new WaitForSeconds(0.05f);
lineRenderer.material.SetColor("_Color", new Color(1f, 1f, 1f, 0.6f));
yield return new WaitForSeconds(0.05f);
lineRenderer.material.SetColor("_Color", new Color(1f, 1f, 1f, 0.5f));
yield return new WaitForSeconds(0.05f);
lineRenderer.material.SetColor("_Color", new Color(1f, 1f, 1f, 0.4f));
yield return new WaitForSeconds(0.05f);
lineRenderer.material.SetColor("_Color", new Color(1f, 1f, 1f, 0.3f));
yield return new WaitForSeconds(0.05f);
lineRenderer.material.SetColor("_Color", new Color(1f, 1f, 1f, 0.2f));
yield return new WaitForSeconds(0.05f);
lineRenderer.material.SetColor("_Color", new Color(1f, 1f, 1f, 0.1f));
yield return new WaitForSeconds(0.05f);
lineRenderer.gameObject.SetActive(false);
lineRenderer.material.SetColor("_Color", new Color(1f, 1f, 1f, 1f));
}
[Rpc (sources:RpcSources.InputAuthority, targets: RpcTargets.All)]
//bro
private void RpcPlayChargingParticle()
{
railgunParticle.gameObject.GetComponent().Play();
}
[Rpc (sources:RpcSources.InputAuthority, targets: RpcTargets.All)]
private void RpcDoLaser(Vector3 WhatHit, bool DidjaHit)
{
lineRenderer.gameObject.SetActive(true);
if (DidjaHit)
{
lineRenderer.gameObject.transform.position = railgun.transform.position + new Vector3(0.0295999981f, 0.250699997f, 0);
lineRenderer.SetPosition(0, railgun.transform.position + new Vector3(0.0295999981f, 0.250699997f, 0));
lineRenderer.SetPosition(1, WhatHit);
StartCoroutine(LaserFade());
}
else
{
lineRenderer.gameObject.transform.position = railgun.transform.position + new Vector3(0.0295999981f, 0.250699997f, 0);
lineRenderer.SetPosition(0, railgun.transform.position + new Vector3(0.0295999981f, 0.250699997f, 0));
lineRenderer.SetPosition(1, cameraTransform.position + cameraTransform.forward * 500f);
StartCoroutine(LaserFade());
}
}
[Rpc (sources:RpcSources.All, targets: RpcTargets.All)]
private void RpcRespawnAll()
{
Runner.gameObject.GetComponent().RespawnPlayers();
}
}
< /code>
ошибка: < /p>
InvalidoperationException: поведение не инициализировано: объект не установлен. Fusion \ Fusion.Runtime \ Components \ NetworkBehaviourutils.cs: 298
at combat.rpcshootanim () [0x0002a] в Assets \ scripts \ combat.cs: 98
at combat.rpcshoot () [0x000c4] in arsets \ scripts \ combat.csshoot (). /> < /blockquote>
Я уже пробовал много вещей, однако все они либо ничего не сделали, либо не сделали его хуже, поэтому помощь ценится. < /p>
Подробнее здесь: [url]https://stackoverflow.com/questions/79662706/how-do-i-get-both-players-to-respawn[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия