C# 如何在游戏开始时启动的预置上在inspector中分配游戏对象变量?
我有一张纸条可以控制武器的上下,但它需要分配一些游戏对象才能工作,但我所有的武器都是在游戏开始时生成的,因为我使用的是程序生成的地图(DunGen asset)。 我如何分配它们 我的代码:C# 如何在游戏开始时启动的预置上在inspector中分配游戏对象变量?,c#,unity3d,C#,Unity3d,我有一张纸条可以控制武器的上下,但它需要分配一些游戏对象才能工作,但我所有的武器都是在游戏开始时生成的,因为我使用的是程序生成的地图(DunGen asset)。 我如何分配它们 我的代码: using System.Collections; using System.Collections.Generic; using UnityEngine; public class WeaponsPickup : MonoBehaviour { public Rigidbody rb;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeaponsPickup : MonoBehaviour
{
public Rigidbody rb;
public BoxCollider coll;
public Transform player, gunContainer, fpsCam;
public float pickUpRange;
public float dropForwardForce, dropUpwardForce;
public bool equipped;
public static bool slotFull;
private void Start()
{
//Setup
if (!equipped)
{
rb.isKinematic = false;
coll.isTrigger = false;
}
if (equipped)
{
rb.isKinematic = true;
coll.isTrigger = true;
slotFull = true;
}
}
private void Update()
{
//Check if player is in range and "E" is pressed
Vector3 distanceToPlayer = player.position - transform.position;
if (!equipped && distanceToPlayer.magnitude <= pickUpRange && Input.GetKeyDown(KeyCode.E) && !slotFull) PickUp();
//Drop if equipped and "Q" is pressed
if (equipped && Input.GetKeyDown(KeyCode.Q)) Drop();
}
private void PickUp()
{
equipped = true;
slotFull = true;
//Make weapon a child of the camera and move it to default position
transform.SetParent(gunContainer);
transform.localPosition = Vector3.zero;
transform.localRotation = Quaternion.Euler(Vector3.zero);
transform.localScale = Vector3.one;
//Make Rigidbody kinematic and BoxCollider a trigger
rb.isKinematic = true;
coll.isTrigger = true;
}
private void Drop()
{
equipped = false;
slotFull = false;
//Set parent to null
transform.SetParent(null);
//Make Rigidbody not kinematic and BoxCollider normal
rb.isKinematic = false;
coll.isTrigger = false;
//Gun carries momentum of player
rb.velocity = player.GetComponent<Rigidbody>().velocity;
//AddForce
rb.AddForce(fpsCam.forward * dropForwardForce, ForceMode.Impulse);
rb.AddForce(fpsCam.up * dropUpwardForce, ForceMode.Impulse);
//Add random rotation
float random = Random.Range(-1f, 1f);
rb.AddTorque(new Vector3(random, random, random) * 10);
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共级武器装备:单一行为
{
公共刚体;
公共BoxColl;
公共变换播放器、枪械容器、fpsCam;
公共浮式皮卡;
公共浮动dropForwardForce,dropUpwardForce;
配备公共厕所;
公共静态bool-slotFull;
私有void Start()
{
//设置
如果(!配备)
{
rb.iskinetic=false;
coll.isTrigger=假;
}
如有配备
{
rb.iskinetic=true;
coll.isTrigger=真;
slotFull=true;
}
}
私有void更新()
{
//检查播放机是否在范围内,并按下“E”
Vector3 distanceToPlayer=player.position-transform.position;
如果(!employed&&distance toplayer.magnity首先指定目标武器(或需要编写脚本的任何对象)的标记,则类似于:
public Transform _weapon;
void Start() {
_weapon = GameObject.FindGameObjectWithTag("weapon").GetComponent<_weapon>();
}
public-Transform\u武器;
void Start(){
_武器=游戏对象。FindGameObjectWithTag(“武器”).GetComponent();
}
应该对您有用。首先指定目标武器(或您需要编写脚本的任何对象)的标签,然后指定如下内容:
public Transform _weapon;
void Start() {
_weapon = GameObject.FindGameObjectWithTag("weapon").GetComponent<_weapon>();
}
public-Transform\u武器;
void Start(){
_武器=游戏对象。FindGameObjectWithTag(“武器”).GetComponent();
}
应该对你有用。我认为你应该在你的游戏对象中添加一个标签来繁殖,工作就完成了
例如,如果你的敌人身上有一个需要繁殖的标记“敌人”,你可以使用该标记而不是像那样的公共变量
GameObject enemy = GameObject.FindGameObjectWithTag("enemy");
然后当然可以访问这样的组件
Rigidbody rb = enemy.getComponent<Rigidbody>();
Rigidbody rb=敌方.getComponent();
有了这个,你还可以决定何时找到该对象,因为如果没有标记为“敌人”的对象,它将不会分配游戏对象敌人
,并且它将为空,直到生成一个对象(如果你将其放入更新()中)我认为你应该在你的游戏对象中添加一个标签来繁殖,工作就完成了
例如,如果你的敌人身上有一个需要繁殖的标记“敌人”,你可以使用该标记而不是像那样的公共变量
GameObject enemy = GameObject.FindGameObjectWithTag("enemy");
然后当然可以访问这样的组件
Rigidbody rb = enemy.getComponent<Rigidbody>();
Rigidbody rb=敌方.getComponent();
有了这个,你还可以决定何时找到该对象,因为如果没有标记为“敌人”的对象,它将不会分配游戏对象敌人
,并且它将为空,直到生成一个对象(如果你将其放入更新()中)应该可以工作,但效率不高。实例化对象时,您可以获得一个可以分配的引用,避免调用Find…
。是的,但他的问题是他想在运行时分配它们……但是如果您知道一个更有效的方法,那么我很想知道!我知道,但再一次:OP在游戏的star期间生成武器t、 他应该在生成它们之后直接分配它们,因为FindGameObjectWithTag
在生成它们之前不起作用。所以为什么不使用Instantiate
返回的引用呢?:P但是该生成代码丢失了,所以我不能建议具体的东西。不完全是我需要的,我需要从另一个游戏对象添加组件。好的,我明白你的意思了…嗯,也许我们需要一个事件或类似的东西来通知我们它们何时被实例化。似乎实例化调用必须在另一个scrpit?中。应该可以,但效率不高。当实例化对象时,你可以分配一个引用,避免调用Find…
是的但他的问题是,他想在运行时分配它们……但如果你知道一个更有效的方法,那么我很想知道!我知道,但再一次:OP会在游戏开始时生成武器。他应该在生成武器后直接分配武器,因为FindGameObjectWithTag
在生成武器之前不起作用。所以为什么不使用referencee那<代码>实例化<代码>返回?:P但是那生成代码丢失了,所以我不能提出具体的建议。不完全是我需要的,我需要从另一个游戏对象中添加组件。对,我明白你的意思了…嗯,也许我们需要一个事件或类似的东西来通知我们它们被实例化的时间。似乎是实例化呼叫必须在另一个scrpit中?