rigidbody force help

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#:

using UnityEngine;
using System.Collections;

public class Gun : MonoBehaviour {
	
	public GameObject Sphere = null;
	public GameObject Capsule;
	public float fireSpeed = 500f;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if(Input.GetButton ("Fire1")){
		Fire();
		}
	
	}
void Fire(){
	GameObject Bullet1 = Instantiate(Sphere, Capsule.transform.position, Capsule.transform.rotation) as GameObject;	 
		Bullet1.rigidbody.AddRelativeForce(transform.forward * fireSpeed, ForceMode.Impulse);  
	}
}

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.

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);

I tried that.

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

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.

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.

Couple things to check.

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

you dont need relativeforce. just use force

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.

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?