Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 mirbatis · Nov 16, 2018 at 08:20 PM · scripting problemkeycodegamespong

Problem coding script for paddles in Pong without using any physics.

Hi, I'm having some trouble trying to create Pong from zero and I'd like to recreate the game without using any physics. (No rigidbody or collider components)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Paddle : MonoBehaviour {
 
         public KeyCode up, down;
         public float speed;
         private CharacterController movement;
 
     // Use this for initialization
     void Start () {
 
         movement = GetComponent<CharacterController>();
     }
     
     // Update is called once per frame
     void Update () {
 
         Vector3 move = new Vector3(0, Input.GetKeyCode, 0);
         movement.Move(move * Time.deltaTime * speed);
 
     }
 }

This is so far my script, I'm having trouble in the following line:

 Vector3 move = new Vector3(0, Input.GetKeyCode, 0);

I'd like to use the same script for both paddlers (Writing in the inspector the keys I'd like to use for movement), yet I'm unable to figure out a way to summon GetKeyCode in that line.

Any help please? It'll be highly appreciated.

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 Tomer-Barkan · Nov 16, 2018 at 08:30 PM

First, I don't see a reason to use CharacterController. Simply change the transform position with transform.Translate instead of movement.Move.

Second, Input.GetKeyCode is not a valid method.

What you could do is the following:

  public class Paddle : MonoBehaviour {
  
          public string axis;
          public float speed;
  
      void Update () {
          Vector3 move = new Vector3(0, Input.GetAxis(axis) * Time.deltaTime * speed, 0);
          transform.Translate(move);
      }
  }
 

set "axis" to the name of the axis for this player (you need to define the axis in the Input Manager window), and you can set a different one for each paddle. Note that I also multiplied the axis value by Time.deltaTime (the frame length) and the speed.


If you don't want to use axis, and instead set keys, then you can use the following:

   public class Paddle : MonoBehaviour {
           public string up, down;
           public float speed;
   
       void Update () {
           KeyCode upKey, downKey;
           upKey = (KeyCode )System.Enum.Parse(typeof(KeyCode ), up);
           downKey= (KeyCode )System.Enum.Parse(typeof(KeyCode ), down);
           float yMove = Input.GetKey(upKey) ? 1 : (Input.GetKey(downKey) ? -1 : 0);
           Vector3 move = new Vector3(0, yMove  * Time.deltaTime * speed, 0);
           transform.Translate(move);
       }
   }

Then you set the "up" and "down" for each paddle, as string. The possible values can be found here.

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 mirbatis · Nov 16, 2018 at 08:57 PM 0
Share

(Yeah, I'd really like to use the set keys.)

Also, thanks for the reply! However, the $$anonymous$$onodevelop Unity uses doesn't have 'Enum.TryParse'... "'Ennum' doesn't contain a definition for 'TryParse'." Should I use 'Enum.IsDefined'?

avatar image Tomer-Barkan mirbatis · Nov 16, 2018 at 09:06 PM 0
Share

Alright, edited to support .net 3.5 (Parse ins$$anonymous$$d of TryParse)

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

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

Related Questions

Detecting Pressed key and returning a KeyCode Variable 2 Answers

How do I change controls by pressing one button? 0 Answers

My ball in pong passes through the paddles. 3 Answers

Check if pressed key exists inside an array of KeyCodes 2 Answers

How can make the player move constantly when the key A or D is held down? 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