Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 FuriousCreeper · Feb 11, 2019 at 10:56 AM · inputbuttonmobileshootcannon

Shoot cannonballs at the push of a button

Hello everyone !

I'm creating a mobile game. It's a little game with a cannon firing bullets. I used the instantinate function to duplicate the gameobjects (cannonball). It works very well in itself but it is necessary to define a key to allow the cannon to shoot. But here's the problem: it's a mobile game and to shoot you have to use the key on the keyboard. I put a UI Button on the canvas for the gun to fire. I put scripts in it, I tried many things, nothing happens ...

How can I fire my cannon without using the keys? (button)

Here is the script :

 using System.Collections;
 using UnityEngine;
 
 public class CannonBallSpawn : MonoBehaviour
 {
     public Rigidbody CannonBallPrefab;
     public Transform barrelEnd;
 
     void Update()
     {
         if (Input.GetButtonDown ("Shoot")) 
         
     {
             Rigidbody cannonBallInstance;
             cannonBallInstance = Instantiate(CannonBallPrefab, barrelEnd.position, barrelEnd.rotation) as Rigidbody;
             cannonBallInstance.AddForce(barrelEnd.forward * 4000);
     }
     
     }
 
 }
 
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

1 Reply

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

Answer by misher · Feb 11, 2019 at 11:39 AM

First create a separate method for shooting like so:

 public void Shoot() {
     Rigidbody cannonBallInstance = Instantiate(CannonBallPrefab, barrelEnd.position, barrelEnd.rotation) as Rigidbody;
     cannonBallInstance.AddForce(barrelEnd.forward * 4000);
 }

Then you can activate it in 2 different ways:

  1. From a button on canvas, you simply add a listener to the button in the inspector, drag the object with the scrip onto it and select the Shoot method from the dropdown list.

  2. From touching the screen, this can be as simple as:

    void Update() { if(Input.GetMouseButtonDown(0)) { Shoot(); } }

Comment
Add comment · Show 3 · 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 FuriousCreeper · Feb 11, 2019 at 12:39 PM 0
Share

It works ! Thanks a lot :)

If you want i put your nick in the credits of my game when its finished

avatar image misher FuriousCreeper · Feb 11, 2019 at 01:24 PM 0
Share

Haha, I'm glad my answer was helpful to you, which method you've ended up using: click on screen or button?

avatar image FuriousCreeper misher · Feb 11, 2019 at 03:57 PM 0
Share

I've used the button method because the click on the screen is already used for aim :(

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

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

is possible press 2 buttons at the same time Mobile touch? 0 Answers

How do I detect touch on another object and check by tag/name? 1 Answer

Drag and Drop button on Mobile (Messenger style) 0 Answers

For mobile games, how could you divide the screen into three buttons vertically? 1 Answer

Making one directional virtual joystick OR smoothly transitioning between buttons 0 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