我当前正在使用一个代码,该代码允许我将放置到场景中的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;
}
}
我想使用键盘来旋转对象,而不是使用滚轮来旋转它。
答案 0 :(得分:0)
好吧,而不是
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.GetKey
和Time.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