• 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 /
  • Help Room /
avatar image
0
Question by Borzi · May 19, 2013 at 09:25 PM · instantiateprojectiletransform.forward

transform.forward problem

Hey everyone, I've been having some difficulties with a projectile script thats really been causing problems for me. I've finally managed to instantiate the projectile in the correct direction with the ray pointing in the right direction as well, but ever since I started using "transform.forward", the bullet flys in all kinds of directions. Does anyone know how to stop it from doing that?

 using UnityEngine;
 using System.Collections;
 
 public class MusketBullet : MonoBehaviour {
 
     //Variables Start___________________________________
  
  
     //The explosion effect is attached to this
     //in the inspector
  
     public GameObject mudParticle;
  
  
     //A quick reference.
  
     private Transform myTransform;
  
  
     //The projectiles flight speed.
  
     private float projectileSpeed = 20;
  
  
     //Prevent the projectile from causing
     //further harm once it has hit something.
  
     private bool expended = false;
  
  
     //A ray projected in front of the projectile
     //to see if it will hit a recognisable collider.
  
     private RaycastHit hit;
  
  
     //The range of that ray.
  
     private float range = 1.5f;
  
  
     //The life span of the projectile.
  
     private float expireTime = 10;
  
  
     //Variables End_____________________________________
  
     // Use this for initialization
     void Start()
     {
     myTransform = transform;
         
     transform.Rotate(0.0f, 270.0f, 0.0f);
  
    //As soon as the projectile is created start a countdown
    //to destroy it.
  
     Destroy (gameObject, expireTime);
  
     }
  
     // Update is called once per frame
     void Update () 
     {
        //Translate the projectile in the up direction (the pointed
        //end of the projectile).
  
       myTransform.Translate(transform.forward * projectileSpeed * Time.deltaTime);
  
  
        //If the ray hits something then execute this code.
  
       Debug.DrawRay( myTransform.position, myTransform.forward * range, Color.red );
       if(Physics.Raycast(myTransform.position,myTransform.forward, out hit, range) && expended == false)
       {
          //If the collider has the tag of Floor then..
  
          Debug.Log( "Ray Hit (floorMud) : " + hit.collider.transform.tag );
          {
           expended = true;
  
  
           //Instantiate an explosion effect.
  
           Instantiate(mudParticle, hit.point, Quaternion.identity);
  
  
           //Make the projectile become invisible.
  
           myTransform.renderer.enabled = false;
          }
        }
     }
  
  
     IEnumerator DestroyMyselfAfterSomeTime()
     {
        //Wait for the timer to count up to the expireTime
        //and then destroy the projectile.
  
        yield return new WaitForSeconds(expireTime);
  
        Destroy(myTransform.gameObject);
     }
 }
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 Bunnybomb7670 · May 19, 2013 at 09:33 PM

The transform.forward goes from the transform.forward of the object that the script is attached to unless you state something elses transform.forward, I suggest you check that the front of your bullet is facing the blue arrow and also make sure that your myTransform is also facing forward.

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 Borzi · May 19, 2013 at 10:34 PM 0
Share

I actually figured out what I need to do, thank you for your help, I realized that I need to modify the gun firing script itself so that the bullet instantiates correctly.

avatar image
3

Answer by Eric5h5 · May 19, 2013 at 10:07 PM

Transform.Translate already uses the object's local space by default. Therefore it makes no sense to use transform.forward inside the Translate function. Use Vector3.forward instead, as shown in the code example for Translate in the Unity documentation.

Comment
Add comment · Show 2 · 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 Borzi · May 19, 2013 at 10:33 PM 0
Share

Okay thank you, I noticed that the problem lies in the instantiation itself though, or in this case, the actual gun firing script. Thank you for your advice though (actually tried Vector3.forward and got the same result).

avatar image Mashakal · Feb 25, 2016 at 03:18 PM 0
Share

This was exactly my problem! Thank you.

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

17 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

Related Questions

Projectile disappears instantly after appearing. 1 Answer

Is there any good sample code for shooting a projectile and collision detection? 0 Answers

Changing rotation of prefab 0 Answers

Instantiating prefabs, and making them move. (Projectiles) 0 Answers

Set Instanatiated Object's Rotation Towards Player 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