Unity简单脚本

一.transform 移动受刚体碰撞

 void Update () {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        transform.Translate(new Vector3(h, 0, v) * speed*Time.deltaTime);
	
	}
//刚体运动效果,可以碰撞物体效果
// rigidbody.MovePosition(transform.position + new Vector3(h, 0, v) * speed * Time.deltaTime);

二.摄像机跟随玩家视角

private Transform player;
    private float smoothing = 3;
	void Start () {

       player = GameObject.FindGameObjectWithTag(Tags.player).transform;
        
	}
	
	// Update is called once per frame
	void Update () {
        Vector3 targetPos = player.position + new Vector3(3.7f, 1.4f, -4.1f);
        transform.position = Vector3.Lerp(transform.position, targetPos, smoothing * Time.deltaTime);
	
	}

三.得到层的代码

groundLayerIndex = LayerMask.GetMask("Ground");

四.控制转向

  Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitInfo;
       if( Physics.Raycast(ray, out hitInfo, 200, groundLayerIndex))
       {
           Vector3 target = hitInfo.point;
           target.y = transform.position.y;
           transform.LookAt(target);
       }

五.延迟 0.05秒执行ClearEffect 函数

<span style="font-size:18px;">Invoke("ClearEffect", 0.05f);</span>

六.射线的碰撞与方向

private LineRenderer lineRenderer;

lineRenderer = this.renderer as LineRenderer;

//
 this.lineRenderer.enabled = true;
        //开始点
        lineRenderer.SetPosition(0, transform.position);
        Ray ray = new Ray(transform.position, transform.forward);
        RaycastHit hitInfo;
        //结束点
        if (Physics.Raycast(ray, out hitInfo))
        {
            lineRenderer.SetPosition(1, hitInfo.point);

        }
        else
        {
            lineRenderer.SetPosition(1, transform.position + transform.forward * 100);

        }

七.禁用脚本


private PlayerShoot playerShoot;

//获取脚本
 playerShoot = this.GetComponentInChildren<PlayerShoot>();

//禁止使用脚本
 this.playerShoot.enabled = false;


八.导航网格

//先Bake,再添加NavMeshAgent
 private NavMeshAgent agent;
 agent = this.GetComponent<NavMeshAgent>();
 agent.SetDestination(player.position);






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值