Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 semi-mong · Mar 15, 2015 at 07:00 PM · boxtransformation

having multiple inputs using getkey

I have code that allows me to move a 2D box using the arrow keys and accomplished this via getkey. Im now need to be able tocode the box so that when the spacebar is held it changes size, is this possible?

UPDATE - i will post what code i have later tonight as unable to grab it.

Pretty much, i have a 2d cude that i can move using arrow keys, when the spacebar is held, i want to be able to transform the scale of the cube with the arrow keys.

Example - If the spacebar is held and arrow key up is being held/pushed, the box increase upwards in height and increase downwards in height if the spacebar is held and the down arrow key is held/pushed.

if the spacebar is held and the arrow keys left and right are pushed/held the box must increase in width.

UPDATE 2 - Thanks guys i've managed todo it! One last thing would be can anyone hint me in the direction of how to stop the increase and decrease at a certain point?

I.E up arrow is to increase in height and down arrow is to decrease in height, until when you decrease the box too much, it starts to flip and increase again? is there anyway to stop this so once the box hits its smallest value it cant flip and start growing in the opposite direction?

THANKS !

 using UnityEngine;
 using System.Collections;
 
 public class playermovement : MonoBehaviour 
 {
     public float speed = 1f;
     public Color newColor;
     
     void Start () 
     {
     }
     
     void Update () 
     {
         if (Input.GetKey (KeyCode.RightArrow))
             transform.position += new Vector3 (speed * Time.deltaTime, 0.0f, 0.0f);
         if (Input.GetKey (KeyCode.LeftArrow))
             transform.position -= new Vector3 (speed * Time.deltaTime, 0.0f, 0.0f);
         if (Input.GetKey (KeyCode.UpArrow))
             transform.position += new Vector3 (0.0f, speed * Time.deltaTime, 0.0f);
         if (Input.GetKey (KeyCode.DownArrow))
             transform.position -= new Vector3 (0.0f, speed * Time.deltaTime, 0.0f);
         if (Input.GetKey (KeyCode.Return))
         newColor = new Color(Random.value, Random.value, Random.value);
         renderer.material.color = newColor;
         
         if (Input.GetKey (KeyCode.Space))
         { 
             if (Input.GetKey (KeyCode.RightArrow))
             {
                 transform.localScale += new Vector3(0.05f,0,0);
             }
         }
         
         if (Input.GetKey (KeyCode.Space))
         { 
             if (Input.GetKey (KeyCode.LeftArrow))
             {
                 transform.localScale -= new Vector3(0.05f,0,0);
             }
         }
         
         if (Input.GetKey (KeyCode.Space))
         { 
             if (Input.GetKey (KeyCode.DownArrow))
             {
                 transform.localScale -= new Vector3(0,0.05f,0);
             }
         }
         
         if (Input.GetKey (KeyCode.Space))
         { 
             if (Input.GetKey (KeyCode.UpArrow))
             {
                 transform.localScale += new Vector3(0,0.05f,0);
             }
         }    
         
     }
 
 }
 

Comment
Add comment · Show 3
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 siaran · Mar 15, 2015 at 09:02 PM 1
Share

Yes, that's possible. ...if you need help with how, you should make your question a bit mroe specific.

avatar image LeftRight92 · Mar 15, 2015 at 09:07 PM 0
Share

As Siaran said, 'Yes' is about the best answer you can get unless you provide a few specifics:

  • What is your existing code?

  • When you press spacebar what exactly do you want to happen? Do you want the box to be bigger while the button is held and revert back to normal after? Do you want the box to grow bigger every time you press space?

avatar image semi-mong · Mar 16, 2015 at 11:06 AM 0
Share

Hello all, i will post what code i have later tonight as unable to grab it.

Pretty much, i have a 2d cude that i can move using arrow keys, when the spacebar is held, i want to be able to transform the scale of the cube with the arrow keys.

Example - If the spacebar is held and arrow key vup is being held/pushed, the box increase upwards in height and increase downwards in height if the spacebar is held and the down arrow key is held/pushed.

if the spacebar is held and the arrow keys left and right are pushed/held the box must increase in width.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ForeignGod · Mar 16, 2015 at 09:34 AM

Your question is very open, but i suppose you look for something like this.

http://docs.unity3d.com/ScriptReference/Transform-localScale.html

Take a look at this and try to use it with GetKey. I wont write the whole script for you, but you can learn alot from here. :)

EDIT: "As you updated your question"

Make an if statement inside your if statement

  if(Input.GetKeyDown(KeyCode.W))
  {
     if(Input.GetKeyDown(KeyCode.Space))
     {
        //Do something if both are pressed.
     }
  }

Not tested just an example on how to do it.

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

GUI Null Reference Exception? 2 Answers

How to make a combo box 4 Answers

Collision not working 1 Answer

I'm trying to add a hitbox to a player. Can I add it to the mesh? 3 Answers

Can someone help me with this script? 2 Answers


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