Мне нужно чтобы в мп с photon было оружие! Я написал скрипт. На сцене есть slider то есть это хп. Но когда я стреляю в игрока пишет что метода нету который уже в другом скрипте. метод называется setDamage.
Вот скрипт игрока -
Всё управление писать не стал.
И да почему-то [PunRPC] не пишеться.
using System;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
namespace UnityStandardAssets.Characters.ThirdPerson
{
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(CapsuleCollider))]
[RequireComponent(typeof(Animator))]
public class ThirdPersonCharacter : MonoBehaviour
{
[SerializeField] float m_MovingTurnSpeed = 360;
[SerializeField] float m_StationaryTurnSpeed = 180;
[SerializeField] float m_JumpPower = 12f;
[Range(1f, 4f)][SerializeField] float m_GravityMultiplier = 2f;
[SerializeField] float m_RunCycleLegOffset = 0.2f; //specific to the character in sample assets, will need to be modified to work with others
[SerializeField] float m_MoveSpeedMultiplier = 1f;
[SerializeField] float m_AnimSpeedMultiplier = 1f;
[SerializeField] float m_GroundCheckDistance = 0.1f;
public GameObject cam;
Rigidbody m_Rigidbody;
Animator m_Animator;
bool m_IsGrounded;
float m_OrigGroundCheckDistance;
const float k_Half = 0.5f;
float m_TurnAmount;
float m_ForwardAmount;
Vector3 m_GroundNormal;
float m_CapsuleHeight;
Vector3 m_CapsuleCenter;
CapsuleCollider m_Capsule;
bool m_Crouching;
public GameObject GamePlayUI;
public Slider HPBar;
public float Health;
void Start()
{
GamePlayUI = GameObject.Find("GamePlayUI");
GamePlayUI.transform.GetChild(0).gameObject.SetActive(true);
HPBar = GamePlayUI.transform.GetChild(0).GetComponent<Slider>();
GamePlayUI.SetActive(true);
m_Animator = GetComponent<Animator>();
m_Rigidbody = GetComponent<Rigidbody>();
m_Capsule = GetComponent<CapsuleCollider>();
m_CapsuleHeight = m_Capsule.height;
m_CapsuleCenter = m_Capsule.center;
m_Rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
m_OrigGroundCheckDistance = m_GroundCheckDistance;
}
void Update()
{
HPBar.value = Health;
}
public void setDamage(float damage)
{
Health -= damage;
if (Health <= 0)
{
Application.Quit();
}
}
Вот скрипт оружия -
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;
public class Gun : MonoBehaviour
{
public float damage;
public GameObject owner;
Ray ray;
RaycastHit hit;
public float distance;
public AudioClip clip;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (owner.GetComponent<PhotonView>().isMine)
{
ray = new Ray(owner.GetComponent<ThirdPersonCharacter>().cam.transform.position, owner.GetComponent<ThirdPersonCharacter>().cam.transform.forward);
if (Input.GetMouseButtonDown(0))
{
GetComponent<AudioSource>().PlayOneShot(clip);
if(Physics.Raycast (ray, out hit, distance))
{
Debug.Log("Попал в " + hit.transform.name);
if(hit.transform.tag == "Player")
{
PhotonNetwork.RPC(hit.transform.GetComponent<PhotonView>(), "setDamage", PhotonTargets.All, true, damage);
}
}
}
}
}
}