• 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 Carson365 · Dec 16, 2017 at 02:29 PM · gameobjectposition

Moving +1 in the x position

Hello! I am wanting to make a cube move 1 in the x position. I've tried a couple of things but none have worked. If you know how to do this, please tell me below, Thanks.

Comment
Add comment · Show 1
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 navot · Dec 16, 2017 at 02:48 PM 0
Share

transform.position += new Vector3(1,0,0);

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by abhaychandna · Dec 16, 2017 at 03:44 PM

rb.addForce(transform.forward * speed);
where speed is a public variable. You can also write - rb.addForce(1,0,0,speed); - in place of transform.forward if the body moves on z-axis in place of x-axis.

Comment
Add comment · Show 7 · 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 Carson365 · Dec 16, 2017 at 03:57 PM 0
Share

Thanks for the answer. I did that and it isn't exactly working. When I press W, the x, y, and z go out of control but the player stays in the same spot.

Code:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Player : $$anonymous$$onoBehaviour {

 Rigidbody rb;
 //public GameObject player;
 public float speed;
 //private Animator forwardAnimation;

 // Use this for initialization
 void Start () {
     //forwardAnimation = player.GetComponent<Animator>();
     rb= GetComponent<Rigidbody>();
 }
 
 // Update is called once per frame
 void FixedUpdate () {
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W)) {
         //transform.position += new Vector3(1,0,0); 
         rb.AddForce(transform.forward * speed); 
     }
 }

}

avatar image abhaychandna Carson365 · Dec 16, 2017 at 04:03 PM 0
Share

Ins$$anonymous$$d of rb try writing GetComponent().AddForce(transform.forward * speed)

By x,y and z go out of control do you mean the co-ordinates? And if you mean the o-ordinates then how can they go out of control and the body not move?

Note that you have to have a rigidbody component attached to the cube for this to work.

avatar image Carson365 · Dec 16, 2017 at 04:14 PM 0
Share

I have done some testing and figured out it wasn't the code really. When I press play, my player cubes position coordinates change really fast to random numbers the first couple seconds then stop, but he stays in the same position. It's really weird and I am not sure why it is happening... Edit: Did more testing and figured out its just whenever I enable gravity it does it.

avatar image abhaychandna Carson365 · Dec 16, 2017 at 04:20 PM 0
Share

Try writing Debug.Log("Input working"); in the if statement and press W during runtime. If the console window shows Input working then the problem is with the Add force statement. OTherwise the problem is with the if statement

avatar image abhaychandna Carson365 · Dec 16, 2017 at 04:25 PM 0
Share

I wrote this from scratch , created a gamobject, attached a rigidbody component and disabled gravity and set the speed to 50 and it worked pretty good.

 using System.Collections;
 
 using System.Collections.Generic;
 
 using UnityEngine;
 
 
 public class t : $$anonymous$$onoBehaviour {
 
 
 
     #region Variables
     public float speed;
     
 #endregion
 
 #region Unity $$anonymous$$ethods
 
     void Start ()     
     {}
 
 
     void FixedUpdate ()     
     {
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W))
             GetComponent<Rigidbody>().AddForce(transform.forward * speed);
             
             
 }
 
 
 #endregion
 
 }


avatar image Carson365 abhaychandna · Dec 16, 2017 at 04:32 PM 0
Share

Again, it isn't stopping once it has gone +1 in the x position, it just keeps going on and on forever.

avatar image Carson365 · Dec 16, 2017 at 04:22 PM 0
Share

Ok, so if I disable gravity it really doesn't matter since I don't need it, so its working fine now. Back to moving the transform, it works but it continuously keeps moving in that direction, when I want it to stop once it has moved +1 in the x position. How would I achieve that?

Updated Code:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Player : $$anonymous$$onoBehaviour {

 private Rigidbody rb;

 public float speed = 5;

 // Use this for initialization
 void Start () {
     rb = GetComponent<Rigidbody>();
 }

 // Update is called once per frame
 void FixedUpdate () {
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W)) {
         rb.AddForce(transform.forward * speed);
     }
 }

}

avatar image
0

Answer by Carson365 · Dec 16, 2017 at 03:27 PM

Thanks, just what I needed. I am wanting it to be less jumpy so it doesn't just go right to the new position, but instead slide to it. I am thinking of adding an animation that goes with it, but please tell me if there is a better way. Thanks!

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 abhaychandna · Dec 17, 2017 at 04:57 AM

If you want it to stop just do this

  private Rigidbody rb;
 
  public float speed = 5;
 
  // Use this for initialization
  void Start () {
      rb = GetComponent<Rigidbody>();
  }
 
  // Update is called once per frame
  void FixedUpdate () {

      if (Input.GetKeyDown(KeyCode.W)) 
          rb.AddForce(transform.forward * speed);

      if (Input.GetKeyDown(KeyCode.S)) 
          rb.AddForce(transform.forward * -speed);
      
  }

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

113 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 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 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 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 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 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

Update character's position after animation 1 Answer

Resize gameObject when it has extended a restricted area 0 Answers

Get enemy position in script 1 Answer

Is it possible to position a GameObject as relative to a GUI? 2 Answers

How to tell if two blocks are right next to each other?(2D) 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