Jump to content
  • Advertisement
mtjscott

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()
    {
        rb = gameObject.AddComponent<Rigidbody2D>();
        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);
    }

}

 

9aaa8a4c711db7d5beea77f6c22e599c.png

b9485fa117d94c56a321f91213fca41c.png

Share this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!