• 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 Mint92 · Mar 28, 2014 at 03:39 PM · c#vector3directionbulletturret

Set bullet direction according to turret direction

Hey guys, Ive got my bullets to spawn through my BulletSpawnPoint through Instantiation.

 using UnityEngine;
 using System.Collections;
 
 public class Bullet : MonoBehaviour 
 {
     public float speed;
     public float maxFlightTime;
     public float currentFlightTime;
     public string firedBy;
     
     // Use this for initialization
     void Start () 
     {}
     
     // Update is called once per frame
     void Update () 
     {
         transform.Translate(Vector3.up* speed * Time.deltaTime);
         currentFlightTime += Time.deltaTime;
     }
     
     public void setFiredBy(string shooter)
     {
         firedBy = shooter;
         
 //        GameObject shooterObj = GameObject.Find(shooter);
         
 //         Physics.IgnoreCollision(collider, shooterObj.collider);
     }
 }

this is my bullet script, i understand that my bullets keeps firing upwards, but how do i make them fire according to the direction of my turret

Comment
Add comment · Show 10
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 robertbu · Mar 28, 2014 at 03:42 PM 0
Share

The solution will be in your Spawn code. It will need to rotate the bullet to face the direction to be fired. Note this would be much easier if you constructed your bullet so that the front was forward rather than up. That is the front of the bullet faced positive 'z'. Also note that by using 'Transform.Translate()', you may have trouble detecting collisions.

avatar image Mint92 · Mar 28, 2014 at 03:56 PM 0
Share

this is my spawn code

 GameObject bullet = (GameObject)Instantiate(bulletPrefab, transform.Find("TurretPivotControl/Turret/BulletSpawnPoint").transform.position,transform.rotation);@robertbu
avatar image Leuthil · Mar 28, 2014 at 04:02 PM 0
Share

It should take the rotation from the turret if your spawn code is from the turret. So really you just need to use Vector3.forward ins$$anonymous$$d of Vector3.up... if your bullet is rotated correctly when imported into Unity.

avatar image Mint92 · Mar 28, 2014 at 04:07 PM 0
Share

ive changed it to vector3.forward but its firing from the side alt text

test1.jpg (28.0 kB)
avatar image robertbu · Mar 28, 2014 at 06:34 PM 1
Share

While we could talk you through some solution given the current rotation of your bullet, the best solution is to fix the mesh. You can do that by:

  1. Change it in your modeling program before importing.

  2. $$anonymous$$ake the visible game object a child of an empty game object. The empty game object should be located at the pivot point of the visible object. When the empty game object has a rotation of (0,0,0), the front of the visible game object should be facing positive 'z'.

  3. You can use the Rotate$$anonymous$$esh editor script. This script creates a clone of the original with the new rotation. You will find the script in my answer here.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

C# - 2d --I have two questions... both seem like they should be incredibly simple - script hive mind + MoveTo not follow 1 Answer

3D nested Turret Prefab Rotation 1 Answer

How to rotate an object to face the direction it's going? 1 Answer

Distribute terrain in zones 3 Answers

vector3.forward doesn't work when used in Rigidbody? 2 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