Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by BigPizzaPie · Feb 20, 2015 at 05:24 PM · movementarrowkeyssidescroll

2D multiple direction movement with arrows problem

Hello,

I am creating a 2D horizontal shmup and I can't find what's the problem with movement.

  1. Movement without firing works fine.

  2. Movement with firing (spacebar) with keys WASD works fine.

  3. Movement with firing (spacebar) with arrow keys when moving x axis positive (right) + y axis positive (up) works fine. Same with both axes negative.

  4. Movement with firing (spacebar) with arrow keys when axes are opposite (I.E. x negative, y positive) doesn't work. The character moves in single direction.

Here is the code (problem is probably within Update() function):

 using UnityEngine;
 using System.Collections;
 
 public class PlaneParameters : MonoBehaviour {
     public float fireRate = 0;
     public float Speed = 2;
     public float movementSpeed = 2;
     public Transform bulletPrefab;
     private float timeToFire = 0;
     private Transform firePoint;
 
     void Awake (){
         firePoint = transform.FindChild("FirePoint");
         if (firePoint == null){
             Debug.LogError ("Can't find Plane child FirePoint");
         }
     }
 
     void Start (){
     }
 
     // Update is called once per frame
     void Update () {
 
         // movement with camera
         transform.Translate(Vector3.right * Speed * Time.deltaTime);
 
         //player input movement
         if (Input.GetButton ("Horizontal")){
         transform.Translate(new Vector3 (Input.GetAxis("Horizontal") * movementSpeed * Time.deltaTime,0f,0f));;
         }
 
         if (Input.GetButton ("Vertical")){
         transform.Translate(new Vector3 (0f,Input.GetAxis("Vertical") * movementSpeed * Time.deltaTime,0f));
         }
 
         //restrictions within screen
         var pos = Camera.main.WorldToViewportPoint(transform.position);
         pos.x = Mathf.Clamp01(pos.x);
         pos.y = Mathf.Clamp01(pos.y);
         transform.position = Camera.main.ViewportToWorldPoint(pos);
 
         //firing
 
         if (Input.GetKey (KeyCode.Space) && Time.time > timeToFire){
             timeToFire = Time.time + 1/fireRate;
             Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
         }
     }
 
     void OnCollisionEnter2D(Collision2D col) {
         if (col.gameObject.tag == "Enemy")
             {
             Application.LoadLevel("restartScreen");
             }
         }
 }
 

 

Any ideas what's wrong?

Thanks in advance.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Gabriel Saldanha · Feb 20, 2015 at 06:17 PM

I think I did not understand the problem.. When you press, for example, Right (X-positive) and Down (Y-negative), the movement is happening in only one of these directions?

I guess you could you try removing the "if(GetButton("Vertical/Horizontal"))" before the

transform.Translate(new Vector3 (0f,Input.GetAxis("Vertical") movementSpeed Time.deltaTime,0f));

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 BigPizzaPie · Feb 23, 2015 at 10:45 AM 0
Share

Doesn't work. Well yeah, when you press right + down the movement works only to the direction you pressed first.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Isometric movement using arrow keys? 1 Answer

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

getkeydown keyboard arrows 1 Answer

Help with 2D AI scripting 2 Answers

How do I do left to right movement with ONLY the WASD keys? 1 Answer


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