Ejemplo n.º 1
0
    void Update()
    {
        if (game.gameState() != GameState.Play && game.gameState() != GameState.ExitingLevel)
            return;

        // Every frame, the cursor updates what interactable thing it's looking at
        // This is done by raycasting into the scene while ignoring the player / raycastignored
        // ~( (1 << 8) | (1 << 9) ) is a layermask which ignores those above two layers selectively
        // If we find a PlayerInteractable object within range, but it says it's disabled, ignore it

        RaycastHit hit;
        Ray ray = game.playerCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
        Physics.Raycast(ray, out hit, interactDistance, ~( (1 << 8) | (1 << 2) ));

        PlayerInteractable current = null;
        if (hit.transform != null)
            current = hit.transform.GetComponent<PlayerInteractable>();
        if (current != null && !current.isInteractable())
            current = null;
        lookingAt = current;

        // If the player isn't grabbing anything, and they left click while looking at something interactable,
        // they need to either begin grabbing it (if it's PlayerGrabbable) or simply send it an interact signal

        if (grabbing == null)
        {
            if (Input.GetButtonDown("Mouse Left Click") && lookingAt != null)
            {
                if (lookingAt is PlayerGrabbable)
                {
                    grabbing = (PlayerGrabbable) lookingAt;
                    grabTime = Time.time;
                    grabPosition = grabbing.holdPosition;
                    transform.localPosition = grabPosition;

                    grabbing.transform.parent = transform;
                    grabbing.interact(0);
                    grabJoint.connectedBody = hit.rigidbody;

                    lookingAt = null;
                }

                else
                    lookingAt.interact(0);
            }
        }

        // Check if the grabbed object should be released. Multiple conditions satisfy this:
        // 1) The user presses LMB (dropped release)
        // 2) The user presses RMB (thrown release)
        // 3) The object becomes noninteractable
        // 4) After snapTime, the object's distance from the grabber becomes greater than snapDistance

        else
        {
            if (grabPosition != grabbing.holdPosition)
            {
                grabPosition = grabbing.holdPosition;
                grabbing.transform.parent = null;
                transform.localPosition = grabPosition;
                grabbing.transform.parent = transform;
            }

            if ((Input.GetButtonDown("Mouse Left Click"))
            || (Input.GetButtonDown("Mouse Right Click"))
            || (!grabbing.isInteractable())
            || (Time.time > grabTime + snapTime && Vector3.Distance(transform.position, grabbing.transform.position) > snapDistance))
            {
                grabbing.transform.parent = null;
                grabbing.interact(1);
                grabJoint.connectedBody.velocity = game.player.getPhysicalVelocity();
                if (Input.GetButtonDown("Mouse Right Click"))
                    grabJoint.connectedBody.AddForce(transform.rotation * Vector3.forward * throwForce);
                grabJoint.connectedBody = null;

                grabbing = null;
            }
        }

        // Make the grabber bob with the camera

        Vector3 target = grabPosition - (transform.parent.localPosition - new Vector3(0, 1.5f, 0)) * 0.5f;
        transform.localPosition = Vector3.Lerp(transform.localPosition, target, 0.5f);
    }
Ejemplo n.º 2
0
 void Awake()
 {
     game = GetComponentInParent<GameController>();
     grabbable = GetComponent<PlayerGrabbable>();
     body = GetComponent<Rigidbody>();
 }
Ejemplo n.º 3
0
 public bool isGrabbing(PlayerGrabbable test)
 {
     return (grabbing == test);
 }