# C# Ball Roll Mechanic Not Working

## Recommended Posts

Hey peeps. It's my first post here. I'm a newbie to c# and unity so bare with me. I'm learning to code by developing a mobile game based around the french game boules. The player is meant to click on the circle on the screen, drag it back then release the ball. The ball with then be shot off in the direction of the start point with the proportionate force according to how far it was dragged. Similar to the drag and release mechanic when playing 8 ball pool on miniclip. So far I have the drag and release mechanic working but I can't seem to get the ball to fire after the ball is release. Any input would help. Thankyou.

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

public class Shoot : MonoBehaviour
{

[SerializeField] GameObject Disc;
[SerializeField] float multiplier;

Vector3 initPos;

private Rigidbody2D rb;

private void Start()
{
initPos = transform.position;
}

void OnMouseDrag()
{
transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10));
}

void OnMouseUp()
{
Disc.GetComponent<Rigidbody2D>().AddForce ((initPos - transform.position).normalized * multiplier);
}

}

##### Share on other sites

Is your script referencing the same object it is attached to?

I see an rb variable being set in start, but then you are getting the rigidbody for the Disk object when you apply the force.  If these should be the same object and the script is attached to it, then just use the rb variable you set in start, instead of the Disk object.  That might help anyhow.

Also, I can't emphasise enough that the different Force Modes have greatly varying effects in certain scenarios, look them up and experiment.

Another thing to check are the constraints on the rigidbody, click the arrow next to constraints and see if any of those boxes are checked that shouldn't be.  Those will prevent any physics from effecting the selected aspects.

##### Share on other sites

I seem to remember that I've opted to use a rigidbody3d even in my 2d game programming.  You just Mark the constraint(s) you need to keep it on the plane you want.  Unity was designed for 3d so some of the 2d counterparts feel and act like afterthoughts sometimes.

##### Share on other sites
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Shoot : MonoBehaviour
{

[SerializeField] GameObject Disc;
[SerializeField] float multiplier;

Vector3 initPos;

private Rigidbody2D rb;

private void Start()
{
rb = gameObject.GetComponent<Rigidbody2D>();
initPos = transform.position;
}

void OnMouseDrag()
{
transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10));
}

void OnMouseUp()
{
rb.AddForce ((initPos - transform.position).normalized * multiplier);
}

}

So I fixed the problem by changing the Rigidbody2D reference to rb. That didn't do anything but then reading back through the code when I was setting rb I was adding a component instead of getting the already existing one. Which fixed the addforce issue. So upon release the ball moves according to whatever force I have set in the visualisation field but it doesn't change according to how far I drag it from the start point which I think may be due to the transform.position logic which I will look into now. Thanks a lot. I've been trying to figure that out for a while

##### Share on other sites

Check out Force Modes as well, I think you may want to use Impulse here instead of whatever the default is.

##### Share on other sites

Thanks my dude, you've been great so far. Impulse worked better and after doing some research i've learned a lot about force modes. God bless.

## Create an account

Register a new account

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634107
• Total Posts
3015549
×