如何在Unity中使用鼠标在Y轴上移动对象

时间:2015-04-21 18:08:21

标签: unity3d mousemove

我希望用Y轴控制变换,因为它在X轴上自行移动。

我使用 Vector2.MoveTowards 在X轴上移动它。

必须通过屏幕上的任何触摸和拖动来移动变换。所以我在更新功能

中编写了我的代码

我的代码:

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour
{

    public Camera playerCamera;

    public float speed = 12.0F;
    private Vector3 moveDirection = Vector3.zero,Point,last;

    void Start()
    {
        Point = transform.position;
        last = transform.position;
        if (playerCamera == null)
        {
            playerCamera = Camera.main;
        }
        playerCamera.transparencySortMode = TransparencySortMode.Orthographic;
    }

    void Update(){
        if(Input.GetMouseButtonUp(0))
        Point = last;
        moveDirection.x = transform.position.x+speed;
        moveDirection.y = transform.position.y;

        if (Input.GetMouseButtonDown(0)) {
            Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
            last = ray.GetPoint (0);
            transform.position = new Vector2 (transform.position.x, last.y - Point.y);
        }
        else
        transform.position = Vector2.MoveTowards( transform.position,moveDirection,speed*Time.deltaTime);

         //moveDirection.y -= 20.0f * Time.smoothDeltaTime;


        //After we move, adjust the camera to follow the player
        playerCamera.transform.position = new Vector3(transform.position.x, playerCamera.transform.position.y , playerCamera.transform.position.z);
    }
}

我该如何解决?

0 个答案:

没有答案