• 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 Raul · Aug 17, 2010 at 03:42 AM · shootturret

My Tank Turret Wont Fire?

So i am working on a top-down tank shoot em up and i have the controls working but not the cannon. I think im doing it right but its not fireing when i press my mouse 0 button have a look.

function update()
{
    if(Input.GetKeyDown(KeyCode.Mouse0))
    {
        var bullet = Instantiate(bulletPrefab, transform.Find("cannonShoot").transform.position, Quaternion.identity);
        bullet.rigidbody.AddForce(bullet.transform.forward * 2000);
    }
}

"cannonShoot" is a empty game object in front of my tank's barrel

Also i need a way to rotate to a specific angle over a period of time... currently im just using snap to angles for movements as shown below.

private var z = 0.0; private var x = 0.0; private var y = 0.0; function Update() { var leftButton = Input.GetKeyDown(KeyCode.A); var upButton = Input.GetKeyDown(KeyCode.W); var rightButton = Input.GetKeyDown(KeyCode.D); var downButton = Input.GetKeyDown(KeyCode.S); var angles = transform.eulerAngles; //Basic Movements if(leftButton) { var rotationLeft = Quaternion.Euler(x, -90, z); transform.rotation = rotationLeft; } if(upButton) { var rotationUp = Quaternion.Euler(x, 0, z); transform.rotation = rotationUp; } if(rightButton) { var rotationRight = Quaternion.Euler(x, 90, z); transform.rotation = rotationRight; } if(downButton) { var rotationDown = Quaternion.Euler(x, -180, z); transform.rotation = rotationDown; } //Two buttons at once if(downButton && leftButton) { var rotationDownLeft = Quaternion.Euler(x, 45, z); transform.rotation = rotationDownLeft; }

}

Any help would be appreciated.

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 spinaljack · Aug 17, 2010 at 10:31 AM 0
Share

I wouldn't recommend using Find every time you shoot. If the cannonshoot game object never changes just store a reference to it, it'll be much faster

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by oinkoinkflapflap · Nov 21, 2010 at 07:39 PM

i do have an alternative although it it's self has a few problems, if i aim up (as i happen to be making a tank game 2) it still fires forward instead of up, and if i turn the cannon of the tank and fire it will fire the direction of the tank body and not the gun... but it fires :)

//Shooting

var bullitPrefab:Transform;

(i don't know how 2 do the fancy script thing on here) but this at the top with other variables

if(Input.GetButtonDown("Fire1")) { var bullit = Instantiate(bullitPrefab, GameObject.Find("spawn").transform.position, Quaternion.identity); bullit.rigidbody.AddForce(transform.forward * 7000); }

and this somewhere within the moving stuff, (your main script btw) the number above (7000) make it bigger to fire faster or lower to slow.

function OnCollisionEnter(){ Destroy(gameObject);

} function Update () { }

make a new script and fill all of it with the above and but it on the bullet to make it disappear on collision, and make the bullets rigidbody, (remove gravity on bullets if you want them not to fall) with the disappear bit it will still have effects on like other rigidbody objects so its good :) hope this helps.... bloody hell i've written allot. :O

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
avatar image
0

Answer by oinkoinkflapflap · Nov 22, 2010 at 05:13 PM

very sorry, forgot 2 add that you will need 2 make an object with no collider or render were u want the bullets to spawn and call it "spawn" (no capitals)

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

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Turret + Cannon Mouse movement 1 Answer

OnColliderEnter - Turret Shoot Only once problem 1 Answer

How can i set up a turret that turns and shoots where you touch onscreen?,how to shoot where you touch 2 Answers

Turret shooting at me 1 Answer

Gun Turret will not Shoot. 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges