Search Unity

rigidbody force help

Discussion in 'Scripting' started by Keith90, Oct 10, 2013.

  1. Keith90

    Keith90

    Joined:
    Sep 27, 2013
    Posts:
    116
    Hello,

    I have looked up tutorials and topics and I tried it all. For some odd reason, I can't make an instantiated object move.

    I even made a new project to keep everything simple and test it out there.

    I put in the First Person Controller, a bullet(sphere) and a gun( capsule).

    Here is code I found on a tutorial using C#:


    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Gun : MonoBehaviour {
    5.    
    6.     public GameObject Sphere = null;
    7.     public GameObject Capsule;
    8.     public float fireSpeed = 500f;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.    
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.         if(Input.GetButton ("Fire1")){
    18.         Fire();
    19.         }
    20.    
    21.     }
    22. void Fire(){
    23.     GameObject Bullet1 = Instantiate(Sphere, Capsule.transform.position, Capsule.transform.rotation) as GameObject;  
    24.         Bullet1.rigidbody.AddRelativeForce(transform.forward * fireSpeed, ForceMode.Impulse);  
    25.     }
    26. }
    27.  
    I understand what it all means. However, I don't know if it's Unity itself or maybe I'm still missing something, but I can't get the object to move.

    All it does is stay in the air. I've managed to make it fall before but now with this new setup, it doesn't even do that which I assume has something to do with the rigidbody on it.


    The transform.forward * fireSpeed should move it forward.

    From what I know, you have a prefab. You make a copy of the prefab and set the position and rotation to whatever it is you'd like, so in this case, the gun. Afterwards, once it's instantiated you add force to the object which then makes it go forward.

    So from that, where am I going wrong? I greatly appreciate any help. I'll even redo the entire code or set it up how exactly you want me to. I just need to understand where it's going wrong.
     
  2. bara1247

    bara1247

    Joined:
    Oct 22, 2012
    Posts:
    40
    its because when Fire() func is executed one time
    you can try 2 solutions:
    Bullet1.rigidbody.velocity = transform.forward * fireSpeed;
    or use this in side an other script attached to the bulet object (update())
    rigidbody.AddRelativeForce(transform.forward * fireSpeed, ForceMode.Impulse);
     
  3. Keith90

    Keith90

    Joined:
    Sep 27, 2013
    Posts:
    116
    I tried that.

    It still just spawns in the air. Doesn't even fall. Could that be with the bullet itself?
     
  4. Marsupilami

    Marsupilami

    Joined:
    Sep 22, 2013
    Posts:
    21
    Try using AddForce instead of AddRelativeForce.

    I may be wrong, but I think AddRelative is for objects that are already in motion and the force is "relative" to their current motion. As you are creating an object with zero motion and trying to add motion "relative" to zero you get zero "relative" motion as a result.
     
  5. Keith90

    Keith90

    Joined:
    Sep 27, 2013
    Posts:
    116
    Still nothing. It shoots but it stays there. I'm going to try again to remake everything.

    There must be a setting that is preventing the bullet from going forward.
     
  6. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,618
    Couple things to check.

    1. Drag. make it zero all round
    2. IsKinematic is off

    you dont need relativeforce. just use force
     
  7. Keith90

    Keith90

    Joined:
    Sep 27, 2013
    Posts:
    116
    I tried using force and still nothing.

    Made sure drag is 0 and that IsKinematic is off. Still nothing.

    Made new script called bullet and move the part of the code that makes it move forward onto that script. Still nothing.
     
  8. Marsupilami

    Marsupilami

    Joined:
    Sep 22, 2013
    Posts:
    21
    Did you attach the script to the Gun? Does the bullet prefab have a rigidbody? Did you associate the appropriate prefabs/objects with the public variables?
     
unityunity