Search Unity

Question How do I throw a grenade from the end of a stick?

Discussion in 'XR Interaction Toolkit and Input' started by kip420, May 15, 2022.

  1. kip420

    kip420

    Joined:
    May 9, 2022
    Posts:
    1
    Hi guys!
    I'm making a VR game and Unity for the first time and trying to learn myself how to code.
    I'm making a lot of progress but I'm having trouble with one particular attack:

    You can grab a stick and while holding it, pressing the trigger will spawn a ball on the end of the stick.
    While holding the trigger the ball will remain on this spawnpoint, also while this point is being moved.
    When releasing the trigger you will "let go" of the ball and it will be thrown.
    You could compare it to one of those dog throw things, where you attach the tennisball to the end of it and throw it further (just imagine the tennisball being a grenade in this case)

    I've watched a lot of tutorials and played around a lot with XR Socket Interactor, but it doesn't seem to work and I'm not sure what to try anymore.

    So I thought I'l' start clean and ask for help.
    Right now, I will press the trigger(new input system) and the ball spawns and drops to the ground, with this code:

    Instantiate(ballPrefab2, ThrowPoint.position, ThrowPoint.rotation).GetComponent<Rigidbody>();
    BallPrefab is the ball
    ThrowPoint is the end of the stick, childed to the Stick.

    Main questions:
    How do I keep the ball attached to the ThrowPoint on the end of the stick?
    How do I detach the ball from ThrowPoint and give the right direction and velocity?
    And how would I add a Throw Velocity Scale to this, just like the XR Interactable?

    I hope someone is willing to help! =)