需要帮助编辑代码以在键盘而不是滚轮上旋转



我目前正在使用一种代码,该代码允许我旋转放置在场景中的 3d 对象,但想使用键盘而不是滚轮来旋转对象,因为它会干扰我用于放大和缩小的脚本。

我已经将滚轮旋转设置为 0,因此它不再旋转对象,但正在努力实现允许我使用键盘的代码。

由于我对 C# 还很陌生,所以我在如何做到这一点方面非常挣扎,并且很难找到学习该语言的免费资源。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GroundPlacementTest : MonoBehaviour {
[SerializeField]
private GameObject placeableObjectPrefab;
[SerializeField]
private KeyCode newObjectHotkey = KeyCode.A;
private GameObject currentPlaceableObject;
private float mouseWheelRotation;
private void Update()
{
    HandleNewObjectHotkey();
    if (currentPlaceableObject != null)
    {
        MoveCurrentObjectToMouse();
        RotateFromMouseWheel();
        ReleaseIfClicked();
    }
}
private void HandleNewObjectHotkey()
{
    if (Input.GetKeyDown(newObjectHotkey))
    {
        if (currentPlaceableObject != null)
        {
            Destroy(currentPlaceableObject);
        }
        else
        {
            currentPlaceableObject = Instantiate(placeableObjectPrefab);
            currentPlaceableObject.layer = LayerMask.NameToLayer("Ignore Raycast");
        }
    }
}
private void MoveCurrentObjectToMouse()
{
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hitInfo;
    if (Physics.Raycast(ray, out hitInfo))
    {
        currentPlaceableObject.transform.position = hitInfo.point;
        currentPlaceableObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
    }
}
private void RotateFromMouseWheel()
{
    Debug.Log(Input.mouseScrollDelta);
    mouseWheelRotation += Input.mouseScrollDelta.y;
    currentPlaceableObject.transform.Rotate(Vector3.up, mouseWheelRotation * 0f);
}
private void RotateFromMouseWheel()
{
    Debug.Log(Input.mouseScrollDelta);
    mouseWheelRotation += Input.mouseScrollDelta.y;
    currentPlaceableObject.transform.Rotate(Vector3.up, mouseWheelRotation * 0f);
}
private void ReleaseIfClicked()
{
    if (Input.GetMouseButtonDown(0))
    {
        currentPlaceableObject.layer = LayerMask.NameToLayer("Default");
        currentPlaceableObject = null;
    }
}

我想使用键盘旋转对象,而不是滚轮旋转对象。

很好,

而不是

private void RotateFromMouseWheel()
{
    Debug.Log(Input.mouseScrollDelta);
    mouseWheelRotation += Input.mouseScrollDelta.y;
    currentPlaceableObject.transform.Rotate(Vector3.up, mouseWheelRotation * 0f);
}

(顺便说一下,你在那里有两次(你可以使用Input.GetKeyDown

// Set in the inspector
public float RotationSpeed;
private void RotateFromMouseWheel()
{
    // whatever key you want
    // this makes one rotation each click
    // if you want it continous see below
    if(!Input.GetKeyDown(KeyCode.R)) return;
    currentPlaceableObject.transform.Rotate(Vector3.up, RotationSpeed);
}

或者在按住按钮时连续旋转Input.GetKeyTime.deltaTime

private void RotateFromMouseWheel()
{
    if(Input.GetKey(KeyCode.R))
    {
        currentPlaceableObject.transform.Rotate(Vector3.up, RotationSpeed * Time.deltaTime);
    }
}

am having a hard time finding free resources to learn the language

例如,您可以查看其中一个教程(这是在Google上查找"Unity键盘输入"时的前三个结果...(并查看Unity API(请参阅上面的链接(

https://www.youtube.com/watch?v=r-hM-yzH_-E

https://www.youtube.com/watch?v=chMxcadsT4U

https://www.youtube.com/watch?v=d0WZf1CmN3k

最新更新