Esempio n. 1
0
    void checkGrab(HandScript hand, RaycastHit hit)
    {
        if (hit.distance < hand.armLength)
        {
            aSource.PlayOneShot(grabClip);

            grabbing = true;
            hand.grabHold(hit);

            Vector3 upVector = VectorFunctions.parallelVectorToNormal(transform.up, hit.normal);
            if (upVector == Vector3.zero)
            {
                upVector = -hand.side * Vector3.Cross(transform.forward, hit.normal);
                print("altered");
            }
            Vector3 forwardVector = hand.side * Vector3.Cross(upVector, hit.normal);

            //print("normal: " + hit.normal);
            //print("upVector: " + upVector);

            // print(upVector.normalized);

            targetRotation = Quaternion.LookRotation(forwardVector, upVector);

            //targetPosition = hit.point + 0.7f * hit.normal - 1f*forwardVector.normalized;
            targetPosition = hit.point - hand.restingPoint.x * hit.normal - hand.restingPoint.y * upVector.normalized - hand.restingPoint.z * forwardVector.normalized;

            //print(VectorFunctions.findVectorInDirection(new Vector3(-1, 0, 0), new Vector3(1, 0, 0)));
        }
        else
        {
            grabbing = false;
            hand.releaseHold();
        }
    }
Esempio n. 2
0
    // Update is called once per frame
    void Update()
    {
        body.angularVelocity = Vector3.zero;

        Vector2 mouseChange = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));

        lockMouse = false;

        if (Input.GetMouseButton(1))
        {
            //targetRotation = transform.localRotation*Quaternion.Euler(-(Input.mousePosition.y - Screen.height/2.0f)/5.0f, 0.5f*(Input.mousePosition.x - Screen.width/2.0f)/5.0f, 0);
            //transform.localRotation = Quaternion.RotateTowards(transform.localRotation, targetRotation, 3*Time.deltaTime*Quaternion.Angle(transform.localRotation, targetRotation));
            transform.localRotation = transform.localRotation * Quaternion.Euler(-12 * mouseChange.y, 12 * mouseChange.x, 0);
            freelooking             = true;
            lockMouse = true;
        }
        else
        {
            freelooking = false;
        }

        if (grabbing)
        {
            //Vector3 mouseChange = Input.mousePosition - lastMousePos;
            // body.angularVelocity = Vector2.zero;

            if (!freelooking)
            {
                moveBody(mouseChange, activeHand);
                transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, 3 * Time.deltaTime * Quaternion.Angle(transform.rotation, targetRotation));
            }

            //transform.position = transform.position + (3*Time.deltaTime)*(targetPosition - transform.position);
            body.velocity = 2 * (targetPosition - transform.position);

            lockMouse = true;

            if (!Input.GetMouseButton(0))
            {
                activeHand.releaseHold();
                inactiveHand.releaseHold();
                grabbing = false;

                aSource.PlayOneShot(releaseClip);
            }
        }
        else
        {
            if (Input.mousePosition.x < 0.45 * Screen.width)
            {
                activeHand   = leftHand;
                inactiveHand = rightHand;
            }
            else if (Input.mousePosition.x > 0.55 * Screen.width)
            {
                activeHand   = rightHand;
                inactiveHand = leftHand;
            }

            checkHandMovement(activeHand);
        }

        inactiveHand.moveToPoint = true;
        inactiveHand.targetPoint = transform.position + inactiveHand.armLength * (inactiveHand.restingPoint.x * transform.right + inactiveHand.restingPoint.y * transform.up + inactiveHand.restingPoint.z * transform.forward);
        //lastMousePos = Input.mousePosition;
        //leftHand.setVelocity(body.velocity);
        //rightHand.setVelocity(body.velocity);
    }