unity鼠标跟随(ITween)

今天记录一下ITween的抛物线移动用法。

案例有两个:

1.一个白色图片的鼠标跟随

2.点击地面会有小球生成并移动到点击的地方。


案例1鼠标跟随

首先在场景中建立一个cube用来当做地面

(因为这样可以减小资源,plane用的是mesh collider会导致资源变大,所以不用plane做地面)

cube属性如图:



新建一个plane作为跟随鼠标的物体。(plane在这里就是跟随鼠标的作用所以为了减小资源可以删除mesh collider。

如下图(黄色地板为cube白色是plane



鼠标跟随的脚本如下:

using UnityEngine;
using System.Collections;

public class course : MonoBehaviour {
    public GameObject target;
     Vector3[] paths = new Vector3[3];
    public GameObject ballPerfab;
	// Use this for initialization
	void Start () {
	
	}
	
	
	void Update () {
        //鼠标跟随
        Ray ray = camera.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit))
        {
            if (hit.transform.gameObject.tag == "tile")

              /*  这里的target目标就是plane,我们就是要实现plane跟随鼠标动,这里在面板里要把plane这个预设体拖到target里。而代码里的new Vector3(hit.point.x, 0.1f, hit.point.z),.1f  这句则是:hit表示的是鼠标的位置分别有XYZ轴位置,只有X,Z两个轴运动所以把Y写成0.1f。最后面还有个.1f表示的是plane跟随鼠标的时间。时间越长就表示是plane走的越慢,鼠标到 了目标地点plane还是要继续走过来*/
            {
                iTween.MoveUpdate(target, new Vector3(hit.point.x, 0.1f, hit.point.z),.1f);
                

            }
    
        }

     
	}


 
/*注意上文中的tile是cube的Tag,从本文最上面那张Cube属性图就可以看到cube的Tag是tile.*/ 

只要把这个脚本拖动给相机运行就可以实现鼠标跟随。如下图:

案例2小球生成并移动到点击的地方:

实现的功能是点击鼠标生成一个小球,然后小球会跑到鼠标点击的位置,之后消失。

代码如下:

using UnityEngine;
using System.Collections;

public class course : MonoBehaviour
{
    public GameObject target;
    Vector3[] paths = new Vector3[3];//这里弄个3维的数组分别表示小球的起始点和目标点还有起始点和目标点中间的一个点。
    public GameObject ballPerfab; //这就是那个要运动的小球

    void Start() { }

    void Update()
    {
        //鼠标跟随刚才上面讲过了  
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;


        if (Physics.Raycast(ray, out hit))
        {
            if (hit.transform.gameObject.tag == "tile")

            {
                iTween.MoveUpdate(target, new Vector3(hit.point.x, 0.1f, hit.point.z), .1f);

                if (Input.GetMouseButtonDown(0))   //这里加个条件就 是鼠标单击的时候执行下面语句    
                {
                    GameObject ball = (GameObject)Instantiate(ballPerfab, new Vector3(0, 0, 0), Quaternion.identity);

                    paths[0] = new Vector3(0, 0, 0);//起始点    
                    paths[2] = hit.point;//终点,即鼠标点击点   

                    //起点到终点之间的点,注意XZ这两个轴,一个用是起始点坐标的一半另个是终点坐标的一半
                    paths[1] = new Vector3(paths[1].x / 2, 1, paths[2].z / 2);

                    iTween.MoveTo(ball, iTween.Hash("path", paths)); //其实这个就是路径了

                    Destroy(ball, 2);//每两秒就destroy一个。    
                }

            }

        }


    }

    void OnDrawGizmos()//这个是path的路线及其颜色设定如下图可见
    {
        iTween.DrawLine(paths, Color.blue);
        Debug.Log("1111111111111111111111111111111");
        iTween.DrawPath(paths, Color.red);
    }
}  
  
  
}


 
 
小球运动无法截图,只能看生成的路线图。如下: 





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现Unity中视角跟随鼠标,可以按照以下步骤进行: 1. 在Unity中创建一个摄像机,并将其放置在场景中。 2. 创建一个空物体,将其命名为“Player”,并将其放置在场景中。 3. 将摄像机作为“Player”的子对象,并将其位置设置为相对于“Player”对象的偏移量(例如,将其放置在“Player”对象的后方一定距离的位置)。 4. 编写脚本来控制摄像机的旋转。在脚本中,使用Input.GetAxis函数获取鼠标移动的偏移量,并将其应用于摄像机的旋转中。 以下是一个示例脚本: ``` public class CameraController : MonoBehaviour { public Transform player; public float sensitivity = 5.0f; public float smoothing = 2.0f; private Vector2 smoothedVelocity; private Vector2 currentLookingPos; void Start() { Cursor.lockState = CursorLockMode.Locked; } void Update() { Vector2 inputValues = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")); inputValues = Vector2.Scale(inputValues, new Vector2(sensitivity * smoothing, sensitivity * smoothing)); smoothedVelocity.x = Mathf.Lerp(smoothedVelocity.x, inputValues.x, 1f / smoothing); smoothedVelocity.y = Mathf.Lerp(smoothedVelocity.y, inputValues.y, 1f / smoothing); currentLookingPos += smoothedVelocity; transform.localRotation = Quaternion.AngleAxis(-currentLookingPos.y, Vector3.right); player.localRotation = Quaternion.AngleAxis(currentLookingPos.x, player.up); } } ``` 在这个脚本中,我们首先定义了一个“Player”对象,以及一些控制摄像机旋转的参数。在Start函数中,我们锁定了鼠标,这样它就不会离开游戏窗口。在Update函数中,我们使用Input.GetAxis函数获取鼠标移动的偏移量,并将其平滑处理,然后将其应用于摄像机的旋转中。最后,我们使用Quaternion.AngleAxis函数来将旋转转换为四元数,并将其应用于摄像机和“Player”对象中。 如果一切正常,你现在应该能够在Unity中看到摄像机跟随鼠标移动了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值