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 TahaCY · Dec 07, 2013 at 07:42 PM · javascriptobjecttwo

Controlling two object with one Javascript

I want to control my two object with one script (different key). I thought, I should define one control setting to one of my object and another controller setting for another object.

(w,a,s,d) for one object (up,down,left,right) for another object.

How can i do it?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by OrbitSoft · Dec 07, 2013 at 08:15 PM

It's pretty easy using Input.GetKey and KeyCode(which contains values for letters, arrows etc.)

 var Object1 : Transform;
 var Object2 : Transform;
 
 function Update()
 {
 
 // WASD
   if (Input.GetKey(KeyCode.W))
   {
     Object1.transform.position.Translate(x,y,z);
   }
   if (Input.GetKey(KeyCode.A))
   {
     Object1.transform.position.Translate(x,y,z);
   }
   if (Input.GetKey(KeyCode.S))
   {
     Object1.transform.position.Translate(x,y,z);
   }
   if (Input.GetKey(KeyCode.D))
   {
     Object1.transform.position.Translate(x,y,z); 
   // substitute all the x,y,z with wanted values (try 0.05)
   }
 
 // Arrows
   if (Input.GetKey(KeyCode.UpArrow))
   {
     Object2.transform.position.Translate(x,y,z);
   }
   // And so on.. Just substitute UpArrow with DownArrow, Left Arrow and Right Arrow
 }
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 prof · Dec 07, 2013 at 08:25 PM 0
Share

Thats a lot of code dublication. What if you have 4 players? ) Use variables ins$$anonymous$$d of $$anonymous$$eyCode and you can use same script for different objects

avatar image
0

Answer by FPSworrior · Dec 07, 2013 at 08:30 PM

All you have to do is change the axis. Make it where you vertical and horizontal axis only work with w,a,s,d and then you make two more axis called horizontal 2, and vertical 2. Make those only work with up arrow, down arrow, left arrow, right arrow. You write the code like this.

 if(Input.GetAxis("Horizontal"))
 {
  //Move left and right
 }
 
 if(Input.GetAxis("vertical"))
 {
  //Move forward and back
 }

If you would like I can provide more sample code for you, and show you how to change the axis.

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

19 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

Related Questions

Setting a variable to an instantiated object. 1 Answer

Drawing a 3D object javascipt 0 Answers

Disable GameObject Only Father Not Children 2 Answers

Making a rigidbody cube snap 90 deg at a time 1 Answer

Setting Scroll View Width GUILayout 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