Hi All, I have been trying to figure this out for sometime now and nothing I have tried has worked. What I’m trying to do is have the player be about to hit the left mouse button (Fire 1) key and pull a bouncing ball to where every the player is on the board. Any help would be great thanks. ![]()
hmmm… you want it to keep bouncing I assume? Kind of a pickle then. If you just want it to be pulled toward the character you can just do something like:
if(player.x > ball.x) {
ball.x += speed * Time.deltaTime;
}else if(player.x < ball.x) {
ball.x -= speed * Time.deltaTime;
}
For the x, y, z variables. If you wanted it to be bouncing, then you would just need to modify the y value independently while pulling it towards with the x z. Depending on how sophisticated you are doing this, you might also need to look into pathfinding.
Welcome to the forum, biggd321!
Can you describe the effect you want in a bit more detail? I’m not sure exactly what is going on in the game.
The player will be controlling a character and he will be moving around a large glass box. On the inside of the glass box will be a ball and it will be bouncing around the and collecting smaller boxes. So what I’m trying to do is press the left mouse button and have it activate a tractor beam / Vacuum to suck the ball to the players location and then be able to release the ball so it can collect the rest of the smaller boxes. The ball will never leave the inside on the large glass box. I hope that it is a little clear on what I’m trying to do. Thank You so much for the help.
If you’re doing this with physics, you can suck the ball to a position with code like the following:-
var ball: Rigidbody;
var sucking: boolean;
var targetPos: Vector3;
var suckForce: float;
var damping: float;
function FixedUpdate () {
if (sucking) {
var suckDir = targetPos - ball.position;
ball.AddForce(suckDir * suckForce - ball.velocity * damping);
}
}
This just calculates the direction from ball to target and applies a force proportional to the distance, like a spring. The reason for the damping is to stop the ball oscillating endlessly around the target point. Use a higher damping value to bring the ball to a halt smoothly and a lower value to make it vibrate slightly as it stops.
Thanks for the reply. I think it is going to lead me in the right direction. Now I just need to make the ball stop moving completely when the the player is holding down the left mouse button and then be able to release the ball back into the game area when the left mouse button is released. I think it might have something to do with my ball bounce code that is not letting it stop it. I’m not a coder so if it is way off from what would be the best logical script to make a ball bounce then all I can say is “I’m trying” :). Thanks a ton for all the help, it has moved my project that much further on getting done.
HERE IS THE BALL BOUNCE CODE THAT I’M USING:
var ballVelocity : Vector3 = Vector3.zero;
var ballSpeed : float;
function Start() {
ballVelocity.Normalize();
}
function FixedUpdate () {
transform.position += ballVelocity*ballSpeed*Time.deltaTime;
}
function OnCollisionEnter(collision : Collision) {
for (var contact : ContactPoint in collision.contacts) {
ballVelocity = Vector3.Reflect (ballVelocity,contact.normal);
}
}