Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Problem with a grenade throwing script

Discussion in 'Scripting' started by trmhtk2_unity, Apr 1, 2021.

  1. trmhtk2_unity

    trmhtk2_unity

    Joined:
    Mar 29, 2020
    Posts:
    35
    Hello everyone,
    I created a script for throwing a grenade, but the only problem is that when I throw it higher, it just falls down like everyone else, and I want it to go up, by the way the game is first person.
    Here is the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GrenadeThrower : MonoBehaviour
    6. {
    7.     public Transform Hand;
    8.     public float throwForce;
    9.     public GameObject grenadPrefab;
    10.     public KeyCode key;
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.        
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         if (Input.GetKeyDown(key))
    21.         {
    22.             ThrowGrenade();
    23.         }
    24.     }
    25.     void ThrowGrenade()
    26.     {
    27.         GameObject grendade = Instantiate(grenadPrefab, Hand.position, Hand.rotation);
    28.         Rigidbody rb = grendade.GetComponent<Rigidbody>();
    29.         rb.AddForce(0, Hand.parent.transform.rotation.y, 0);
    30.         rb.AddForce(transform.forward * throwForce,ForceMode.Impulse);
    31.     }
    32. }
    33.  
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Im not entirely sure what your problem is.
    But im pretty sure line 29 does not what you think it does. Rotation is a Quaternion, meaning all x,y,z and w (!) values are in the range of 0-1. These have nothing to do with the euler angles you likely intended to use. So the force you add there is miniscule to non-existant and the value you base it on is arbitrary.
    This is likely not causing your main problem, but then again you may wanna elaborate your problem a bit more. So far i only understood that you throw the grenade up and it comes down, which seems pretty reasonable.
     
  3. trmhtk2_unity

    trmhtk2_unity

    Joined:
    Mar 29, 2020
    Posts:
    35
    The problem is that I want when the player looks up he will throw the grenade where he is looking at it and not fall down
     
  4. JoelLeeJie

    JoelLeeJie

    Joined:
    Jul 4, 2020
    Posts:
    20
    you can add a script to the grenade prefab, putting AddForce(transform.forward * throwforce, ForceMode.Impulse); in void Start() instead.

    It'll add a force to the grenade when created.

    idk how to do it your way cause never learn yet haha
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    It's all about reasoning about and getting the hierarchy of your character right, and choosing the right direction.

    Here is my grenade tosser, part of my terrain deformer demo:



    The actual "throw this thing" code is at:

    https://github.com/kurtdekker/makegeo/blob/master/makegeo/Assets/Scripts/WeaponGrenadeTosser.cs

    I actually just did my own physics with a simple ballistic arc (Ballistic3D.cs) but the velocity calculations I did would work just fine for your Rigidbody-lobbing purposes. I would NOT use forces: I would just set the velocity of the Rigidbody.