Uniyt3d物体跟随鼠标方向移动
2013-05-21 19:08
302 查看
第一个脚本实现效果:鼠标单机移动物体
基础部分终于看的差不多了。
要开始写代码,实现效果了~
程序基础不怎么好,慢慢来~~
点击鼠标左键 物体移动到鼠标位置
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);
}
}
基础部分终于看的差不多了。
要开始写代码,实现效果了~
程序基础不怎么好,慢慢来~~
点击鼠标左键 物体移动到鼠标位置
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);
}
}
相关文章推荐
- [unity3d]如何实现游戏对象跟随鼠标方向移动
- unity3d游戏物体跟着鼠标方向移动
- [unity3d]如何实现游戏对象跟随鼠标方向移动
- [脚本]实现游戏对象跟随鼠标方向移动
- unity3d游戏物体跟着鼠标方向移动
- 2D画面中使物体跟随鼠标的方向旋转(向量法)
- threejs中坐标系转换和实现物体跟随鼠标移动
- 《鼠标点击物体,物体跟随光标移动》与《鼠标按住左键不放拖拽物体》
- Unity3D之物体跟随鼠标移动和旋转
- Unity3d 物体跟随鼠标移动
- [脚本]实现游戏对象跟随鼠标方向移动
- [脚本]实现游戏对象跟随鼠标方向移动
- Unity 物体在屏幕内跟随鼠标移动
- 游戏物体跟随鼠标移动
- unity3d游戏物体跟着鼠标方向移动
- unity3d实现一个人物跟随鼠标点击地面那点就移动那点,实现点击特效加人物动画方向的切换。
- unity3d游戏物体跟着鼠标方向移动
- 【Unity】物体跟随鼠标移动
- Unity3d 物体跟随鼠标移动
- Unity 物体跟随鼠标点击移动,判断物体是否在移动