2018-01-03 14:53:04 q764424567 阅读数 4508

这里写图片描述

在Update中运行

Vector3 mousePos=Camera.main.ScreenToWorldPoint(Input.mousePosition)+new Vector3(0,0,10);

这行代码的意思是屏幕坐标转化为三维坐标,然后z轴加10就是正确的位置了

transform.position=mousePos;

当前坐标跟随鼠标移动

2019-12-22 14:30:57 zhunju0089 阅读数 216

效果图:

1.创建一个三维物体Cylinder圆柱体 Target

2.创建词汇表Yellow材质球

3.将材质球挂载到Target物体上

4.创建脚本MouseMove.cs

MouseMove.cs代码

using UnityEngine;
/*
 让物体完全跟随鼠标
     */
public class MouseMove : MonoBehaviour
{
    [Header("主相机")]
    [Tooltip("主相机")]
    /// <summary>
    /// 主相机
    /// </summary>
    public Camera m_mainCamera;
    [Header("目标物体")]
    [Tooltip("目标物体")]
    /// <summary>
    /// 目标物体
    /// </summary>
    public Transform m_target;

    void Start()
    {
        isDown = false;
        targetWTSP = m_mainCamera.WorldToScreenPoint(m_target.position);
    }
    /// <summary>
    /// 是否按下
    /// </summary>
    private bool isDown;
    /// <summary>
    /// 记录初始化时候target世界坐标转屏幕坐标时候的位置 WTSP==WorldToScreenPoint
    /// </summary>
    private Vector3 targetWTSP;
    void Update()
    {
        if (Input.GetMouseButtonDown(0)) {
            isDown = true;
        }
        if (Input.GetMouseButtonUp(0))
        {
            isDown = false;
        }
    }
    private void LateUpdate()
    {
        if (isDown)
        {
            Vector3 mouseVec3 = m_mainCamera.ScreenToWorldPoint(
                new Vector3(Input.mousePosition.x, Input.mousePosition.y, targetWTSP.z)
            );
            m_target.position = mouseVec3;
        }
    }
}

5.新建一个空物体Script,并将MouseMove.cs脚本挂载上去,再将相机和Target物体拖拽到脚本上

完毕!

 

 

 

 

2015-04-17 14:30:25 u010499272 阅读数 3689
分享一个Unity3D点击模型跟随鼠标移动的小脚本,希望对需要的人有帮助!
直接把这个代码放在要移动的模型上就OVER了!!!
</pre><pre name="code" class="csharp">using UnityEngine;
using System.Collections;

public class ModelsPosChange : MonoBehaviour {
    RaycastHit hit;
    private Transform Icon;
    bool isMove = false;
    void Start () {
	}

    void FixedUpdate() {
        if (isMove) {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if ((Physics.Raycast(ray, out hit,500)) && (null != hit.collider)) {
                Debug.DrawLine(ray.origin, hit.point);
                Icon = transform.GetComponent<Transform>();
                Vector3 screenPos = Camera.main.WorldToScreenPoint(Icon.position);
                Vector3 offset = Icon.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPos.z));
                Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPos.z);
                Vector3 worldPos = Camera.main.ScreenToWorldPoint(mousePos);
                Icon.position = worldPos;
            }
            transform.renderer.material.color = Color.green;
            if (Input.GetKey(KeyCode.Delete)) {
                Destroy(this.gameObject);
            }

        } else {
            transform.renderer.material.color = Color.white;
        }
    }
    void OnMouseDown() {
        isMove = !isMove;
    }
}

2017-08-17 18:42:15 Czhenya 阅读数 2165

在场景中创建一个Plane和一个Sphere以及3CubePlane做为地面,Sphere作为人物,Cube作为敌人。用户通过点击地面,使Sphere移动到点击的位置(不能使用导航网格寻路)Sphere如果接近敌人一定距离后,敌人会跟随Sphere行动。如果敌人在主角旁边静止2s则主角消失。

public class cube : MonoBehaviour {

    public Sphere sphere;
    float des; //与主角之间的距离

    // Use this for initialization
    void Start () {
        
    }
	
	// Update is called once per frame
	void Update () {
       
        des = Vector3.Distance(sphere.transform.position, this.gameObject.transform.position);
        if(des < 3 && des > 2)
        {       //看向主角并跟随;
            this.gameObject.transform.LookAt(sphere.transform.position);
            this.gameObject.transform.Translate(Vector3.forward * Time.deltaTime);

        }
        
    }

public class Sphere : MonoBehaviour {

    Ray ray;
    RaycastHit rh;
    float dis;      //鼠标与球的距离
    Vector3 tar;   //目标点

    private Vector3 lastPos;    //上一次运动停止的位置
    private float lastTime;     //上一次运动停止的时间

    // Use this for initialization
    void Start () {
        lastPos = transform.position;
        lastTime = 0;
    }
	
	// Update is called once per frame
	void Update () {

        if (Input.GetMouseButtonDown(0))   
        {      //左键,获得鼠标射线
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out rh))     
            {               
                if (rh.collider.name == "Plane")   //判碰撞物体是否是地面
                {
                    tar = rh.point;
                    
                    //Debug.Log(rh.point+"2");
                }
            }

        }
        dis = Vector3.Distance(this.gameObject.transform.position, tar);
        if (dis > 0.5f)   //鼠标点击与球的距离
        {
            this.gameObject.transform.LookAt(tar);
            this.gameObject.transform.Translate(Vector3.forward * Time.deltaTime);
        }
        if (dis < 3)
        {
            if (lastPos != transform.position)  //如果上次静止的位置和当前位置不相同,就更新上次静止的位置和时间
            {
                lastTime = Time.time;
                lastPos = transform.position;
            }
            if (Time.time - lastTime > 2)       //如果静止时间>2s  
            {
                lastTime = Time.time;
                Destroy(this.gameObject);
            }
        }
    }
}

2018-07-06 17:48:25 sun_surprise 阅读数 449

话不多说,直接上代码。

public class CubeScale : MonoBehaviour {

    private Vector3 MousePos;
    private float LastCubeScale=1.0f;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
        MousePos = Input.mousePosition;     
        transform.position = Camera.main.ScreenToWorldPoint(MousePos + new Vector3(0, 0, 10));


        if (Input.GetAxis("Mouse ScrollWheel")>0)
        {
            LastCubeScale += 0.05f;
        }
        else if (Input.GetAxis("Mouse ScrollWheel") < 0)
        {
            LastCubeScale -= 0.05f;
        }
        transform.localScale = Vector3.one * LastCubeScale;
    }
}
没有更多推荐了,返回首页