建立一个平面,并贴图

建立一个立方体,为立方体建立一个材质,把材质绑定(拖)立方体上

修改材质

双击颜色控件

 

创建脚本

创建的脚本名称必须与类的名称一致

 

配置脚本编辑器 VS2019

代码扩展功能正常即可

摄像头脚本(绑定到立方体)

这样就可以移动摄像头

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

public class CameraMoveControl : MonoBehaviour
{
    public float speed=0;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        float h= Input.GetAxis("Horizontal")*speed*Time.deltaTime;
        float v= Input.GetAxis("Vertical")*speed*Time.deltaTime;
    
        transform.Translate(h,0,v);
    }
}

操作脚本(绑定到子弹)

WSAD 移动子弹

鼠标左键子弹旋转

鼠标右键向另一个胶囊体移动并画线

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

public class VO : MonoBehaviour
{

	Vector3 v1=new Vector3(3,0,0);
	Vector3 v2=new Vector3(0,0,2);
	Vector3 v;
	Quaternion quaternion;

	float vdot;
	public float speed;
    public Transform target;//!
	float angleval=15;

    // Start is called before the first frame update
    void Start()
    {
        VectorDistance();
        vdot=Vector3.Dot(v1,v2);
        Debug.Log("the dot of v1 and v2="+vdot);
        v=Vector3.Cross(v2,v1);
        Debug.Log("the cross of v2 and v1="+v);
    }

    // Update is called once per frame
    void Update()
    {
        Move();

        if(Input.GetMouseButton(0))
        {
        	angleval+=angleval*Time.deltaTime;
        	transform.rotation=Quaternion.AngleAxis(angleval,Vector3.up);
        }
        if(Input.GetMouseButton(1))
        {
        	transform.rotation=Quaternion.LookRotation(target.position-transform.position);
        	Debug.DrawLine(transform.position,target.position,Color.white,10f);
        	transform.Translate(Vector3.forward*speed*Time.deltaTime);
        }
    }

    void VectorDistance()
    {
    	float distance;
    	distance=Vector3.Distance(v1,v2);
    	print("the dis of v1 and v2 ="+distance);
    }

    private void Move()
    {
    	if(Input.GetKey(KeyCode.W))
    	{
    		transform.Translate(Vector3.forward*speed*Time.deltaTime);
    	}
    	if(Input.GetKey(KeyCode.S))
    	{
    		transform.Translate(Vector3.back*speed*Time.deltaTime);
    	}
    	if(Input.GetKey(KeyCode.A))
    	{
    		transform.Translate(Vector3.left*speed*Time.deltaTime);
    	}
    	if(Input.GetKey(KeyCode.D))
    	{
    		transform.Translate(Vector3.right*speed*Time.deltaTime);
    	}
    }
}

 

创建一个胶囊体作为target,在脚本vo的设置中把target设置为胶囊体

效果

鼠标左键旋转,鼠标右键对准绿胶囊并向其运动

posted on 2020-03-28 14:14  海月CSDN  阅读(653)  评论(0编辑  收藏  举报