Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Nov 29, 2016 at 07:01 PM by Nova-1504 for the following reason:

Figured it out

avatar image
0
Question by Nova-1504 · Nov 29, 2016 at 02:34 PM · rigidbodyshootingtransform.positioncarshoot

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nova-1504 · Nov 29, 2016 at 06:26 PM 0
Share

Ok, collider is disabled and Update is FixedUpdate and it still does it.

avatar image Nova-1504 · Nov 29, 2016 at 06:37 PM 0
Share

I changed it back to Update because FixedUpdate made it spawn literally thousands of projectiles.

avatar image Crash0 Nova-1504 · Nov 29, 2016 at 06:55 PM 0
Share

yeah you should add code to make sure its called just once.

avatar image Nova-1504 · Nov 29, 2016 at 06:40 PM 0
Share

What if, since the car's collider is so big, it's hitting the player's capsule collider?

avatar image Nova-1504 Nova-1504 · Nov 29, 2016 at 06:48 PM 0
Share

Never $$anonymous$$d, didn't have a collider in the first place. Tried adding it, no change.

avatar image Crash0 · Nov 29, 2016 at 06:47 PM 0
Share

Code works fine here? is there a script on any of the prefabs that might interfere?

avatar image Nova-1504 · Nov 29, 2016 at 06:58 PM 0
Share

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.

avatar image Crash0 Nova-1504 · Nov 29, 2016 at 07:05 PM 0
Share

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.

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

92 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges