Uniyt3d物体跟随鼠标方向移动

第一个脚本实现效果:鼠标单机移动物体
基础部分终于看的差不多了。


要开始写代码,实现效果了~


程序基础不怎么好,慢慢来~~


点击鼠标左键 物体移动到鼠标位置


C#实现


using UnityEngine;
using System.Collections;


public class Script_01 : MonoBehaviour 
{

private Vector3 world;
private float speed; //the move speed of object

void Start () 
{

}



void Update () 
{
Vector3 screenpos=Camera.main.WorldToScreenPoint(transform.position); //turn the coordinate of object from World to Screen
Vector3 m=Input.mousePosition; //the position of mouse

//when mouse click
if(Input.GetMouseButton(0))
{
m.z=screenpos.z; //give a z axis coordinate to mouse
world=Camera.main.ScreenToWorldPoint(m); //change to world coordinate 
speed=30;
}
if(transform.position==world)
{
speed=0;
}
transform.LookAt(world); //let the object look at mouse
transform.Translate(Vector3.forward*speed*Time.deltaTime);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值