- Home /
 
How can I make my character go forward based on the direction it is facing in 2d C#
I am trying to make my character go forward (up on x axis) whenever I press W. I have tried looking, but everyone said use transform.forward, but that has not seemed to work. Here is my script with transform.forward.
using UnityEngine;
public class MoveRotate : MonoBehaviour { Rigidbody m_Rigidbody; public float m_Speed;
 void Start()
 {
     //Fetch the Rigidbody component you attach from your GameObject
     m_Rigidbody = GetComponent<Rigidbody>();
     
    
 }
 void Update()
 {
     if (Input.GetKey(KeyCode.UpArrow))
     {
         //Move the Rigidbody forwards constantly at speed you define (the blue arrow axis in Scene view)
         m_Rigidbody.AddForce(transform.forward * Time.deltaTime * m_Speed);
     }
     if (Input.GetKey(KeyCode.DownArrow))
     {
         //Move the Rigidbody backwards constantly at the speed you define (the blue arrow axis in Scene view)
         m_Rigidbody.AddForce(-transform.forward * Time.deltaTime * m_Speed);
     }
     if (Input.GetKey(KeyCode.RightArrow))
     {
         //Rotate the sprite about the Y axis in the positive direction
         transform.Rotate(new Vector3(0, 0, -1) * Time.deltaTime * m_Speed, Space.World);
     }
     if (Input.GetKey(KeyCode.LeftArrow))
     {
         //Rotate the sprite about the Y axis in the negative direction
         transform.Rotate(new Vector3(0, 0, 1) * Time.deltaTime * m_Speed, Space.World);
     }
 }
 
               }
also if there is any confusion, disregard the part where it says "(Up on the x axis)"
try
transform.up ins$$anonymous$$d of transform.forward
Answer by avip805 · Sep 06, 2018 at 12:11 AM
I can succesfully turn, but can not go forward after that. All help is appreciated!
also if there is any confusion, disregard the part where it says "(Up on the x axis)"
Your answer
 
             Follow this Question
Related Questions
turn fish3d follow mouse 0 Answers
Bank Camera Based On Rotation Delta 1 Answer
Why can't I turn more then 45 degrees 0 Answers
Problem turning character on a sloped surface. 1 Answer
Turn character the way I am moving 2 Answers