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 /
avatar image
0
Question by OrionGamer · Dec 13, 2017 at 07:10 PM · scripting problembulletturrettank

How to Align/Orient a Bullet/Projectile along/with a Gun/Turret?

I am a beginner in Unity who is trying to make a 2D, topdown, tank-shooter game.

My question: how do I make the bullets fired from a turret in Unity actually originate from the end of its gun and continue to move in a trajectory which is parallel to the barrel of the gun?

Context: the player's character is a tank, with a body and turret controlled by separate scripts. The turret points in the direction of the player's cursor, represented by a crosshairs. The tank's body is controlled independently by the keyboard.

Problem: I am trying to add another script to the turret, written some years ago by @Clet_, which takes an empty object on the end of the gun and makes it (I am assuming) the same location as an origin point for the bullet. Attempts to use this script as is result in the following error: Assets/Scripts/TurretFire.cs(30,49): error CS1502: The best overloaded method match for `UnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Transform, bool)' has some invalid arguments Numerous attempts to alter the code have resulted in the same/similar errors. Clet_ mentioned in his original post that 'Rigidbody2D' belongs somewhere in the sample code he produced, but I am unable to determine where.

{ // https://answers.unity.com/questions/701611/getting-bullets-to-fire-in-direction-of-gun-barrel-1.html // Script by "Clet_"

using UnityEngine;

public class cannonShell : MonoBehaviour {

 public float speed;
 public Vector3 direction;

 private void Update () {
     transform.position += direction * speed * Time.deltaTime;
 }

}

public class Turret : MonoBehaviour {

 public Transform TurretTip;
 public GameObject cannonShellPrefab;
 public AudioSource PlayerCannonSoundEffect;

 private void Update () {
     if((Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1) || Input.GetMouseButtonDown(2))) {
     PlayerCannonSoundEffect.Play();
     FireCannon();
     }
 }

 private void FireCannon () {
     cannonShell actualCannonShell = (cannonShell)(Instantiate (cannonShellPrefab, TurretTip, Quaternion.identity) as GameObject);
     actualCannonShell.direction = TurretTip.forward;
 }

} }

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

· Add your reply
  • Sort: 
avatar image
0

Answer by OrionGamer · Dec 13, 2017 at 07:31 PM

I should clarify that the first line of the method/constructor "FireCannon()" is the source of the error.

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 OmarMoya · Dec 13, 2017 at 08:42 PM

You can assign the transform.forward of the gun/turret to the velocity of the bullet, multiplied by some factor that represents the actual speed of the bullet.

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.

Follow this Question

Answers Answers and Comments

125 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 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

Bullet Not Working 3 Answers

Turret Firing problem [C#] 2 Answers

Tank turret 1 axis, but keeping turret rotation for parent. 0 Answers

bullets acting weird when spawn 2 Answers

Tank AI script that calculates distance of targets and firing angle 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