将脚本继承从可脚本对象添加到游戏对象



我有一个名为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
}
}

目前这是不可能的,因为机枪.cs不再继承单一行为。

我的场景中有一个武器游戏对象,所以我的问题来了: 如何将 MachineGun.cs 脚本作为组件添加到我的武器游戏对象中?或者因为这是不可能的,

我应该如何使用通用的 Weapon.cs 脚本构建武器系统,所有武器都可以从中继承基本函数和字段/变量?

编辑1:为我的帖子提供了代码,并添加了"为什么我想这样做"。

提前感谢!

ScriptableObjects应该主要以面向数据的方式使用,它们对于任务来说非常方便且非常有效。此外,困扰您的MonoBehavior项目是一种非常糟糕(且广泛)的做法。

IMO,你应该有一个带有武器逻辑管理的单一行为,你的可脚本对象应该是你的武器数据(通过在武器单一行为中加载它们来解释),这样你就有机枪,格洛克,武士刀。可编写脚本的对象,具有攻击速度、装填速度、充电器大小、武器模型/纹理、模型命中框引用、yadayadayada等数据。(你可能有一个通用的武器单一行为,但派生一个枪,一个刀片等,用于非常具体的管理,但仍然需要来自ScriptableObjects的数据)

简而言之,您的MonoBehavior定义了使用和交互,而您的ScriptableObjects定义了特征。

ScriptableObject 说:

描述

如果要创建不派生的对象,则可以从中派生的类 需要附加到游戏对象

因此,如果要将此脚本附加到游戏对象,则不应将此脚本附加到游戏对象,或者从"MonoBehavior"派生对象。

你为什么从ScriptableObject中得出它?

最新更新