Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • 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.

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

How to make object move forward and up simultaneously 1 Answer

transform.forward acting like vector3.forward 0 Answers

Ray does not rotate with gameobject when IK is enabled 1 Answer

Space Shooter - Asteroids moving randomly 1 Answer

Writing a simple script to rotate an object in direction of axis 0 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