c# - 即使游戏对象被禁用,也让脚本修改变换位置
问题描述
我正在尝试使用主摄像机位置来显示和消失。例如,如果camera.main.transform.position = (0,2,0);
使对象出现,否则使其消失。
本例中的对象是基本的Cube
. 我开始使用setActive
函数,但事实证明,一旦你拥有特定对象上setActive
的false, Update
函数,它就不会运行。我添加了我正在使用的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class backandforth : MonoBehaviour
{
public float speed = 2.5f;
GameObject targetObject;
// Use this for initialization
void Start()
{
targetObject = GameObject.Find("Cube");
targetObject.SetActive(false);
}
// Update is called once per frame
void Update()
{
//move the cube from (0,0,0)
if (Camera.main.transform.position== new Vector3(0, 2, 0)) {
transform.position = new Vector3(Mathf.PingPong(Time.time * speed, 5), transform.position.y, transform.position.z);
transform.Rotate(0, 0, 5);
}
else
{
targetObject.SetActive(true);
transform.position = new Vector3(Mathf.PingPong(Time.time * speed, 5), transform.position.y, transform.position.z);
transform.Rotate(0, 0, 100);
//gameObject.SetActive(false);
}
}
}
关于我该如何做这件事的任何建议?谢谢!
解决方案
如果我理解正确,更新方法应该是这样的:
void Update()
{
if (Camera.main.transform.position== new Vector3(0, 2, 0)) {
//if the camera.main.transform.position = (0,2,0); make the object appear
targetObject.SetActive(true);
}
else
{
//otherwise make it disappear
targetObject.SetActive(false);
}
}
推荐阅读
- rocket.chat - 无法从 https://10.20.1.98/config.js?room=n...' 加载配置,[TypeError:网络请求失败
- sorting - 使用瑞典字母 ÖÄÅ 批量排序
- validation - 来自另一个 Google 表格的数据验证
- c++ - Unity 在 Unreal Engine 4 C++ 中的“Vector3.Slerp”等价物?
- rest - 如何使用 REST API 客户端工具(邮递员或 jemeter)发送内容类型为:multipart/related 的请求
- python - Auto-Sklearn:是否可以将 imblearn 的指标集成为得分手?
- php - 我如何解决我的 wordpress php 标头更新文件问题
- sql - 在 SQL 的 update 子句中实现 select 语句
- javascript - 如何检查用户是否在没有 request.session 的情况下登录
- android - 内部 recyclerview 内部支架 切割 切割