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 FrowningPigeon · Aug 24, 2013 at 02:08 PM · movementrigidbodycube

Trouble with moving cube when key is pressed

Hi all,

I have been following a tutorial which explains how to create a 'runner' style game; but I do not want my cube to constantly be in motion. INstead I want it to move when I press 'd'.

Here is the script:

using UnityEngine; using System.Collections;

public class RunnerScript : MonoBehaviour {

 public float accelaration = 5;
 public float maxSpeed = 10;
 private bool touchGround = false;
 public Vector3 JumpVel;
 public Vector3 ForwardVel;
 
 // Update is called once per frame
 void Update () {
     
     if (Input.GetButtonDown("Jump") && touchGround){
         rigidbody.AddRelativeForce(JumpVel, ForceMode.VelocityChange);            
         touchGround = false;
     }
     
             
     var currentSpeed = rigidbody.velocity;
     currentSpeed.y = 0;
     
     //Here is where I am having trouble
     
     if (touchGround && currentSpeed.magnitude < maxSpeed){
     rigidbody.AddRelativeForce(accelaration, 0, 0);
     }

//To here

 }
 
 //If the player touches the ground
 void OnCollisionEnter(){
         touchGround = true;
 }

}

See the comment '//Here is where I am having trouble', that is currently what I am using to move the cube; which works fine in a sense. But when I put the condition 'Input.getKeyDown('d))' on the end of that; it does not work.

Anyone have any suggestions?

Thanks,

Callum

/ UPDATE /

Ok, I now have it moving when i press a key; but the issue now is that it only moves when i press and release the key. Like it is only happening once every update.

Is there a way to have it so that when i hold the key down, the cube moves?

Thanks.

The code I am using:

if (Input.GetKeyDown("d")){ currentSpeed.x = 100;
rigidbody.AddRelativeForce(accelaration, 0, 0); }

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 robertbu · Aug 24, 2013 at 04:13 PM

There are a couple of ways you can deal with this issue. The first is you use GetKeyDown() and GetKeyUp() to set the velocity:

 if (Input.GetKeyDown(KeyCode.D)) {
     Vector3 vel = new Vector3(10.0f, 0.0f, 0.0f);
     vel = transform.TransformDirection(vel);
     rigidbody.velocity = vel;
 }
 
 if (Input.GetKeyUp(KeyCode.D)) {
     rigidbody.velocity = Vector3.zero;
 }

An alternate solution would be to crank up the 'Drag' setting of the rigidbody in the inspector to something like 15. Then you use this code instead.

 if (Input.GetKey(KeyCode.D)) {
     Vector3 vel = new Vector3(10.0f, 0.0f, 0.0f);
     vel = transform.TransformDirection(vel);
     rigidbody.velocity = vel;
 }

This will result in a small amount of movement after the key is lifted, which often looks more natural.

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

15 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

Related Questions

Getting cube movement down 2 Answers

Cube rotates to the left on its own when moving 1 Answer

Rigid body child Player movement 0 Answers

How can I get a responsive rigidbody FPS controller without acceleration that reacts to forces? 2 Answers

How do I make a cube rigidbody pushable by the default 3rd person character controller without tilting and going left and right? 0 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