• 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 /
avatar image
0
Question by kems932000 · Jan 13 at 05:17 PM · movement

Move and rotate X or Z axis but not diagonally!

Hello guys I'm new to programming and I have the following issue:

My player is a tank and I want this tank to be able to move and rotate to the direction is moving and keep that rotation till the player changes direction on the X an Z axis but not diagonally! The inspiration for this movement comes from an old SNES game named Battle City . My script looks as follows:

public class PlayerController : MonoBehaviour { public float speed = 2f; public GameObject bulletPrefab; public Transform fireBullet; bool isReadyToInstantiate;

 // Start is called before the first frame update
 void Start()
 {
     isReadyToInstantiate = true;
 }

 // Update is called once per frame
 void Update()
 {
     float horizontalInput = Input.GetAxis("Horizontal");
     float verticalInput = Input.GetAxis("Vertical");

     Vector3 moveDirection = new Vector3(horizontalInput, 0, verticalInput);
     moveDirection.Normalize();

     transform.Translate(moveDirection * speed * Time.deltaTime, Space.World);

     if(moveDirection != Vector3.zero)
     {
        transform.forward = moveDirection;
     }

     if (isReadyToInstantiate && Input.GetKeyDown(KeyCode.Space))
     {
          StartCoroutine(PreventSpam());
          Instantiate(bulletPrefab, fireBullet.position, fireBullet.rotation);
     }
 }

 IEnumerator PreventSpam() 
 {
     isReadyToInstantiate = false;
     yield return new WaitForSeconds(1);
     isReadyToInstantiate = true;
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by kems932000 · Jan 15 at 12:34 PM

I've finally got a solution for my problem with the following modifications :

void Start() { isReadyToInstantiate = true; }

 // Update is called once per frame
 void Update()
 {
     float horizontalInput = Input.GetAxis("Horizontal");
     float verticalInput = Input.GetAxis("Vertical");

     Vector3 moveDirection = new Vector3(horizontalInput, 0, 0);
     Vector3 moveDirection2 = new Vector3(0, 0, verticalInput);
     

     moveDirection.Normalize();



     if (moveDirection != Vector3.zero)
     {
         transform.forward = moveDirection;
         transform.Translate(moveDirection * speed * Time.deltaTime, Space.World);


     }
     else if (moveDirection2 != Vector3.zero)
     {
         transform.forward = moveDirection2;
         transform.Translate(moveDirection2 * speed * Time.deltaTime, Space.World);

     }
   
     if (isReadyToInstantiate && Input.GetKeyDown(KeyCode.Space))
     {
          StartCoroutine(PreventSpam());
          Instantiate(bulletPrefab, fireBullet.position, fireBullet.rotation);
     }


 }

 IEnumerator PreventSpam() 
 {
     isReadyToInstantiate = false;
     yield return new WaitForSeconds(1);
     isReadyToInstantiate = true;
 }
Comment
Add comment · Show 1 · 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 kems932000 · Jan 15 at 12:37 PM 0
Share

Exclude moveDirection.Normalize(); I forgot to remove it!

avatar image
0

Answer by CmdrZin · Jan 13 at 05:43 PM

You should review this tutorial. https://learn.unity.com/project/tanks-tutorial
But basically, you want to use Vector3.forward to move in the direction your tank is pointing.
https://docs.unity3d.com/ScriptReference/Transform-forward.html shows you how and shows how to rotate also.
Have fun.

Comment
Add comment · Show 1 · 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 kems932000 · Jan 13 at 08:33 PM 0
Share

I think is a bit tricky what I try to accomplish I tried to implement a solution based on the documentation you mentioned for Transform.forward but its rotating my object in place and I have to apply the movement with the up arrow key . But what I want exactly to achieve is when I use a direction key the player object must rotate to exactly 90 degrees in that direction and with the same key to keep that direction when moving. I think it needs some math calculation but I'm yet a beginner and my mind cant grasp the concept on how to achieve this... Sorry for this big comment sir!

avatar image
0

Answer by djexstas9 · Jan 13 at 05:56 PM

first thing that came to mind:

         float horizontalInput = Input.GetAxis("Horizontal");
         float verticalInput = Input.GetAxis("Vertical");
         Vector3 moveDirection = Mathf.Abs(horizontalInput) > Mathf.Abs(verticalInput) ? Vector3.right * horizontalInput : Vector3.forward * verticalInput;

         // Or more readable

         if (Mathf.Abs(horizontalInput) > Mathf.Abs(verticalInput))
         {
             moveDirection = Vector3.right * horizontalInput;
         }
         else
         {
             moveDirection = Vector3.forward * verticalInput;
         }
Comment
Add comment · Show 1 · 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 kems932000 · Jan 13 at 08:02 PM 0
Share

I used the mentioned code, I chose the more readable version but after playing the scene the player is not moving at all and I have no errors on the console. Maybe I did something wrong?

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

165 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 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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Making a bubble level (not a game but work tool) 1 Answer

Reducing overhead while controlling many units 0 Answers

Stop moving GameObject 1 Answer

Dynamic wall movement 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges