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 :)
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
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; } }
Your answer
![](https://koobas.hobune.stream/wayback/20220612111733im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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