![](/img/trans.png)
[英]Error in unity(Script Inspector 3): GetBool is not allowed to be called from a ScriptableObject constructor
[英]Add script inheriting from scriptableobject to game object
我有一个名为Weapon.cs的脚本,这是一个可编写脚本的对象。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class Weapon : ScriptableObject {
protected Camera _mainCamera;
protected Transform _weaponTransform;
protected int _damage;
protected int _fireRatePerSecond;
protected bool _isAutomaticWeapon;
protected void FireWeapon()
{
//if the weapon is automatic
if (_isAutomaticWeapon)
{
ShootRapidFireOnMouseHold();
}
//if the weapon is semi automatic
else
{
ShootSingleBulletOnMouseClick();
}
}
protected void MoveWeaponWithCamera(Transform weaponTransform)
{
_weaponTransform.rotation = _mainCamera.transform.rotation; //temporary way of making sure the gun moves with the camera
}
protected void ShootSingleBulletOnMouseClick()
{
if (Input.GetKeyDown(KeyCode.Mouse0)) //if left mouse button is clicked
{
CastRay();
}
}
protected void ShootRapidFireOnMouseHold()
{
if (Input.GetKey(KeyCode.Mouse0)) //if left mouse button is held down
{
CastRay(); //rapid fire
//PlayShootAnimation();
}
}
protected void CastRay()
{
RaycastHit hit;
if (Physics.Raycast(_mainCamera.transform.position, _mainCamera.transform.forward, out hit, 100))
{
Debug.Log(hit.transform.name);
}
}
//protected abstract void PlayShootAnimation();
}
我有另一个名为MachineGun.cs的脚本,它是从Weapon.cs继承的,因此是间接从可脚本编写的对象继承的。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MachineGun : Weapon{
private GameObject _machineGunBarrel;
//private float _animationVelocity;
// Use this for initialization
void Start () {
//initialize the basic properties of a weapon
_damage = 7;
_fireRatePerSecond = 10;
_isAutomaticWeapon = true;
_weaponTransform = GameObject.Find("Weapon_MachineGun").transform;
_machineGunBarrel = GameObject.Find("machinegun_p1");
_mainCamera = Camera.main;
}
// Update is called once per frame
void Update () {
MoveWeaponWithCamera(_weaponTransform);
FireWeapon();
}
//protected override void Shoot()
//{
// RaycastHit hit;
// if(Physics.Raycast(_mainCamera.transform.position, _mainCamera.transform.forward, out hit, 100))
// {
// Debug.Log(hit.transform.name);
// }
//}
void PlayShootAnimation()
{
_machineGunBarrel.transform.RotateAround(_machineGunBarrel.transform.up, _fireRatePerSecond * Time.deltaTime);
PlayShootAnimation(); //play the shoot animation of the gun
}
}
由于MachineGun.cs不再从monobehaviour继承,目前这是不可能的。
我的场景中有一个武器游戏对象,所以这是我的问题:如何将MachineGun.cs脚本作为组件添加到我的武器游戏对象中? 或者由于这是不可能的,
我应该如何使用通用的Weapon.cs脚本构建武器系统,所有武器都可以从中继承基本功能和字段/变量?
编辑1:向我的帖子提供了代码,并添加了“我为什么要这样做”。
提前致谢!
描述
如果要创建不需要附加到游戏对象上的对象,可以从中派生一个类。
因此,如果您不希望将此脚本附加到游戏对象,则不要将该脚本附加到游戏对象,也不要从“ MonoBehaviour”派生您的对象。
为什么要从ScriptableObject
派生它?
ScriptableObjects应该主要以面向数据的方式使用,它们非常方便且非常高效。 另外,困扰您的MonoBehaviour项目是一种非常糟糕的做法(而且广为流传)。
海事组织 ,你应该有武器的逻辑管理MonoBehaviour和你ScriptableObjects应该是你的武器的数据(这是由您支招MonoBehaviour装起来解释),其中有这样的数据,你有机枪,格洛克,武士刀.. 可编程对象 ,攻击速度,重装速度,充电器大小,武器模型/纹理,对模型Hitbox的引用,yadayadayada。 (您可能具有通用的武器MonoBehaviour ,但派生了Gun Gun,Blade等用于非常特定的管理,但仍需要ScriptableObjects的数据)
简而言之,您的MonoBehaviours定义用法和交互,而ScriptableObjects定义特征
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.