触发对象时将文本(消息)放下?
Bring text (message) down when triggering an object?
我正在做一个图书馆的交互式地图,我想要这样当玩家接触并触发地图周围的不可见物体时,显示消息的文本会出现在他的视野中,然后他可以按回车键,它会恢复原状,当我尝试 运行 代码时,我的控制台显示“无法修改 'Transform.position' 的 return 值,因为它不是变量”。也许这与文本是'RectTranform'而不是Transform有关,我不知道
这里是触发代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//I misspelled collision but that doesn't matter
public class collsiion : MonoBehaviour
{
void OnTriggerEnter(){
FindObjectOfType<messageToggle>().goingdown();
}
}
这里是消息移动代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class messageToggle : MonoBehaviour
{
public RectTransform _message;
public bool gru = false;
public float brumium = 50f;
public void goingdown(){
_message.posY =- brumium;
gru = true;
}
void Update()
{
if(gru == true){
if(Input.GetKey("f")){
//by the way does anyone know how to put Enter key as an input
_message.position.y =+ brumium;
}
}
}
}
因为 Transform.position 是 属性 并且在访问时,将返回一个副本,因为它是一个结构。为避免抛出错误,您需要重新分配位置向量:
_message.position = new Vector3(_message.position.x, _message.position.y + brumium, _message.position.z);
或者,您可以翻译它:(https://docs.unity3d.com/ScriptReference/Transform.Translate.html)
我正在做一个图书馆的交互式地图,我想要这样当玩家接触并触发地图周围的不可见物体时,显示消息的文本会出现在他的视野中,然后他可以按回车键,它会恢复原状,当我尝试 运行 代码时,我的控制台显示“无法修改 'Transform.position' 的 return 值,因为它不是变量”。也许这与文本是'RectTranform'而不是Transform有关,我不知道
这里是触发代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//I misspelled collision but that doesn't matter
public class collsiion : MonoBehaviour
{
void OnTriggerEnter(){
FindObjectOfType<messageToggle>().goingdown();
}
}
这里是消息移动代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class messageToggle : MonoBehaviour
{
public RectTransform _message;
public bool gru = false;
public float brumium = 50f;
public void goingdown(){
_message.posY =- brumium;
gru = true;
}
void Update()
{
if(gru == true){
if(Input.GetKey("f")){
//by the way does anyone know how to put Enter key as an input
_message.position.y =+ brumium;
}
}
}
}
因为 Transform.position 是 属性 并且在访问时,将返回一个副本,因为它是一个结构。为避免抛出错误,您需要重新分配位置向量:
_message.position = new Vector3(_message.position.x, _message.position.y + brumium, _message.position.z);
或者,您可以翻译它:(https://docs.unity3d.com/ScriptReference/Transform.Translate.html)