• 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
2
Question by xXSilverswordXx · Feb 22, 2017 at 11:10 PM · transform.forward

Transform.Forward in 2D

Here is the code:

 using UnityEngine;
 using System.Collections;
 
 public class bulletactions : MonoBehaviour
 {
     private float yposV;
     private float xposV;
     public Rigidbody2D rb;
     private float PLAyposV;
     private float PLAxposV;
 
     // Use this for initialization
     void Start()
     {
         rb = GetComponent<Rigidbody2D>();
         // This stuff makes the object point towards the mouse pointer when it first spawns, but not again.
         var pos = Camera.main.WorldToScreenPoint(transform.position);
         var dir = Input.mousePosition - pos;
         var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
         //
         PLAyposV = GameObject.Find("Player").GetComponent<PlayerShoot>().playerYpos; //Finding the players X and Y position from another script and then moving the projectile to it.
         PLAxposV = GameObject.Find("Player").GetComponent<PlayerShoot>().playerXpos;
         transform.position = new Vector2(PLAxposV, PLAyposV);
     }
 
     void Update()
     {
         yposV = transform.position.y;
         xposV = transform.position.x;
         transform.position += transform.forward * Time.deltaTime;  // This line
         if (yposV == -200)
             Destroy(gameObject);
         if (yposV == 200)
             Destroy(gameObject);
         if (xposV == -200)
             Destroy(gameObject);
         if (xposV == 200)
             Destroy(gameObject);
     } //If the object goes out of map range, delete it.
 }

What this script is supposed to do: The script is attached to a missile entity that upon being cloned, faces towards the mouse pointer. Then, it will travel a speed of X in the direction it is currently facing until it goes out of the region and is destroyed.

What the script actually does right now The missile appears, rotates correctly, but does not move.

This is because i do not understand how to put transform.position += transform.forward * Time.deltaTime; // This line into action.

Please help! Thanks :)

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
9
Best Answer

Answer by navot · Feb 22, 2017 at 11:16 PM

transform.forward is the Vector pointing in the z Direction of the object. In 3D space, this is usually in front of you, but in 2D this points "into" the screen.

You need to replace it with either transform.right or transform.up depending on how your gameobject is rotated.

You can find which one either by trial and error or looking in the editor how it is rotated. The x axis is right and the y axis is up

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
avatar image
0

Answer by DRAGONBORN-AD · Sep 20, 2020 at 06:26 PM

Well, you can use this to get the proper direction: using System.Collections; using System.Collections.Generic; using UnityEngine; public class Bullet : MonoBehaviour { public Rigidbody2D m_RigidBody; public GameObject m_Gun; public float m_Speed; void Start() { Vector2 direction = transform.position - m_Gun.transform.position; m_RigidBody.velocity = direction * m_Speed; } }

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.

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

95 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

cannon ball direction when fired 0 Answers

transform.forward doesn't seem to be right? 2 Answers

Really need some help with my bullets firing opposite and not following rotation. 0 Answers

Projectile Direction issue - Sentry Turret - Autoaim 0 Answers

transform.forward with rotation offset variable? 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