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 DesertPhoenix · Apr 29, 2014 at 09:27 AM · instantiatetransform.forward

2D tank firing projectile in C# - transform.foward doesn't seem to work

Hi all. I've been following the tutorial below on how to create instances of a shell, and have it shoot off in the same direction of the barrel, however transform.forward doesn't seem to work to me. Multiplying the transform.forward by any amount seems to have no effect - the shell just falls to the ground.

https://unity3d.com/learn/tutorials/modules/beginner/scripting/instantiate

I'm using C#. If anyone could tell me what I'm doing wrong, that would be great... My code is below.

 using UnityEngine;
 using System.Collections;
 
 
 public class Fire : MonoBehaviour {
 
     public Rigidbody2D ArtyShell;
     public Transform EndOfTurret;
     public Vector2 ShellMove;
 
 
 
     void Example() {
                 animation.wrapMode = WrapMode.Once;
         }
 
     public KeyCode Fire_KC;
 
     private Animator animator;
 
     // Use this for initialization
     void Start () {
     animator = this.GetComponentInChildren<Animator> ();
     }
     
     // Update is called once per frame
     void Update () {
     
         
 //        if ((Input.GetKey (Fire_KC)) Input.GetKeyDown (Fire_KC)) {
         if (Input.GetKeyDown (Fire_KC)) {
 
         // turns on the int that triggers the animation
             animator.SetBool("FireYN", true);
 
             //creates a shell at the end of the turret, and gives it a push
             ****
         Rigidbody2D ShellInstance;
             **ShellInstance = Instantiate(ArtyShell, EndOfTurret.position, EndOfTurret.rotation) as Rigidbody2D;
             ShellInstance.velocity = transform.forward * 50000;**
         } 
         
         //turns off the int that turns off the animation
         else {
             animator.SetBool("FireYN", false);
         }
 
 
     }
 }
 
Comment
Add comment · Show 1
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 coldjalapeno · Apr 29, 2014 at 10:14 AM 0
Share

Different results if you use transform.right or left??

1 Reply

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

Answer by Jenesis · Apr 29, 2014 at 10:23 AM

Rigidbody2D.velocity is a Vector2 - it's only got velocity in two dimensions, as you'd expect. Transform.velocity, however, is a Vector3. So what you're doing on line 40 is assigning a Vector3 to a Vector2. Normally you wouldn't be able to do that, because they're different structs, but Vector2 defines an implicit conversion operator from Vector3. What happens when you do the assignment is that the x and y components of the Vector3 are copied into the Vector2, and the z component gets ignored. So the following code

 Vector2 foo;
 Vector3 bar=Vector3.forward; //(0,0,1)
 foo=bar;
 Debug.Log(foo);

will print (0.0, 0.0) to the console.

Now, what I suspect is happening is that transform.forward is returning (0,0,1) - that's the default orientation. So the line ShellInstance.velocity = transform.forward * 50000; assigns the vector (0,0,50000) to a Vector2, which gives you (0,0). Possibly you should be using EndOfTurrent.transform.forward rather than transform.forward, but in any event, it's the conversion that I think is giving you trouble. You can check easily enough by adding Debug.Log(transform.forward) in there and seeing what you get.

If you're doing some sort of sidescrolling tank game, which is what it sounds like from your description, then z is probably into the screen, and the game takes place in the x-y plane. If you're moving one end of the turret up and down and leaving the other end fixed, you're rotating it around the z-axis. So you might have to construct your 'forward' vector by rotating Vector3.right by EndOfTurrent.transform.eulerAngles.z, or something similar. There'll be a few extra things to handle - you'll have to change things if the tank faces the other way, for example - but that may get you on the right track.

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 DesertPhoenix · Apr 29, 2014 at 02:39 PM 0
Share

Thanks a lot - tried transform.right, and it works fine now. $$anonymous$$ight start working in debugging information too, looks like that might help me with future problems.

Still very new to this.

avatar image Jenesis · Apr 29, 2014 at 05:04 PM 0
Share

Learning to debug well is a vital program$$anonymous$$g skill. Things like Debug.Log are useful, but actually quite cumbersome to work with on a large scale. Look into breakpoints - these let you pause the program when you hit a certain line of code, and then used your code editor to exa$$anonymous$$e the state of the program at that point, which is really useful for spotting errors. Once you're done, you can resume the program and it'll carry on. The $$anonymous$$onoDevelop IDE that ships with Unity can debug in this way. If you're using another IDE, you'll have difficulty getting it to talk to Unity.

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

22 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

Related Questions

Trouble with rb.Addforce(transform.foreward) 3 Answers

Fire bullet in camera direction problem 3 Answers

Instantiate / transform.forward not working properly,Instantiate - Transform.position + Transform.forward gets further away if I am moving? Why? 1 Answer

Checking if object intersects? 1 Answer

Instantiate ground/enemies 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