# Ball scripting

**URL:** <https://discussions.unity.com/t/ball-scripting/401198>\
**Category:** Physics\
**Tags:** Scripting\
**Created:** [November 8, 2009, 8:10pm UTC](https://discussions.unity.com/t/ball-scripting/401198 "2009-11-08T20:10:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon\_5443197](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@anon\_5443197](https://discussions.unity.com/u/anon_5443197)\
**Post date:** [November 8, 2009, 8:10pm UTC](https://discussions.unity.com/t/ball-scripting/401198/1 "2009-11-08T20:10:08Z")

</div>

Hi, I’m developing a ball game and I ahve problems with ball moving script. I’ve tried rigidbody.AddForce, but then ball is constantly accelerating, adding torques, but it’s too slow and changing velocity, but it’s still imperfect.  
Here’s the code:

```auto
var globalPoint : Transform;
var moveForce = 2.0;
var jumpForce = Vector3(0,5,0);

function OnCollisionStay(collision : Collision)
{
	if(collision.gameObject.tag!="NonJump"){
		if(Input.GetAxis("Horizontal")){
			rigidbody.velocity = globalPoint.right*Input.GetAxis("Horizontal")*moveForce;
		}
		if(Input.GetAxis("Vertical")){
			rigidbody.velocity = globalPoint.forward*Input.GetAxis("Vertical")*moveForce;
		}
		if(Input.GetButtonDown("Jump")||Input.GetButtonDown("Fire1")) {
			Jump(jumpForce);
		}
	}	
	
}
function Jump(force_of_jump)
{
	rigidbody.velocity += force_of_jump;
}

```

(Global Point is the point the camera is looking at useful for global axis declaration).

Have Yiu got any idea why is the ball moving so strangely?

---

<div class="post-metadata">

**Author:** ![raiden](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/raiden/32/492708_2.png) [@raiden](https://discussions.unity.com/u/raiden)\
**Post date:** [November 9, 2009, 5:04am UTC](https://discussions.unity.com/t/ball-scripting/401198/2 "2009-11-09T05:04:07Z")

</div>

I haven’t tested anything, but you might take a look at rigidbody.AddRelativeForce() and use a ForceMode of VelocityChange.

It says something about it doesn’t change the velocity based on mass, so if it’s time based, then I’m thinking it’s a constant speed with no acceleration.

-Raiden

---

<div class="post-metadata">

**Author:** ![anon\_5443197](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@anon\_5443197](https://discussions.unity.com/u/anon_5443197)\
**Post date:** [November 9, 2009, 7:29pm UTC](https://discussions.unity.com/t/ball-scripting/401198/3 "2009-11-09T19:29:11Z")

</div>

Actually, it does not work. The problem is, that when force is acting on a bodfy, it accelerates constantly - it’s obvious. I’ve tried adding torque, but it’s far too slow. Is there a way to write such a controller in which ball will move precisely when keys are down and starting behaving physically when they are not?

---

<div class="post-metadata">

**Author:** ![PS\_Neville](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/ps_neville/32/154495_2.png) [@PS\_Neville](https://discussions.unity.com/u/PS_Neville)\
**Post date:** [November 10, 2009, 1:43am UTC](https://discussions.unity.com/t/ball-scripting/401198/4 "2009-11-10T01:43:16Z")

</div>

> [@""](#):
>
> Is there a way to write such a controller in which ball will move precisely when keys are down and starting behaving physically when they are not?

Did you try turning on/off the rigidbody’s isKinematic property depending on whether you have the keyboard input?

---

<div class="post-metadata">

**Author:** ![anon\_68312675](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@anon\_68312675](https://discussions.unity.com/u/anon_68312675)\
**Post date:** [November 10, 2009, 2:18am UTC](https://discussions.unity.com/t/ball-scripting/401198/5 "2009-11-10T02:18:17Z")

</div>

If you want absolute control over the velocity, you need to compensate the current velocity of the body.

```auto
rigidbody.AddForce(-rigidbody.velocity + desiredVelocityVector, ForceMode.VelocityChange);

```

---

<div class="post-metadata">

**Author:** ![andeeeee](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@andeeeee](https://discussions.unity.com/u/andeeeee)\
**Post date:** [November 10, 2009, 4:05pm UTC](https://discussions.unity.com/t/ball-scripting/401198/6 "2009-11-10T16:05:17Z")

</div>

Perhaps you can describe exactly how the ball control works. Does the game involve hitting/kicking the ball? Once it is moving, is it out of the player’s control or is there some kind of “aftertouch” that can be applied to it?
