I try to shoot an object with rigidbody3D attach on him and addforce forward on that rigidbody, but when i shoot the object fall in the floor without any force … I look on the internet but nothing fix my problem this is my code :
PS : The mass of the bullet is 1
using UnityEngine;
using System.Collections;
public class Bullet_Fire : MonoBehaviour
{
public GameObject Bullet_Emitter;
public GameObject Bullet;
public float currentTime = 0;
public float ShootingTime = 2f;
public float powerMultiplier = 10f;
private bool lastUsePressedState = false;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
currentTime += Time.deltaTime;
var controller = (GetComponent<Collider>().GetComponent<VRTK_ControllerEvents>() ? GetComponent<Collider>().GetComponent<VRTK_ControllerEvents>() : GetComponent<Collider>().GetComponentInParent<VRTK_ControllerEvents>());
if (controller)
{
if (lastUsePressedState == true && !controller.usePressed && currentTime > ShootingTime) {
//The Bullet instantiation happens here.
GameObject Temporary_Bullet_Handler;
Temporary_Bullet_Handler = Instantiate (Bullet, Bullet_Emitter.transform.position, Bullet_Emitter.transform.rotation) as GameObject;
Temporary_Bullet_Handler.transform.Rotate (Vector3.left * 90);
//Retrieve the Rigidbody component from the instantiated Bullet and control it.
//Rigidbody Temporary_RigidBody;
//Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody> ();
//Tell the bullet to be "pushed" forward by an amount set by powerMultiplier.
Temporary_Bullet_Handler.GetComponent<Rigidbody>().AddForce(transform.forward * powerMultiplier);
currentTime = 0;
}
lastUsePressedState = controller.usePressed;
}
}
}
the issue is that force needs to be applied multiple times, but your code only adds force once. so you could start a coroutine that keeps adding force until it’s velocity = somespeed.
Just to be sure, is the blue arrow for your bullet’s transform pointing in the direction you want the bullet to go? i.e. it’s not pointing downward or something, right? And your bullet rigidbody doesn’t have IsKinematic checked?
Hmm the only other thing I noticed is that in your AddForce call you use “transform.forward” (as opposed to say, Temporary_Bullet_Handler.transform.forward), which would use the forward vector of the object this script is on as opposed to the bullet. So when I asked if your bullet transform was pointing in the right direction, I should have said was the transform of the object this script is attached to pointing in the right direction. Verify that object’s transform, if that wasn’t what you had done earlier.
can you give this a try for me. I haven’t tested the code. so if there is a typo, my bad, This might cause an infinite loop. so save you project first
using UnityEngine;
using System.Collections;
public class Bullet_Fire : MonoBehaviour
{
public GameObject Bullet_Emitter;
public GameObject Bullet;
public float currentTime = 0;
public float ShootingTime = 2f;
public float powerMultiplier = 10f;
private bool lastUsePressedState = false;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
currentTime += Time.deltaTime;
var controller = (GetComponent<Collider>().GetComponent<VRTK_ControllerEvents>() ? GetComponent<Collider>().GetComponent<VRTK_ControllerEvents>() : GetComponent<Collider>().GetComponentInParent<VRTK_ControllerEvents>());
if (controller)
{
if (lastUsePressedState == true && !controller.usePressed && currentTime > ShootingTime) {
//The Bullet instantiation happens here.
GameObject Temporary_Bullet_Handler;
Temporary_Bullet_Handler = Instantiate (Bullet, Bullet_Emitter.transform.position, Bullet_Emitter.transform.rotation) as GameObject;
Temporary_Bullet_Handler.transform.Rotate (Vector3.left * 90);
//Retrieve the Rigidbody component from the instantiated Bullet and control it.
//Rigidbody Temporary_RigidBody;
//Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody> ();
//Tell the bullet to be "pushed" forward by an amount set by powerMultiplier.
//Temporary_Bullet_Handler.GetComponent<Rigidbody>().AddForce(transform.forward * powerMultiplier);
StartCoroutine(AddForce(Temporary_Bullet_Handler.GetComponent<Rigidbody>()));
currentTime = 0;
}
lastUsePressedState = controller.usePressed;
}
}
IEnumerator AddForce(Rigidbody rb)
{
while(rb.velocity < 10)
{
rb.AddForce(transform.forward * powerMultiplier);
}
return;
}
}
In addition to the advice above from @johne5 , do you have a large drag value (this is “air resistance”) on the rigidbody by any chance? You could even set this to zero for testing. Outside of that, I’m running out of ideas here, sorry!
using UnityEngine;
using System.Collections;
public class Bullet_Fire : MonoBehaviour
{
public GameObject Bullet_Emitter;
public GameObject Bullet;
public float currentTime = 0;
public float ShootingTime = 2f;
public float powerMultiplier = 10f;
private bool lastUsePressedState = false;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
currentTime += Time.deltaTime;
var controller = (GetComponent<Collider>().GetComponent<VRTK_ControllerEvents>() ? GetComponent<Collider>().GetComponent<VRTK_ControllerEvents>() : GetComponent<Collider>().GetComponentInParent<VRTK_ControllerEvents>());
if (controller)
{
if (lastUsePressedState == true && !controller.usePressed && currentTime > ShootingTime) {
//The Bullet instantiation happens here.
GameObject Temporary_Bullet_Handler;
Temporary_Bullet_Handler = Instantiate (Bullet, Bullet_Emitter.transform.position, Bullet_Emitter.transform.rotation) as GameObject;
Temporary_Bullet_Handler.transform.Rotate (Vector3.left * 90);
//Retrieve the Rigidbody component from the instantiated Bullet and control it.
//Rigidbody Temporary_RigidBody;
//Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody> ();
//Tell the bullet to be "pushed" forward by an amount set by powerMultiplier.
//Temporary_Bullet_Handler.GetComponent<Rigidbody>().AddForce(transform.forward * powerMultiplier);
StartCoroutine(AddForce(Temporary_Bullet_Handler.GetComponent<Rigidbody>()));
currentTime = 0;
}
lastUsePressedState = controller.usePressed;
}
}
IEnumerator AddForce(Rigidbody rb)
{
while(rb.velocity.x < 10f)
{
rb.AddForce(transform.forward * powerMultiplier);
}
yield return null;
}
}
But not work Unity turn on “No reponse” and freez when i shoot
using UnityEngine;
using System.Collections;
public class Bullet_Fire : MonoBehaviour
{
public GameObject Bullet_Emitter;
public GameObject Bullet;
public float currentTime = 0;
public float ShootingTime = 2f;
public float powerMultiplier = 5f;
public float BallisticForce = 3f;
private bool lastUsePressedState = false;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
currentTime += Time.deltaTime;
var controller = (GetComponent<Collider>().GetComponent<VRTK_ControllerEvents>() ? GetComponent<Collider>().GetComponent<VRTK_ControllerEvents>() : GetComponent<Collider>().GetComponentInParent<VRTK_ControllerEvents>());
if (controller)
{
if (lastUsePressedState == true && !controller.usePressed && currentTime > ShootingTime) {
//The Bullet instantiation happens here.
GameObject Temporary_Bullet_Handler;
Temporary_Bullet_Handler = Instantiate (Bullet, Bullet_Emitter.transform.position, Bullet_Emitter.transform.rotation) as GameObject;
Temporary_Bullet_Handler.transform.Rotate (Vector3.left * 90);
//Retrieve the Rigidbody component from the instantiated Bullet and control it.
//Rigidbody Temporary_RigidBody;
//Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody> ();
//Tell the bullet to be "pushed" forward by an amount set by Bullet_Forward_Force.
Temporary_Bullet_Handler.GetComponent<Rigidbody>().velocity = BallisticForce * powerMultiplier * Temporary_Bullet_Handler.transform.TransformDirection(Vector3.forward);
//Basic Clean Up, set the Bullets to self destruct after 10 Seconds, I am being VERY generous here, normally 3 seconds is plenty.
//Destroy (Temporary_Bullet_Handler, 3.0f);
currentTime = 0;
}
lastUsePressedState = controller.usePressed;
}
}
}
I apply the velocity only once, with power forward and ballistic force ( For make fake energy at the bullet and produce fake ballistic effect at the end on the bullet )