Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by alienufo · Feb 26, 2013 at 01:42 AM · addforcebulletshoot

Bullets shooting the wrong way with AddForce

So my project right now has two "bases", one on the right and one on the left. Each base has units going toward the opposite base and when they get in range of eachother, they are supposed to stop moving and attack eachother until all of one side is dead, then continue moving. Most of that works right now, but for some reason my attack script only works for the left side units, and not the right side ones. Specifically, the bullets that I instantiate go the wrong way when they are fired for the ones on the right side, but they work exactly as expected for the ones on the left... they both use the same script but the "tag" value is set to opposite units in the editor (so they will not attack themselves).

http://youtu.be/iTHr3D6QsL4 - a video demonstrating the problem.

Here is the script:

 using UnityEngine;
 using System.Collections;
 
 public class AttackEnemy : MonoBehaviour {
 
     public GameObject bullet;
     public float bulletSpeed = 1.0f;
     public float fireRate = 1.0f;
     public float fireRadius = 5.0f;
     private    GameObject target = null;
     private UnitHealthControl enemyUnit;
     private float timeOfLastShot = 0.0f;
     public string tag;
 
 
     void FindTargets()
     {
         foreach(Collider col in Physics.OverlapSphere(transform.position, fireRadius))
         {
             if(col.tag == tag)
             {
                 target = col.gameObject;
                 enemyUnit = target.GetComponent<UnitHealthControl>();
                 break;
             }
         }
         
         if(target != null)
         {
             gameObject.SendMessage ("isMoving", false);
             Debug.Log(gameObject.name + " has stopped moving");
             if(Time.time - fireRate > timeOfLastShot)
             {
                 
                 SpawnBullet();
                 Debug.Log (gameObject.name + " is attacking " + target.name);
                 timeOfLastShot = Time.time;
             }
         }
         else
         {
             if(enemyUnit.isAlive == false)
             {
                 target = null;    
             }
             Debug.Log (gameObject.name + " has begun moving");
             gameObject.SendMessage ("isMoving", true);
         }
     }
 
     void Update()
     {
 
         FindTargets();
     }
     
     void SpawnBullet()
     {
         GameObject newBullet = Instantiate(bullet,transform.position, bullet.transform.rotation) as GameObject;
         newBullet.rigidbody.AddForce((target.transform.position - transform.position).normalized*bulletSpeed,ForceMode.VelocityChange);
     }
 
 }
 


Anyone have a clue as to why this doesn't work for both sets?

Comment
Add comment · Show 1
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 alienufo · Feb 27, 2013 at 10:46 PM 0
Share

Nobody has any ideas?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Kiloblargh · Feb 27, 2013 at 11:03 PM

First, use transform.LookAt to rotate the players units so they are facing their target. Then try using LerpAngle or SmoothDampAngle and give them little cube prefab guns just so you can see which way they're currently facing, and wait until they rotate to face their target to actually fire.

Then set the rotation value in Instantiate to transform.rotation instead of bullet.transform.rotation, and use AddRelativeForce to move the bullet.

 void SpawnBullet()
     {  
        GameObject newBullet = Instantiate (bullet,transform.position, transform.rotation) as GameObject;
        newBullet.rigidbody.AddRelativeForce(Vector3(0,0,bulletSpeed),ForceMode.VelocityChange);
     }

Also, any time you are using "transform" in Update() you should make a habit of putting "var myTransform : Transform" in the start of your script and then "myTransform = transform" in Awake() and using myTransform instead; it's faster.

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 alienufo · Feb 28, 2013 at 02:38 AM 0
Share

Okay I'm about to try this, but I'm a little confused about the last sentence. So I declare my transform in awake, and then I can just use that variable anywhere I used transform before? Like if I want the bullets rotation I could do bullet.myTransform.rotation , and for my own position, i'd just do myTransform.position ? Both will work?

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.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

bullets don't go forward 1 Answer

Add force to Instantiated object. 4 Answers

How to make a gun shoot? 2 Answers

Bullet Shoot At Enemy Position 1 Answer

add force on topdown bullet 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