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 lampshade · May 20, 2010 at 12:32 AM · movingforward

Moving Cube Forward

Hi,

What I have done so far is made a C# Script called player (player being the cube object) and coded it in the following way:

using UnityEngine; using System.Collections;

public class Player : MonoBehaviour {

public float PlayerSpeed; void Start() { } // Update is called once per frame void Update () { // Amount to Move float MoveHorizontal = Input.GetAxis("Horizontal") PlayerSpeed Time.deltaTime; float MoveVertical = Input.GetAxis("Vertical") PlayerSpeed Time.deltaTime;

 // Move the Player
 transform.Translate(Vector3.right * MoveHorizontal);
 transform.Rotate(0, PlayerSpeed * MoveVertical, 0);

}

With the code above, hopefully you will see that I did get the cube (Player) moving about the x and y-axis'.

I have visited the Input Manager under Edit->Project Options->Input and made it so I can have the keys that I wanted, for the movement about the vertical and horizontal axis.

I'm wondering now that if their is a z-axis that I am missing under the Input Manager? I have checked around on the net and it seems that what some people are doing is just rotating the position in some way to move forward..

But, so far so good.

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

3 Replies

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

Answer by lampshade · May 20, 2010 at 04:28 AM

This is the solution, however, RotateSpeed needs to be tweaked a little, e.g. faster.

using UnityEngine; using System.Collections;

public class Player : MonoBehaviour {

public float MoveSpeed = 10; public float RotateSpeed = 40;

void Start() { } // Update is called once per frame

void Update () { // Amount to Move float MoveForward = Input.GetAxis("Vertical") MoveSpeed Time.deltaTime; float MoveRotate = Input.GetAxis("Horizontal") RotateSpeed Time.deltaTime;

 // Move the player
 transform.Translate(Vector3.forward * MoveForward);
 transform.Rotate(Vector3.up * MoveRotate);

}

}

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 Eric5h5 · May 20, 2010 at 01:26 AM

You can add any number of axes in the input manager and do whatever you like with them, including making a z-axis control.

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 lampshade · May 20, 2010 at 01:42 AM 0
Share

Thanks for the reply; would you be refferring to the Add Axis feature in the Input $$anonymous$$anager? I have tried adding a '3rd Axis' (Joysticks and ScrollWheel), but I'm not to sure about that.

avatar image Eric5h5 · May 20, 2010 at 02:32 AM 0
Share

@lampshade: no, that 3rd axis is the actual 3rd axis on a joystick or gamepad (or it's the scroll wheel on a mouse). Just add another axis in the input manager, and the controls can be whatever you want. Although if your question is about replacing the left/right movement with forward/backward, then change your script so it's using Vector3.forward ins$$anonymous$$d of Vector3.right. The names of the axes in the input manager are merely descriptive, they don't dictate which axis your objects move on. That's up to your code.

avatar image
0

Answer by Jon 1 · May 20, 2010 at 02:04 AM

Okay, I work with JavaScript and no nothing about C# so I do not know if this only applies to JavaScript, but the way I do it is this:

if(Input.GetButton("W")) { transform.position = transform.forward PlayerSpeed Time.deltaTime }

This would make the GameObject move forward when the W key is pressed, as in a WASD control scheme. However, like I said, if this is only JavaScript stuff I heartily apologize to you :)

Comment
Add comment · Show 3 · 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 lampshade · May 20, 2010 at 02:16 AM 0
Share

Thanks for the reply, I found exactly what I need to happen at the following link:

http://www.scottpetrovic.com/blog/2009/11/unity3d-3rd-person-basic-movementrotation-wsource/

It's a shame for me that it is written in JS! I've been trying to write it in C#Script..No luck so far

avatar image lampshade · May 20, 2010 at 02:22 AM 0
Share

For some reason your the if statement prevents the other lines of code to function properly...And no apology is necessary, I appreciate the reply ;)

avatar image fireDude67 · Jul 08, 2010 at 09:00 PM 0
Share

The above code works with c# with no changes

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

No one has followed this question yet.

Related Questions

How do I launch a object with another object? 0 Answers

Moving game objects 2 Answers

Having trouble moving character backwards... 1 Answer

Truck moves sideways when using transform.forward 1 Answer

Moving character....northwest...? 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