• 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 /
avatar image
0
Question by duh · Aug 12, 2011 at 04:28 AM · collisionforce

Collision problem on force increase

We have a problem with the bullet collision with the target. if the bullets force is above 1000, then the target does not register the collision. If we make the force <999 then it registers the collision, however visually it seems the bullet does not seem like a fired bullet. Please suggest how we can maintain speed and still register the collision

the following script was added to the target

var targetRoot : GameObject; private var beenHit: boolean = false; var hitSound : AudioClip; var UpSound : AudioClip;

function OnCollisionEnter(theObject : Collision) { if(beenHit==false && theObject.gameObject.tag=="bullet") { audio.PlayOneShot(hitSound); targetRoot.animation.Play("down"); beenHit=true; TargetControll.tgtdwn++; } } function Update () { }

And the following script was added to the gun

static var canshoot = true; static var canreload = false; var BulletPrefab: Transform; var DeagleSound : AudioClip; var DeagleReload: AudioClip;

function Update () { var deagle = GameObject.Find("desert eagle"); if(Input.GetButtonDown("Fire1") && canshoot==true) { var Bullet = Instantiate(BulletPrefab, GameObject.Find("BulletSpawn").transform.position, GameObject.Find("Main Camera").transform.rotation); Bullet.rigidbody.AddForce(transform.forward * 9000);

     if(Indepvariable.charge <= 6)
         {
             canshoot = false;
             deagle.animation.Play("shootfix");
             audio.PlayOneShot(DeagleSound);
             weaprecoil ();
         }
     }
 if(Indepvariable.charge == 7)
     {
         canshoot = false;
         deagle.animation.Play("reload");
         audio.PlayOneShot(DeagleReload);
         reload ();
     }
 if(Input.GetButtonDown("reload") && canreload == true)
     {
         canshoot = false;
         deagle.animation.Play("reload");
         audio.PlayOneShot(DeagleReload);
         reload ();
     }

}

function weaprecoil () { yield new WaitForSeconds (0.5); Indepvariable.charge++; canshoot = true; }

function reload () { Indepvariable.charge = 0; yield new WaitForSeconds (3); canshoot = true; canreload = false; }

Comment
Add comment · Show 2
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 fredpointzero · Aug 12, 2011 at 08:28 AM 2
Share

$$anonymous$$ay be the bullet is too fast to be detected, try to play with Rigidbody.interpolation, it may help you.

Otherwise, consider Raycasting through a layer to detect bullet collision. This is much efficient and use less CPU.

avatar image duh · Aug 16, 2011 at 01:28 AM 1
Share

I am still looking for help wit regards to this question

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Aug 16, 2011 at 03:02 AM

@fredpointzero is right: fast projectiles cause this kind of error. Collisions are checked at the FixedUpdate rate (default: 50 times per second). If some object is fast enough, it can "pass through" the target between cycles - in one cycle it's before the target, but in the next it's after the target. Since the target collider was not touched, no collision will be reported.
For this reason, only slow projectiles like rockets are actually instantiated. For bullets, you should use a Raycast - the engine traces an imaginary ray and returns the first object hit. It's much more efficient, and makes no visual difference since you can't see the bullet anyway.
You can modify the shooting code in your gun script like this:
EDITED: I confused Desert Eagle with the target. There's the correct version:

function Update(){
    var deagle = GameObject.Find("desert eagle");
    if(Input.GetButtonDown("Fire1") && canshoot==true){
        audio.PlayOneShot(shotSound); // play the shot sound here
        var hit: RaycastHit;
        var spawnPos = GameObject.Find("BulletSpawn").transform.position;
        var direction = Camera.main.transform.forward;
        if (Physics.Raycast(spawnPos, direction, hit)){ // if something hit...
            // try to get the script TargetScript.js from the target...
            var tgScript: TargetScript = hit.transform.GetComponent(TargetScript);
            // if the object has such script, call its function TargetShot()
            if (tgScript) tgScript.TargetShot();
        }
        if(Indepvariable.charge 
I supposed the target script is called TargetScript.js. If it has a different name, replace TargetScript in the script above by its actual name (no quotes or extension) .
Anyway, replace OnCollisionEnter(...) by TargetShot() in the target script:

 
  ...
  function TargetShot(){
    audio.PlayOneShot(hitSound);
    targetRoot.animation.Play("down");
    beenHit=true;
    TargetControll.tgtdwn++;
  }
  ...
Comment
Add comment · Show 1 · 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 aldonaletto · Aug 16, 2011 at 04:02 AM 0
Share

I had confused Desert Eagle with the target - I edited the answer to fix this.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Make AI run from Player 1 Answer

OnCollisionEnter Push Object Problem 0 Answers

How do I move this rigidbody? 1 Answer

What does rigidbodies sleeping have to do with collision detection? 0 Answers

Is there a way to have rigid bodies collide, but not transfer any force? 2 Answers


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