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 Enut · Nov 14, 2012 at 07:49 PM · javascriptcubevertical

How do you move a cube with javascript

Hi guys, I am new to Unity and I wanted to ask how do I translate the position of a cube that I made with script. I am just testing out small little scripts to see how things work. Here is my code:

 var myCube: GameObject;
 var charCube: GameObject;
 
 var x = 0;
 var y = 1;
 var z = 0;
 
 function Start () {
 
      var aPositions : Array = [new Vector3(0,0,0),
                                       new Vector3(0,0,1),
                                       new Vector3(0,0,2),
                                       new Vector3(0,0,3),
                                       new Vector3(0,0,4),
                                       new Vector3(1,0,1),
                                       new Vector3(1,0,2),
                                       new Vector3(1,0,3),
                                       new Vector3(1,0,4)];
  
      var rot : Quaternion = Quaternion.identity;
  
      for(var i=0; i<aPositions.length; i++)
      {
           Instantiate(myCube, aPositions[i], rot);
      }
      Instantiate(charCube, new Vector3(x,y,z), rot);
 }
 
 function Update () {
     if (Input.GetButtonDown("Vertical"))
         //I want the charCube to move forward if I pressed 'w'.
         
 }

I tried a lot of things and I am sure that it is not too difficult. But I just can't wrap my finger around it. Any help would be appreciated. Thanks guys!

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
1

Answer by Peterw301 · Nov 14, 2012 at 08:05 PM

 function Update() {
     // Move the object forward along its z axis 1 unit/second.
     transform.Translate(Vector3.forward * Time.deltaTime);
 
     // Move the object upward in world space 1 unit/second.
     transform.Translate(Vector3.up * Time.deltaTime, Space.World);
 }

look at http://docs.unity3d.com/Documentation/ScriptReference/Transform.Translate.html for more :)

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 Enut · Nov 15, 2012 at 12:40 AM 0
Share

I actually tried that. Well, I guess I should be clearer. So each myCube is like a tile. So if I wanted charCube to move from tile to tile ins$$anonymous$$d of move forward an indefinite amount, how should I go about it without using Lerp. As of now, charCube is Vector3(x, y, z) where x = 0, y=1, z=0 and I would like each time I press W, x+=1.

avatar image
0

Answer by Screenhog · Nov 14, 2012 at 08:05 PM

If it's just a case of moving it, you'll want Transform.Translate.

However, if you want it to detect collisions on the way, then that probably won't work for you, and you'll need to do some kind of physics-based solution, like Rigidbody.AddForce.

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

11 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

Related Questions

How to make a Cube with a texture in a JavaScript? 0 Answers

Up and Down Cubes Shaking Issue. 0 Answers

Start Animation in code is not working 1 Answer

Making cube a child of an object. 1 Answer

Detect face of cube clicked 4 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