Input类,Vector3实例

这篇文章主要是记录听网课的一些记录,不一定是完全的Input的有关的,希望能给大家有帮助。

(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 update
    void Start()
    {
        cube = GameObject.Find("Cube");
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("左鼠标被按下");
        }
        if(Input.GetMouseButtonDown(1))
        {
            Debug.Log("右鼠标被按下");
        }
        //  Debug.Log(Input.mousePosition);
        float v = xSpeed * Input.GetAxis("Mouse X");// Input.GetAxis("Mouse X")这个是鼠标这一帧与上一帧在X方向的偏移量
        float h = ySpeed * Input.GetAxis("Mouse Y");// Input.GetAxis("Mouse Y")这个是鼠标这一帧与上一帧在Y方向的偏移量
        cube.transform.Rotate(v, h, 0.0f);
        //这里关于Speed我的理解是这样的:不要理解为速度,看成某一个固定的值就好,这样可能会理解的更好。
    }
}

//Touch类的补充
//touchCount表示触碰的次数。
//touches返回所有的触碰信息。
//GetTouch(int index)里面的元素是触碰的顺序。
/*Touch对象的生命周期的结束并不是手指离开屏幕后立刻销毁
如果一根手指在同一位置快速点击,则视作同一Touch对象
tapCount为Touch对象所对应的手指点击屏幕的次数
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 update
    void Start()
    {
        a = new Vector3(3, 2, 1);
        b = new Vector3(1.5f, 1.0f, 0.5f);


    }

    // Update is called once per frame
    void Update()
    {
        
    }
    //向量之间的点乘与叉乘。
    private void OnGUI()
    {

        float c = Vector3.Dot(a, b);//c为a,b的点乘;
        //求角度,Acos为cos的反函数。
        float angle = Mathf.Acos(Vector3.Dot(a.normalized, b.normalized)) * Mathf.Rad2Deg;

        //标签,GUI控件
        GUILayout.Label("两者的点乘为:" + c);
        GUILayout.Label("两者的角度为:" + angle);

        //用两个向量的叉乘来求角度,所谓叉乘就是a的模乘以b的模乘上两者角度的sin值。
        float cc = Mathf.Asin(Vector3.Distance(a.normalized, b.normalized)) * Mathf.Rad2Deg;
        GUILayout.Label("两者的角度为:" + cc);

    }
}

上面这段代码主要是介绍向量之间的点乘和叉乘。

(3)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Vector22 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    private void OnGUI()
    {
        if (GUILayout.Button("creatCube"))
        {
            //创建一个实例的cube,并且赋予其材质为红色,最后加了一个刚体组件,有了这个组件的话物体就会受重力
            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;
        }
       


    }

}

这段代码的话就是通过代码来创建物体,然后给物体加想要的组件,实现颜色的变化。

希望能够对大家有帮助!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值