小编Ger*_*rte的帖子

游戏世界中从摄像机到鼠标位置的光线投射

当鼠标位于门(红色区域)时我想做点什么。我正在尝试投射射线,但射线没有击中门,而且我无法找到它到底击中的位置。另外我怎样才能发出Debug.DrawRay这条射线?

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;

if (Physics.Raycast(ray, out hit, Mathf.Infinity)) 
{
    if (hit.collider.tag == "InteractiveDoor")
    {
        doorInteractGameObject.SetActive(true);
    }
    else
    {
        doorInteractGameObject.SetActive(false);
    }
}
Run Code Online (Sandbox Code Playgroud)

统一图像

c# unity-game-engine raycasting

5
推荐指数
1
解决办法
9196
查看次数

如何正确使用 CharacterController.Move() 移动角色

我创建了一个基本的第一人称控制器,但我的问题是当我向前和向侧面移动时,我移动得更快。

我如何在 1 个 Vector3 中添加moveDirectionForwardmoveDirectionSide以便能够使用CharacterController.Move()而不是 CharacterController.SimpleMove()

void MovePlayer() {

    // Get Horizontal and Vertical Input
    float horizontalInput = Input.GetAxis ("Horizontal");
    float verticalInput = Input.GetAxis ("Vertical");

    // Calculate the Direction to Move based on the tranform of the Player
    Vector3 moveDirectionForward = transform.forward * verticalInput * walkSpeed * Time.deltaTime;
    Vector3 moveDirectionSide = transform.right * horizontalInput * walkSpeed * Time.deltaTime;

    // Apply Movement to Player
    myCharacterController.SimpleMove (moveDirectionForward + moveDirectionSide);
Run Code Online (Sandbox Code Playgroud)

c# vector unity-game-engine

2
推荐指数
1
解决办法
1万
查看次数

如何从 Unity 中的自定义编辑器脚本修改序列化变量

我有一个带有 1 个序列化字符串的测试脚本,我试图通过在 TextField 中输入一些内容来访问和修改它,但我不知道将 TextField 分配给什么。

测试脚本:

using UnityEngine;

public class Test : MonoBehaviour
{
    [SerializeField] private string value;

}
Run Code Online (Sandbox Code Playgroud)

测试工具脚本:

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(Test))]
public class TestTool : Editor
{
[ExecuteInEditMode]
public override void OnInspectorGUI()
{

    base.OnInspectorGUI();

    Rect textFieldRect = new Rect(EditorGUILayout.GetControlRect(false, EditorGUIUtility.currentViewWidth));

    EditorGUI.DrawRect(textFieldRect, Color.gray);

    EditorGUI.TextField(textFieldRect, "Type here...");
}
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

在此处输入图片说明

c# user-interface editor unity-game-engine

1
推荐指数
1
解决办法
6241
查看次数