- Home /
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
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();
}
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.
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
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
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
Follow this Question
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