以下代码实现对物体的一些列操作:
using UnityEngine;
public class sanweixuanzhuandx : MonoBehaviour
{
private float positionX;
private float positionY;
private float positionZ;
private float scalePosiontX = 0.1f;
private float scalePosiontY = 0.1f;
private float scalePosiontZ = 0.1f;
Vector3 scale;
float offset = 0.001f;
float maxSize = 10.0f;
float minSize = 0.001f;
public float speed = 100f;
// Use this for initialization
void Start()
{
scale = this.transform.localScale;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0)) //右键改变x,y,z位置
{
positionX = Input.GetAxis("Mouse X") * scalePosiontX;
positionY = Input.GetAxis("Mouse Y") * scalePosiontY;
positionZ = -Input.GetAxis("Mouse ScrollWheel") * scalePosiontZ;
transform.position += new Vector3(positionX, positionY, positionZ);
}
//鼠标滚轮的效果
//Zoom out
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
if (scale.x <= maxSize)
{
scale.x += offset;
scale.y += offset;
scale.z += offset;
this.transform.localScale = scale;
}
}
//Zoom in
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
if (scale.x > minSize)
{
scale.x -= offset;
scale.y -= offset;
scale.z -= offset;
this.transform.localScale = scale;
}
}
if (Input.GetKey(KeyCode.J))
{
// gameObject.active = false;
transform.Rotate(new Vector3(0, 0, 1));
}
if (Input.GetKey(KeyCode.K))
{
// gameObject.active = false;
transform.Rotate(new Vector3(1, 0, 0));
}
//鼠标左键旋转物体
/*if (Input.GetMouseButton(0))
{
float axis = Input.GetAxis("Mouse X");
this.transform.Rotate(new Vector3(1, 0, 0) * Time.deltaTime * speed * axis);
}*/
/*if (Input.GetMouseButton(2))
{
float axis = Input.GetAxis("Mouse X");
this.transform.Rotate(new Vector3(1, 0, 0) * Time.deltaTime * speed * axis);
}*/
if (Input.GetKey(KeyCode.I))
{
// gameObject.active = false;
transform.Rotate(new Vector3(0, 1, 0));
}
}
}