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 jthomas117 · Oct 02, 2013 at 04:49 PM · javascriptprogrammingbeginner

Creating a Pacman style game, would appreciate help.,My first real game, pac-man inspired. Appreciate help.

Hi everyone, so I am currently designing a Pacman inspired game, simply for its "simplicity". The only thing is, I haven't quite got the coding in the old noggin yet, going rough tutorials and getting there but it is a long battle.

My question is, I need a little kick start on a simple bit of code that makes the character, which is a sphere for now, move one unit to whichever key is pressed, and will continue to move if the key is held, otherwise he will stop. What I tried made the character just snap from one place to another instantly. Another thing I was considering is for the character to rotate to the directing he is moving, so turning 90 or 180 degrees as you press a different direction key. Btw, I am using and learning JavaScript.

Sorry to be a pain, I really want to start making something simple to begin with so that I can learn better by doing it without tutorials, but it seems that a little to unrealistic for my current level of programming knowledge.

Thanks for any help everyone, it means a lot!,

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
1

Answer by robertbu · Oct 02, 2013 at 06:20 PM

Here is a bit of code to get you started. It use the Horizontal and Vertical axes, which are assigned to the arrow keys and ASDW by default. The code points the 'up' of your model in the direction of movement. Adjust 'moveDist' and 'speed' in the inspector as needed for your game.

 #pragma strict
 
 var target : Vector3;
 var moveDist = 0.1; // Amount to move 
 var speed = 0.1;     // Units moved per second
 
 function Start() {
     target = transform.position;
 }
 
 function Update() {
 
     if (transform.position == target) {
         var f = Input.GetAxis("Horizontal");
         if (f != 0.0) {
             var dir = Vector3.right * moveDist * Mathf.Sign(f);
             target += dir;
             transform.up = dir;  // Rotation
         }
         else {
             f = Input.GetAxis("Vertical");
             
             if (f != 0.0) {
                 dir = Vector3.up * moveDist * Mathf.Sign(f);
                 target += dir;
                 transform.up = dir;  // Rotation
             }            
         }
     } 
 
     transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
 }
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

14 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

Related Questions

Multiple Cars not working 1 Answer

Procedural Terrain - Diamond Square Algorithm 1 Answer

Is this a good source to learn javascript? 1 Answer

In-depth Animation and Audio guide for Enemy AI? 1 Answer

How angry bird's "GAME OVER" system work? 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