Figured it out
Car not shooting like bullet properly
I'm trying to shoot a car like a bullet, but it just kind of flops in front of the character. Here's the code;
using UnityEngine;
using System.Collections;
public class ThrowObject : MonoBehaviour {
public GameObject bullet;
public AudioClip shootSound;
public GameObject vehicularToThrow;
private float throwSpeed = 2200f;
private AudioSource source;
private float volLowRange = .5f;
private float volHighRange = 1.0f;
void Awake () {
source = GetComponent<AudioSource>();
}
void Update () {
if (Input.GetButtonDown("Fire1"))
{
float vol = Random.Range (volLowRange, volHighRange);
source.PlayOneShot(shootSound,vol);
GameObject throwThis = Instantiate (bullet, transform.position, transform.rotation) as GameObject;
throwThis.GetComponent<Rigidbody>().AddRelativeForce (new Vector3(0,0,throwSpeed));
}
if (Input.GetButtonDown("Fire2")){
GameObject shootCar = Instantiate (vehicularToThrow, transform.position, transform.rotation) as GameObject;
shootCar.GetComponent<Rigidbody>().AddRelativeForce (new Vector3(0,0,throwSpeed));
}
}
}
It's almost like it hit a collider right in front of the player, but there isn't one. Maybe because it's instantiating itself inside the gun. I understand this is kind of a weird thing to try and do, but please help anyway! How do I fix this?
Answer by Crash0 · Nov 29, 2016 at 06:22 PM
First of all you want to do the spawning, and applying force in the FixedUpdate()
method.
And yea, its probably because its spawned inside the gun. The easiest way to solve this is just to disable the collider for the gun.
Ok, collider is disabled and Update is FixedUpdate and it still does it.
I changed it back to Update because FixedUpdate made it spawn literally thousands of projectiles.
yeah you should add code to make sure its called just once.
What if, since the car's collider is so big, it's hitting the player's capsule collider?
Never $$anonymous$$d, didn't have a collider in the first place. Tried adding it, no change.
Code works fine here? is there a script on any of the prefabs that might interfere?
The thing is, the script controls shooting two types of objects(a small cube, which never has any problems, and a car, which fails every time.) So I think it's either a script problem, rigidbody problem or a size problem with the car.
yes, its because its hitting a collider somewhere. The script runs fine without a collider at the base GameObject. Try lifting it up y++, and disable the collider. And also try to disable colliders child objects if there are any.
Answer by Nova-1504 · Nov 29, 2016 at 07:01 PM
OK I fixed it. Rigidbody mass on the car was set too high(20).
Follow this Question
Related Questions
I can't get my rigidbody reference set on my script in the editor? 2 Answers
How do I make two objects share two of the same transform values? 1 Answer
Space shooter - why after clicking play the bolt is shooting 0 Answers
Shots don´t go forward 0 Answers
3D Pong Clone: Set position of a Rigidbody, or change how a speed vector is verified? 1 Answer