Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 lolstrike · Feb 21, 2019 at 01:10 AM · vector3movement scriptmechanics

Roll a ball to fixed distance

Hi, I'm actually learning Unity and try to create simple game mechanics. I would like to roll a ball to a fixed distance, for example my ball is on a cube1 and have to roll on cube2 and stop. The following video illustrates what I want to do (just when the ball is moving forward) : https://www.youtube.com/watch?v=V0PmmhittAs


This is what I did, but the ball doesn't reach the target position and stop before.

public class Movement : MonoBehaviour { public float speed; private Rigidbody rb;

     private void Start()
     {
         rb = GetComponent<Rigidbody>(); 
     }
 
 
     private void Update()
     {
 
         
         if (Input.GetKeyDown(KeyCode.UpArrow))
         {
             Vector3 pos = rb.transform.position;
             Debug.Log(pos.ToString());
             pos.x += 1f;
             Debug.Log(pos.ToString());
             transform.position = Vector3.MoveTowards(rb.transform.position, pos, speed * Time.deltaTime);
         }
     }
 }

Thank you for your help

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
0
Best Answer

Answer by lolstrike · Feb 27, 2019 at 02:30 PM

I fixed my problem

 float distanceTravelled;
 Vector3 lastPosition;
 float speed = 2.0f;
 bool isMoving;

 private void launcheMovement()
 {
     if (Input.GetKey(KeyCode.UpArrow))
     {
         isMoving = true;
     }
 }

 private void Movement()
 {

     if (isMoving)
     {
         lastPosition = transform.position;
         transform.position += Vector3.right * speed * Time.deltaTime;
         distanceTravelled += Vector3.Distance(transform.position, lastPosition);
         Debug.Log("Distance travelled : " + distanceTravelled);
         if (distanceTravelled >= 1.0f)
         {
             isMoving = false;
             distanceTravelled = 0;
         }
     }
 }

 private void Update()
 {
     launcheMovement();
     Movement();
     
 }

 
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 jstopyraIGG · Feb 21, 2019 at 02:21 AM

You're telling the script to keep moving to the right. In the code you wrote, rb.transform.position is equal to transform.position, so every frame youre taking your own position, and trying to move to that exact same position, but 1 unit to the right.

Do you want to press arrow just once and have it move all the way, or do you want the user to hold it until the ball gets there? You need to describe the logic you want to happen more before we can help you.

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 lolstrike · Feb 21, 2019 at 02:34 AM 0
Share

Thank you for your comment, I want the user to press arrow just once. When he pressed, the ball should move on the other cube, I just placed 3 cubes on the x axis at the moment, and if the ball is on the cube 2 and the user presses again, the ball should move on the cube 3.

The following video illustrates what I want to code (just when the ball is moving forward, not the other mechanics) : https://www.youtube.com/watch?v=V0PmmhittAs

avatar image
0

Answer by Cornelis-de-Jager · Feb 21, 2019 at 03:39 AM

You are kinda mixing rigidbody movement and transform movement. If you were to just use RigidBody movement it would look like this:

 Transform target;
 RigidBody rb;
 
 void Start () {
     rb = GetComponent<RigidBody>();
 }
 
 void LateUpdate() {
     if (Input.GetKeyDown(KeyCode.UpArrow))
         HandleMovement ();
 }
 
 void HandleMovement () {
     Vector3 movePosition = transform.position;
      
     movePosition.x = Mathf.MoveTowards(transform.position.x, target.position.x, speed * Time.deltaTime);
      
     rb.MovePosition(movePosition);
 }

From here you only need to assign the target to

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 lolstrike · Feb 21, 2019 at 04:57 AM 0
Share

Thank you for your comment. I tried your solution but I have a problem, the ball doesn't travel to the target in one time, I have to press several times arrow key to reach the target.position.x

The transform.position.x is incremented about 0.03 at each press while it should incremented by 1.

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

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

Multiple Cars not working 1 Answer

Mouse click movement script issue. 2 Answers

Smooth touch for map 0 Answers

2D Vector Movement,Vectorel Movement with Buttons 2 Answers

Temporary GameObject Movement 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