unity3D-基础2----用C#脚本操作基本的组件
一. 鼠标与键盘输入
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class input : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.A)){
Debug.Log("getA");
}
if (Input.GetKeyDown(KeyCode.A))
{
Debug.Log("k down");
}
if(Input.GetKeyUp(KeyCode.A))
{
Debug.Log("key up");
}
// 0-左键1-右键 2-中建
if (Input.GetMouseButton(0))
{
Debug.Log("左键");
}
if (Input.GetMouseButtonUp(0))
{
Debug.Log("左键uo");
}
if (Input.GetMouseButtonDown(0))
{
Debug.Log("左键down");
}
}
}
二. 变换组件移动游戏物体
运动特点:
- 移动的物体会"穿透"场景中的其他物理模型;
- 移动的物体不会受重力影响,到达场景边缘外,不会下落
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class student : MonoBehaviour
{
private Transform m_Transform;
// Use this for initialization
void Start () {
m_Transform=gameObject.GetComponent<Transform>();
}
// Update is called once per frame
void Update () {
// m_Transform.Translate(Vector3.forward * 0.1f /*向前*/, Space.Self /*以自身为坐标系*/);
if (Input.GetKey(KeyCode.W))
{
m_Transform.Translate(Vector3.forward * 0.1f /*向前*/, Space.Self /*以自身为坐标系*/);
}if (Input.GetKey(KeyCode.S))
{
m_Transform.Translate(Vector3.back * 0.1f /*向前*/, Space.Self /*以自身为坐标系*/);
}if (Input.GetKey(KeyCode.A))
{
m_Transform.Translate(Vector3.left * 0.1f /*向前*/, Space.Self /*以自身为坐标系*/);
}if (Input.GetKey(KeyCode.D))
{
m_Transform.Translate(Vector3.right * 0.1f /*向前*/, Space.Self /*以自身为坐标系*/);
}
}
}
三. 刚体简介
作用:添加了刚体左键的游戏物体,就有了重力,就会做足以与落体运动,也就意味着可以像现实生活中的物体一样运动
属性:
- Mass(质量): 单位Kg
- Drag(阻力): 0表示无阻力,值很大时物体会停止运动
- Angular Drag(角阻力): 受到扭曲时的空气阻力,0表示无阻力,值很大时会停止运动
- Use Gravity(使用重力):是否使用重力,默认使用
使用刚体移动的