Input类,Vector3实例
生活随笔
收集整理的這篇文章主要介紹了
Input类,Vector3实例
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
這篇文章主要是記錄聽網(wǎng)課的一些記錄,不一定是完全的Input的有關(guān)的,希望能給大家有幫助。
(1)
using System.Collections; using System.Collections.Generic; using UnityEngine;public class InputOne : MonoBehaviour {float xSpeed = 2.0f;float ySpeed = 2.0f;GameObject cube;// Start is called before the first frame updatevoid Start(){cube = GameObject.Find("Cube");}// Update is called once per framevoid Update(){if (Input.GetMouseButtonDown(0)){Debug.Log("左鼠標(biāo)被按下");}if(Input.GetMouseButtonDown(1)){Debug.Log("右鼠標(biāo)被按下");}// Debug.Log(Input.mousePosition);float v = xSpeed * Input.GetAxis("Mouse X");// Input.GetAxis("Mouse X")這個(gè)是鼠標(biāo)這一幀與上一幀在X方向的偏移量float h = ySpeed * Input.GetAxis("Mouse Y");// Input.GetAxis("Mouse Y")這個(gè)是鼠標(biāo)這一幀與上一幀在Y方向的偏移量cube.transform.Rotate(v, h, 0.0f);//這里關(guān)于Speed我的理解是這樣的:不要理解為速度,看成某一個(gè)固定的值就好,這樣可能會(huì)理解的更好。} }//Touch類的補(bǔ)充 //touchCount表示觸碰的次數(shù)。 //touches返回所有的觸碰信息。 //GetTouch(int index)里面的元素是觸碰的順序。 /*Touch對象的生命周期的結(jié)束并不是手指離開屏幕后立刻銷毀 如果一根手指在同一位置快速點(diǎn)擊,則視作同一Touch對象 tapCount為Touch對象所對應(yīng)的手指點(diǎn)擊屏幕的次數(shù) myTouch.tapCount*/(2)
using System.Collections; using System.Collections.Generic; using UnityEngine;public class Vector: MonoBehaviour {private Vector3 a;private Vector3 b;// Start is called before the first frame updatevoid Start(){a = new Vector3(3, 2, 1);b = new Vector3(1.5f, 1.0f, 0.5f);}// Update is called once per framevoid Update(){}//向量之間的點(diǎn)乘與叉乘。private void OnGUI(){float c = Vector3.Dot(a, b);//c為a,b的點(diǎn)乘;//求角度,Acos為cos的反函數(shù)。float angle = Mathf.Acos(Vector3.Dot(a.normalized, b.normalized)) * Mathf.Rad2Deg;//標(biāo)簽,GUI控件GUILayout.Label("兩者的點(diǎn)乘為:" + c);GUILayout.Label("兩者的角度為:" + angle);//用兩個(gè)向量的叉乘來求角度,所謂叉乘就是a的模乘以b的模乘上兩者角度的sin值。float cc = Mathf.Asin(Vector3.Distance(a.normalized, b.normalized)) * Mathf.Rad2Deg;GUILayout.Label("兩者的角度為:" + cc);} }上面這段代碼主要是介紹向量之間的點(diǎn)乘和叉乘。
(3)
using System.Collections; using System.Collections.Generic; using UnityEngine;public class Vector22 : MonoBehaviour {// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){}private void OnGUI(){if (GUILayout.Button("creatCube")){//創(chuàng)建一個(gè)實(shí)例的cube,并且賦予其材質(zhì)為紅色,最后加了一個(gè)剛體組件,有了這個(gè)組件的話物體就會(huì)受重力GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);obj.GetComponent<Renderer>().material.color = Color.red;obj.transform.position = new Vector3(0.0f, 10.0f, 0.0f);obj.AddComponent<Rigidbody>();}if(GUILayout .Button("creatshpere")){GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);sphere.AddComponent<Rigidbody>();sphere.GetComponent<Renderer>().material.color = Color.blue;}}}這段代碼的話就是通過代碼來創(chuàng)建物體,然后給物體加想要的組件,實(shí)現(xiàn)顏色的變化。
希望能夠?qū)Υ蠹矣袔椭?#xff01;
總結(jié)
以上是生活随笔為你收集整理的Input类,Vector3实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Unity之Math等方法的使用
- 下一篇: Android小结(1)