Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 user-4788 (google) · Sep 18, 2010 at 07:45 PM · 2dcannonball

2d cannon bullet

Detail:Making a 2d cannon shooter which shoots in the sky for flying planes.I have a cylinder as a cannon,A sphere as a prefab(bullet).Attached at the tip of the cannon is a locator from which the bullet instannce comes out.I need to rotate the cannon.This is my script.Just started C#(3-4 days): cannon:using UnityEngine; using System.Collections;

public class player : MonoBehaviour { public GameObject bullet; // Use this for initialization void Start () {

}

// Update is called once per frame void Update () {

    if (Input.GetKeyDown("space"))
 {        
 Instantiate(bullet,GameObject.Find("bullet_spawn").transform.position,Quaternion.identity);
 bullet.rigidbody.AddForce(Vector3.forward * 200000);
 }
    if (Input.GetKey("left"))
    {
       this.transform.Rotate(Vector3.forward);
    }

}

}

The problems are: 1.When ever I press "space"(fire) there is always a first sphere that is created which just sits and the subsequent ones move forward.If I pause and fire again this thing happens again. 2.When i start rotating the cylinder(cannon) the spheres just pop out and dont shoot out and when i stop rotating the shooting starts again but with the first one coming out with like no power.

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 Wotan · Sep 18, 2010 at 08:08 PM

I think you misunderstand Instantiate(). The documentation states:

static function Instantiate (original : Object, position : Vector3, rotation : Quaternion) : Object

Description Clones the object original and returns the clone.

The first parameter is the object that shall be cloned (the original) and the cloned object is returned. Your code however ignores the return code.

Therefore

bullet.rigidbody.AddForce(Vector3.forward * 20000;

works on the original bullet, not on its clone!

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 NPSF3000 · Oct 03, 2010 at 07:19 AM 0
Share

So what you want to do is: var instance : GameObject = Instantiate(bullet,GameObject.Find("bullet_spawn").transform.position,Quaternion.identity); instance.rigidbody.AddForce(Vector3.forward * 200000);

(Sorry that's in JS)

avatar image
0

Answer by poncho · Dec 31, 2010 at 04:11 PM

if you already have your cannon ball you dont need to instantiate an other one, you can use the existing one if you want to rotate the cannon to make an angle you code wont work cus it will just have force on X axiz meanin in the <- or -> ways

you can try this

float cannonAngle=0;
cannonAngle = cannon.transform.rotation.eulerAngles.z; // this gives you the inclination if you have your 2d game in just X and Y axis
Vector3 bulletForce = new Vector3(20000*mathf.cos(cannonAngle*180/mathf.PI),20000*mathf.sin(cannonAngle*180/mathf.PI),0);
bullet.rigidbody.AddForce(bulletForce);
//or you can use
bullet.rigidbody.velocity = bulletForce;

any of AddForce or velocity works for movin the object, the difference is that with one the cannon ball will fly at amazin speed, just play a little with the values of your foce when you decide wich one will you use

hope it helps you =)

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

No one has followed this question yet.

Related Questions

2D Animation does not start 1 Answer

Instantiate object that points to the direction 1 Answer

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

Why does my reflect code not work? 1 Answer

2D Space Shooter Instantiating bullet question 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