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 Kenshen112 · Apr 26, 2013 at 03:38 AM · rpgkey

C# Capture Keypress

I'm making an RPG random Battle system and I would love to have the player enter a battle after taking a certain number of steps.

My code is:

 using UnityEngine;
 using System.Collections;
 
 public class Battle : MonoBehaviour
 {
     int Steps;
     bool Go;
     
     void Counter()
     {
        Steps = Steps + 10;

         if (Steps == 100)
         {
             Go = true;
         }
         if (Go = true)
         {
             Application.LoadLevel("Battle1");
         }
     } 
 }

How do I make it so that when I press KeyCode.UpArrow, Left, Right Or Down Steps adds 1?

Comment
Add comment · Show 2
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 Loius · Apr 26, 2013 at 05:06 AM 0
Share

http://docs.unity3d.com/Documentation/ScriptReference/Input.html

avatar image Ben-Stoneman ♦♦ · Jun 16, 2014 at 02:27 PM 0
Share

also:

 if (Go == true)
         {
             Application.LoadLevel("Battle1");
         }

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ExpiredIndexCard · Apr 26, 2013 at 05:14 AM

 if(Input.GetKey(KeyCode.UpArrow))
   Steps++;
Comment
Add comment · Show 2 · 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 Kenshen112 · Apr 26, 2013 at 03:08 PM 0
Share

It doesn't work nothing happens

avatar image richardalgor · Jun 16, 2014 at 11:38 AM 0
Share

check here more about C# keypress...C# $$anonymous$$eyPress Event

Vayne

avatar image
0

Answer by Krizzu · Jun 16, 2014 at 12:07 PM

 void Update()
 {
    if(Input.GetKeyDown(KeyCode.Space))
       Steps++;
 }

With if You check if there was button pressed and as argument you pass which key exacly was pressed (in this case it's Space). If it's true ( if space was pressed) then add steps. (Steps++ == Steps=Steps+1;)

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
avatar image
0

Answer by Ben-Stoneman · Jun 16, 2014 at 12:56 PM

Hi KenShen,

You do not want to count steps this way. Yes, the player moves when the key is pressed down, however the player does not move forward by tapping the up key. You need to work out a way that best suits you to count the steps of the player.

A suggestion would be to get the position of the player when the game starts then add it to slot one. Now in slot one we have (0,0,0) then in update we get the position again and compare it with the previous position:

(0,0,0) -> (0,2,0)

We pass this to a function to compare each vector and count the difference. So say each step equalled "0.5" then the StepCount function would see (0 steps, 4 steps, 0 steps).

The StepCount function then replaces the previous position with the current position and repeats.

 using UnityEngine;
 using System.Collections;
 
 public class StepCounter : MonoBehaviour {
 
     public Vector3 lastPosition;
     public float stepLength;
     float xSteps;
     float ySteps;
     public float stepsTaken;
     public bool countingSteps = true;
 
     // Use this for initialization
     void Start () 
     {
         lastPosition = gameObject.transform.position;
         StartCoroutine (StepCountUpdate ());
     }
 
     private IEnumerator StepCountUpdate()
     {
         while (countingSteps == true)
         {
             yield return new WaitForSeconds(4);
             Vector3 currentPos = gameObject.transform.position;
             StepCount (currentPos);
         }
     }
 
     //Counts the steps made
     public void StepCount(Vector3 currentPosition) 
     {
         // calculates the steps made in the X Axis.
         float x = currentPosition.x - lastPosition.x;
         x = Mathf.Round(x);
         if (x < 0) 
         {
             xSteps = x / stepLength;
             xSteps = Mathf.Abs (xSteps);
         } 
         else 
         {
             xSteps = x / stepLength;
         }
 
         // calculates the steps made in the X Axis.
         float y = currentPosition.y - lastPosition.y;
         y = Mathf.Round(y);
         if (y < 0) 
         {
             ySteps = y / stepLength;
             ySteps = Mathf.Abs (ySteps);
         } 
         else 
         {
             ySteps = y / stepLength;
         }
 
         stepsTaken = stepsTaken + xSteps + ySteps;
 
         lastPosition = currentPosition;
     }
 }

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

15 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

Related Questions

Multiple Cars not working 1 Answer

error CS1023: An embedded statement may not be declaration or labeled statement 1 Answer

Help calling a code in C# script 1 Answer

Add force to GameObject 2 Answers

Block Placement Help 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