• 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 Treven · Sep 01, 2015 at 06:05 PM · forwardaimingbehaviourfiringunknown

Why does my code shoot up but not forward?

I dont get why this is doing what it does my code is using UnityEngine; using System.Collections;

 public class ProjectileController : MonoBehaviour {
 
     public float fireSpeed;
     public Rigidbody2D myRigidbody2D;
     public PlayerController player;
 
     public float fireLife;
     public float fireLifeStore;
 
     // Use this for initialization
     void Start () {
         myRigidbody2D = GetComponent<Rigidbody2D> ();
         player = FindObjectOfType<PlayerController> ();
     }
     
     // Update is called once per frame
     void Update () {
         GetComponent<Rigidbody2D>().AddForce(transform.forward * fireSpeed);
         //myRigidbody2D.velocity = new Vector2 (fireSpeed, myRigidbody2D.velocity.y);
         fireLife -= Time.deltaTime;
 
         if (fireLife <= 0) 
         {
             Destroy(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

3 Replies

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

Answer by D3Duck · Sep 01, 2015 at 08:45 PM

Is the script attached to the projectile or a child?

Select the thing that it is attached to in the inspector. Make sure you use local for the gizmos and look where it is pointing at. Is it up or forward? (probably up because your script looks fine ;) )

Comment
Add comment · Show 5 · 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 Treven · Sep 02, 2015 at 06:57 AM 0
Share

Its attached to the projectile

and how do i check where it is point towards

aand found a wierd thing where it works when i use GetComponent().AddForce(transform.up * fireSpeed); but i dont want that direction.

avatar image D3Duck · Sep 02, 2015 at 10:09 AM 0
Share

So you fixed it?

Select your projectile in the scene view and see where the blue arrow points at. That's forward.

Also, make sure that when you instantiate you do so with Quaternion.Identity (no rotation).

avatar image Treven · Sep 02, 2015 at 12:35 PM 0
Share

nope its not fixed nd im not seeing a blue arrow only red right and green up

avatar image Treven · Sep 03, 2015 at 10:28 AM 0
Share

I figured it out i didnt need transform.forward i need transform.right which is the red line thanks all for the help.

avatar image D3Duck · Sep 03, 2015 at 11:03 AM 0
Share

$$anonymous$$y bad I didn't realize you were making a 2D game. Anyway, glad you found it.

avatar image
0

Answer by Jessespike · Sep 01, 2015 at 08:36 PM

transform.forward is referring to the ProjectileController. I'm guessing ProjectileController is on an object that doesn't move, I don't know how you set this up. What if you use the player's transform?

      GetComponent<Rigidbody2D>().AddForce( player.transform.forward * fireSpeed);
      
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 Treven · Sep 02, 2015 at 06:58 AM 0
Share

nope same result

avatar image
0

Answer by Orc_Horn_Productions · Sep 02, 2015 at 07:14 AM

Hi @Treven,

Might I make the suggestion that you attach your projectile controller to your player character and then use scripting to instantiate/move your projectile from it's desired location.

This would be a more common way of creating projectiles in game and probably easier to transfer onto new models/projects if you plan on future proofing your code.

You can get a good introduction to this concept in either of the following tutorials:

https://unity3d.com/learn/tutorials/projects/space-shooter/shooting-shots?playlist=17147 https://unity3d.com/learn/tutorials/projects/survival-shooter/harming-enemies?playlist=17144

I'd be more than happy to look over your script in more detail if there's anything your still not sure about and help anyway I can.

Ross

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 Treven · Sep 03, 2015 at 10:23 AM 0
Share

okay the tutorials didnt help and attaching the script to the player character did not help. is there a way to do transform.forward accross the x axis cause is only goes across the Z axis which dosent work in unity 2D.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

BCE0005: Unknown identifier: 'transform' 2 Answers

Essential math for games 2 Answers

EMERGENCY!!! PLEASE HELP!!!!! 3 Answers

DmC: Devil May Cry Dual Shot? 0 Answers

Why does Collider derives from Component instead of Behaviour ? 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