• 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
Question by zpunehx · May 13, 2014 at 10:34 PM · c#physicsrigidbodyprojectilerts

Fireing a tank projectile.

i am trying to create an attack mechanic for my rts game. here is the steps:

1)i raycast .

2) if the game object i hit is of Type AI i can Start the Attack Coroutine.

3)i use transform.look at AI Game object.

4)i instantiate a missile and add a script component to it called "bullet script".

5)in the bullet script i add force to the bullet ( it has a rigid body component).

6)on collision i reduce the other game object's health.

7) i have an empty game object in the scene where the missiles are instantiated.

8) that empty game object is a child of my tank.

Update:


Current Problem: when i instantiate the bullet the behavior and rotation are very odd. now the bullet is instantiated and goes in the right direction (previous problem) but the behavior is wrong it drops towards the ground and right before it reaches the ground it looks vertically at the target then goes it's way. so basically: 1)the rotation is not 90 so its instantiated vertically. 2)When it's instantiated it's not air born immediately like i previously mentioned. any help would be highly appreciated.

Bullet Script

public class BulletScript : MonoBehaviour {

 private Rigidbody myRigid;
 private float force;
 private float dmg;
 private attacktest myTank;
 private void Start() {
     myTank = GetComponent<attacktest> ();
     myRigid = GetComponent<Rigidbody> ();
     force = 100f;
     dmg = Random.Range (50f, 80f);
 }

 private void Update(){
     transform.rotation = Quaternion.LookRotation (myRigid.velocity);
 }

 private void FixedUpdate() {
     myRigid.AddForce(Vector3.forward*force,ForceMode.Impulse);
 }
 private void OnCollisionEnter(Collision collision) {
     collision.gameObject.GetComponent<ObjectLogic> ().ApplyDmg (dmg);
 }

}

AttackTEST Script

 public GameObject rocketPrefab;
 public Transform barrelEnd;
 public GameObject Target {
     get {
         return target;
     }
     set {
         if(hit.transform.gameObject.GetComponent<ObjectLogic>().myType !=UnitType.Player) {
             Debug.Log("Gi");
             target = value;
             StopCoroutine("Attack");
             StartCoroutine("Attack");
         }else{
             Debug.Log("Invalid Target");
         }
     }
 }
 public delegate void mydel ();
 public static event mydel Click;

 private GameObject target;
 Ray ray;
 RaycastHit hit;

 private void Update() {
     LockTarget ();
 }

 private void LockTarget() {
     if (Input.GetButton ("Fire1")) {
         Click();
         Debug.Log("Target Aquired + "+Target.name);
     }
 }

 private void Tester() {
     Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     if (Physics.Raycast (ray, out hit)) {
         Target = hit.transform.gameObject;
     }
 }

 private void FireTankMissle() {
     GameObject rocketInstance;
     rocketInstance = Instantiate(rocketPrefab, barrelEnd.position, 
                                  barrelEnd.rotation) as GameObject;
     rocketInstance.AddComponent<BulletScript>();
 }

 IEnumerator Attack() {
     Debug.Log ("About to Attack");
     do {
         transform.LookAt(Target.transform.position);
         yield return new WaitForSeconds (2f);
         FireTankMissle();
         yield return new WaitForSeconds(4f);
     } while (Target!=null);
     yield return new WaitForSeconds (2f);
 }

 void OnEnable() {
     Click += Tester;
 }
 void OnDisable() {
     Click -= Tester;
 }
Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer Wiki

Answer by Kiwasi · May 13, 2014 at 11:21 PM

Line 17 of your BulletScript is the problem

Vector3.forward will always return (0,0,1). Essentially ignoring rotation of the transform

Change it to transform.forward. This will provide the forward vector of the rotated bullet.

Code example for line 17

 myRigid.AddForce(transform.forward*force,ForceMode.Impulse);

The bullet will now travel in the local forward direction.

Comment
AdvsNoob

People who like this

1 Show 5 · 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 AdvsNoob · May 13, 2014 at 11:28 PM 0
Share

BoredMormon is correct. You are adding force to the global position not the local one, hence why the bullet only travels in one direction.

avatar image zpunehx · May 14, 2014 at 03:46 AM 0
Share

if u notice i mentioned i attempted trying to add the transform.forward but the force for some reason is not applied. the bullet just drops.

avatar image zpunehx · May 14, 2014 at 04:06 AM 0
Share

Alright so now it works but the behavior is odd. the bullet drops then before it reaches the ground looks vertically at the target then launches. so now it goes in the target's direction but in an odd way , any ideas?

avatar image Kiwasi · May 14, 2014 at 09:32 PM 0
Share

Is the bullet a child of another rigidbody? This produces really really strange effects.

avatar image zpunehx · May 19, 2014 at 07:18 PM 0
Share

@BoredMormon Thanks alot !

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

22 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

Related Questions

Throwing a Spear 1 Answer

rigidbody2D.addforce in collider 1 Answer

Vibrating GameObjects 0 Answers

How do I add a pulling force? 1 Answer

Rigidody.velocity = Direction * speed; How to get direction? 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