0
\$\begingroup\$

So I decided I wanted to make this script part of a separate swap-able weapon so I made a child and put the script there. Before the script worked fine but now it won't work. The script works as follows: if the left mouse button is pressed send a ray cast out forward, if it hits something matching the condition (a tag check, and distance check) parent it to the player, and if the right click button is pressed than un-parent it.

The code:

public class PhysGun : MonoBehaviour
{
    public GameObject Player;
    private bool isPlayerHolding;
    private GameObject RayCastHitObject;

    // Update is called once per frame
    void Update()
    {
        Vector3 fwd = transform.forward;

        Ray ray = new Ray(transform.position, transform.forward);
        RaycastHit hit;

        if (Input.GetMouseButton(0) && Physics.Raycast(ray, out hit, 10f) && hit.collider.gameObject.CompareTag("Pickable"))
        {
            Debug.Log("Press detected");
            isPlayerHolding = true;
            hit.transform.SetParent(Player.transform);
            Debug.Log("We hit: " + hit.transform.name);
            hit.rigidbody.constraints = RigidbodyConstraints.FreezeAll;
            MoveScrollWheel();
        }
        if (Input.GetMouseButton(1) && Physics.Raycast(ray, out hit, 10f))
        {
            hit.transform.parent = null;
            isPlayerHolding = false;
            hit.rigidbody.constraints = RigidbodyConstraints.None;
        }
        if (isPlayerHolding != false)
        {
            MoveScrollWheel();
        }
    }

        void MoveScrollWheel ()
        {
            if (Input.GetAxis("Mouse ScrollWheel") > 0f) // forward
            {
                Debug.Log("Mouse wheel up");
            }
            else if (Input.GetAxis("Mouse ScrollWheel") < 0f) // backwards
            {
                Debug.Log("Mouse wheel down");
            }
    }
}

What is wrong with my setup?

To clarify a few things...

  • It's not working - By this mean it does not parent the object to the player like it should.
  • Steps I've taken to resolve the issue (some are already above)...
  • Remove tag check requirement - this causes a weird issue no matter where I am it always parents the terrain to the player and causes the player fall through it.
  • Manually attach the player GameObject to the script.
  • Detach the child (and make it a sibling) and put in front to see if it works that way (it doesn't).
  • The raycast is hits something without the tag check (in this case the terrain as described above) but that is totally the wrong thing.
  • No error messages appear in the console or anywhere.
  • As per request here is a demonstration of the issue.
\$\endgroup\$
  • 3
    \$\begingroup\$ "it won't work" always needs unpacking. In what way is it failing to work? What are the symptoms? What debugging steps have you taken to narrow down the problem? Are either of the raycasts hitting anything? Does that thing have a rigidbody & pass the tag check? Are you getting any error messages? Can you show us a screenshot of a problem case so we can see whether there are any other colliders getting in the way? \$\endgroup\$ – DMGregory May 3 '16 at 1:59
  • \$\begingroup\$ @DMGregory I've updated my question with clarification on many of those questions. \$\endgroup\$ – Sie May 3 '16 at 2:24
1
\$\begingroup\$

A Couple things to check:

1) Transform.forward may not be the direction you actually want to shoot the ray. For example if this is a first-person game, the direction the PhysGun is facing (transform.forward) may be totally different than where the camera is looking, and in that case you would probably want to shoot the ray from the camera.

2) Have you tried using RaycastAll to get more than just one hit? Perhaps you are raycasting in the correct direction, but the first result is undesirable.

\$\endgroup\$
  • \$\begingroup\$ 1) Well I tried using Debug.DrawRay to see where the ray is shooting but it didn't show anything anywhere. Even with the tag check off it still seems to be grabbing the terrain regardless. 2) I could but I don't really see how that will help. \$\endgroup\$ – Sie May 3 '16 at 18:07
  • \$\begingroup\$ 1) Beware that Debug.DrawRay will only show up in the scene view, or in the Game window if the "Gizmos" button is depressed for the frames in which you draw that ray. What I'm suggesting here really depends on your game, whether it's 2D, 3D, first person, etc. A screen shot might help here quite a bit. 2) RaycastAll could be useful if terrain is somehow in the way. \$\endgroup\$ – Stephen Schroeder May 3 '16 at 19:08
  • \$\begingroup\$ 1) I'm aware and yes Gizmos are on and no it still does not show anything. 2) Just tried that and still nothing. Also the game is a 3D first person game if it makes a difference. \$\endgroup\$ – Sie May 3 '16 at 20:46
  • \$\begingroup\$ How does the location of the raycast hit compare with your intended direction and the location of the object you want to hit? \$\endgroup\$ – Stephen Schroeder May 3 '16 at 21:35
  • \$\begingroup\$ That's just it even with the tag check removed (and with it) I cannot see it. It just happens. \$\endgroup\$ – Sie May 3 '16 at 22:07
0
\$\begingroup\$

Well I finally figured out the issue with some help from here and Reddit. Basically I was trying to send the raycast from the model's (the child) forward and that was the wrong forward.

So I changed the third (2nd) line of code in the update statement to Ray ray = new Ray(Player.transform.position, Player.transform.forward);. This basically makes it so that the ray will originate from the players forward facing position rather than whatever the model's (child) forward is.

Long story short I used the Player's forward position and it worked brilliantly.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.