如何在数组中存储鼠标移动位置值?

时间:2015-12-03 07:47:46

标签: c# unity3d

这里我试图将鼠标位置值存储在数组中,以便我的球将根据该数组值移动。

有没有更好的方法来做到这一点,因为我被困在项目的最后。我们将不胜感激。

using UnityEngine;
using System.Collections;

[RequireComponent (typeof(Ball))]
public class BallDragLaunch : MonoBehaviour {
    //MyScript
    //public float powePerPixel;
    public float maxPower;
    public float sensitivity;
    //private Vector3 touvhPos;
    private bool isRolling;
    private float rotation = 10f;
    //
    private Rigidbody rg;
    private Vector3 dragStart, dragFlick, dragEnd;
    private float startTime, endTime, flickTime;
    private Ball ball;
    public Rigidbody ballRigid;
    public float [] mousePositionX, mousePositionY;

    // Use this for initialization
    void Start () {
        ball = GetComponent<Ball> ();
        //MyScript
        isRolling = false;
        //ballRigid = this.ball;
    }

    public void MoveStart (float speed) {
        if (Input.GetAxis ("Mouse X") > 0) {
            if (! ball.inPlay) {
                dragStart = Input.mousePosition;
                //transform.position = new Vector3(Mathf.Clamp(ball.transform.position.x + speed, ball.transform.position.x - speed, 0), -42.0f, 42.0f);

                float xPos = Mathf.Clamp (ball.transform.position.x + speed, -42.0f, 42.0f);

                float yPos = ball.transform.position.y;
                float zPos = ball.transform.position.z;
                ball.transform.position = new Vector3 (xPos, yPos, zPos);
                Debug.LogError("Right");
            }
        }

        if (Input.GetAxis ("Mouse X") < 0) {
            if (! ball.inPlay) {
                dragStart = Input.mousePosition;
                //transform.position = new Vector3(Mathf.Clamp(ball.transform.position.x + speed, ball.transform.position.x - speed, 0), -42.0f, 42.0f);

                float xPos = Mathf.Clamp (ball.transform.position.x - speed, -42.0f, 42.0f);

                float yPos = ball.transform.position.y;
                float zPos = ball.transform.position.z;
                ball.transform.position = new Vector3 (xPos, yPos, zPos);
                Debug.LogError("Left");
            }
        }
    }

    public void DragStart () {
        if (! ball.inPlay) {
            // Capture time & position of drag start
            dragStart = Input.mousePosition;
            startTime = Time.time;
            Debug.LogError("Drag Start");
        }
    }

    public void DragEnd () {
        if (Input.GetAxis ("Mouse X") < 0.2f) {

            if (! ball.inPlay) {
                // Launch the ball
                dragEnd = Input.mousePosition;
                endTime = Time.time;

                float dragDuration = endTime - startTime;
                float launchSpeedX = (dragEnd.x - dragStart.x) / dragDuration;
                float launchSpeedZ = (dragEnd.y - dragStart.y) / dragDuration;
                transform.Rotate (0, rotation, 0);
                Vector3 launchVelocity = new Vector3 (launchSpeedX * sensitivity, 0, launchSpeedZ);

                ball.Launch (launchVelocity);
                Debug.Log ("Drag End Ball Launch");
                Debug.LogError("Left Ball Movemet ");

            }
        }
        if (Input.GetAxis ("Mouse X") > 0.2f) {

            if (! ball.inPlay) {
                // Launch the ball
                dragEnd = Input.mousePosition;
                endTime = Time.time;

                float dragDuration = endTime - startTime;
                float launchSpeedX = (dragEnd.x - dragStart.x) / dragDuration;
                float launchSpeedZ = (dragEnd.y - dragStart.y) / dragDuration;
                transform.Rotate (0, rotation, 0);
                Vector3 launchVelocity = new Vector3 (launchSpeedX * sensitivity, 0, launchSpeedZ);

                ball.Launch (launchVelocity);
                Debug.Log ("Drag End Ball Launch");
                Debug.LogError("Right Ball Movemet ");

            }
        }
        if (Input.GetAxis ("Mouse X") >= 0 ) {

            if (! ball.inPlay) {
                // Launch the ball
                dragEnd = Input.mousePosition;
                endTime = Time.time;

                float dragDuration = endTime - startTime;
                float launchSpeedX = (dragEnd.x - dragStart.x) / dragDuration;
                float launchSpeedZ = (dragEnd.y - dragStart.y) / dragDuration;
                transform.Rotate (0, rotation, 0);
                Vector3 launchVelocity = new Vector3 (launchSpeedX * sensitivity, 0, launchSpeedZ);

                ball.Launch (launchVelocity);
                //Debug.Log ("Drag End Ball Launch");
                Debug.LogError("Mid Ball Right Move");
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

回应Lajos Arpad回答

我不得不说使用列表而不是数组。它们使用起来要好得多,而且你不需要知道有多少变量会在手中进入列表。

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[RequireComponent (typeof(Ball))]
public class BallDragLaunch : MonoBehaviour {
    //MyScript
    public List<Vector3> mousPositions;

在您的代码中,您可以完全按照处理数组的方式处理列表。要将鼠标位置添加到列表,只需写入

mousePosition.Add(Input.mousePosition);

答案 1 :(得分:0)

您的ball正在三维移动,并且您使用Vector3。另一方面,您有两个float[]阵列,分别称为mousePositionXmousePositionY。您可以使用Vector3[]来实现此目的。这就是你声明的方式:

public Vector3[] mousePositions;

如果你知道它的大小,例如n,那么这就是你初始化它的方式:

mousePositions = new Vector3[n];

如果你想设置它的i'值,那么你可以这样做:

mousePositions[i] = ball.transform.position;

您需要解决的问题: - 在初始化之前,您需要知道数组的大小 - 您需要跟踪尚未放置有效位置的第一个索引

我相信你不会提前知道数组的大小,也不想总是跟踪索引。如果我是你,我会使用Queue。别担心,这并不困难。首先尝试使用阵列解决方案取得一些成功,当您看到它有效时,创建代码备份并尝试使用Queue<Vector3>进行试验。相信我,这是值得的。