Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

How do I make something go in the direction it is facing Unity 2D

Discussion in '2D' started by Duranahanjr, Nov 7, 2020.

  1. Duranahanjr

    Duranahanjr

    Joined:
    Nov 7, 2020
    Posts:
    2
    Hi, I am pretty new to unity and I am trying to make a game where you need to fly around in a plane and shoot at other planes. So far my script is pretty simple and you can rotate the plane to turn left and right, but I want the plane to automatically go forward in the direction it is facing. This is my script so far:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Rotate : MonoBehaviour
    {
    Rigidbody2D body;
    public float degreesPerSec = 360f;

    void Start()
    {

    }


    void Update()
    {
    float rotationAmount = 0f;
    float currentRotation = transform.localRotation.eulerAngles.z;

    if (Input.GetKey(KeyCode.A))
    {
    rotationAmount = degreesPerSec * Time.deltaTime;
    }
    if (Input.GetKey(KeyCode.D))
    {
    rotationAmount = (degreesPerSec * -1f) * Time.deltaTime;
    }
    transform.localRotation = Quaternion.Euler(new Vector3(0, 0, currentRotation + rotationAmount));

    }
    }


    Any help would be very appreciated! Thank you!
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Duranahanjr likes this.
  3. Duranahanjr

    Duranahanjr

    Joined:
    Nov 7, 2020
    Posts:
    2
    Thank you so much for responding so quickly! I had tried that before but was using it wrong. It works perfectly now and the problem is resolved. Thanks so much!
     
    eses likes this.