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 JoeD86 · Apr 22, 2014 at 11:46 PM · rotationinstantiategravitydirectionbullet

Shoot an object and have it move based on rotation

Here is my situation. I'm trying to make a turret that shoots shells (I already have it set as a child object to a tank). I'm trying to get it to where the shell will be shot in a direction that is based on the rotation of the turret. However, I have tried several things and nothing worked. Here is my code for the bullet

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class Bullet : MonoBehaviour {

 public int damage =1;// Use this for initialization
 public bool isEnemyShot = false;

 public Vector2 speed= new Vector2(10,0);
 //public int aimAngle = TankFire.instance.aimAngle;
 public Vector2 direction = new Vector2 (1, 0);
 private Vector2 movement;

 void Start () 
 {
     DestroyObject (gameObject, 20);//20sec, need to destory to avoid leak
     //movement = new Vector2(speed.x, speed.y);
     movement = new Vector2(2045, 2045);
     rigidbody2D.AddForce (movement);



 }
 void Update()
 {
     //rigidbody2D.velocity = transform.forward;
 }
 
 void FixedUpdate()
 {
     //rigidbody2D.velocity = movement;
     //apply movement to the rigidbody
 }

}

And here is my code for the object shooting it

     if(tick >= fireRate)
     {

         //Instantiate (tankBullet, this.transform.position,  Quaternion.Euler (this.transform.rotation.eulerAngles.z, this.transform.rotation.eulerAngles.y,this.transform.rotation.eulerAngles.z));
         Instantiate (tankBullet, this.transform.position,this.transform.rotation);


         tick = 0;
     }

I also need the shell to drop due to gravity using the Unity physics engine.

Thanks to whoever is able to help.

Comment
Add comment · Show 2
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 ToxxicSin · Apr 23, 2014 at 02:25 AM 0
Share

Have you tried making the velocity of the shell be part of transform.forward or transform.right? If you're working in Unity2D make sure you try transform.right, because your forward may be on the Z axis that you are not using.

If you're attaching this script to the bullet itself, have you considered making a variable of type GameObject named GunTip or something of that sort, and then telling the bullet to have the velocity going in the direction of GunTip.forward or GunTip.right

avatar image ToxxicSin · Apr 23, 2014 at 02:32 AM 0
Share

And to make the bullet drop, just attach a rigidbody to the prefab ^~^

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Apr 23, 2014 at 03:53 AM

Assuming:

  • The game object with the Instantiate script is pointing (forward) in the direction you want to fire the Projectile.

  • You have a Rigidbody component on the projectile prefab

  • You've constructed the prefab so that the forward of your projectile faces positive 'z' when the rotation is (0,0,0)

  • The 'tankBullet' variable is of type GameObject.

You could then do:

 if(tick >= fireRate)
 {
     GameObject go = Instantiate (tankBullet, this.transform.position,this.transform.rotation) as GameObject;
     go.rigidbody.AddForce(transform.forward * 1000); 
  
     tick = 0;
 }

The '1000' assumes the projectile has a mass of 1.0 and may need to be adjusted for your app.

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

21 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

Related Questions

My 2d spawned bullet prefabs will not fly up, just fall 3 Answers

Turret bullet rotation problem 1 Answer

Can't make a bullet move in the direction the actor is facing 2 Answers

bullet rotation as its affected by gravity 2 Answers

Problem with Shooting Accuracy 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