Problems with AddForce2D and Linerenderer

RDT_20230726_153256

Hello fellow Gamedevs,

i started my Journey 2019 with exploring Unreal and Unity. Life got busy especially with Covid arround in 2020 and i stopped developing. Now i got more free time again, and got back to Game developing.

I’m trying to build a simple 2D Slingshot Arena Game as a first project, for learning purposes.

Watched a few tutorials and code examples, and then just started creating.

I am facing the following problems:

  1. It doesn’t matter how far i drag from the game object, the applied AddForce2D seems to have always the same power. Even if i just click it, same power.

  2. The game object, does not really stop itself so i have written a script to change the angularvelocity to 0, if it is under 1.5f velocity.magnitude. As you can see in the video, this happens only near walls after colliding and never somewhere in the field. It has rigidbody and circle collider, both with the same physics material. Gravity is 0, and body-type dynamic.

  3. And lastly my Line Render gets only shown for a second after clicking as you can see shortly in the video

Physic Settings:
Bounce-Treshold: 0.05f;
Bounciness: 0.3f;
Friction: 0.15f;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Slingshot_mechanic : MonoBehaviour
{
    // Gameobject components needed
    [HideInInspector]
    public Rigidbody2D rB; 
    public GameObject arrowAssembly; // Arrowhead showing the shoot direction
    Camera _cam;
    [SerializeField] AnimationCurve _animCurve; // animation curve for the line renderer
    LineRenderer _lineR;
 
    // Start and End Positions for Line Renderer
    Vector3 _startP;
    Vector3 _endP;
 
    public float shootForce = 1.5f; // Power of the shot
    float _forceScale;
    float _scaleAmmount;
    public float maxScale = 7f;
    public float scaleFactor = 5f;
 
    [HideInInspector]
    public bool isShot;
 
 
    void Start()
    {
        rB = GetComponent<Rigidbody2D>();
        _cam = Camera.main;
 
        Physics.bounceThreshold = 0.05f;
 
        arrowAssembly.SetActive(false);
        isShot = false;
    }
 
    private void OnMouseDown()
    {
        if (!isShot)
        {
            rB.isKinematic = true;
            arrowAssembly.transform.position = transform.position;
            arrowAssembly.SetActive(true);
 
            if (_lineR == null)
            {
                _lineR = gameObject.AddComponent<LineRenderer>();
            }
            _lineR.enabled = true;
            _lineR.positionCount = 2;
            _startP = _cam.ScreenToWorldPoint(Input.mousePosition);
            _lineR.SetPosition(0, _startP);
            _lineR.useWorldSpace = true;
            _lineR.widthCurve = _animCurve;
            _lineR.numCapVertices = 10;
        }
    }
 
    private void OnMouseDrag()
    {
 
        // Checking for current mouse position and setting the line Renderer
        _endP = _cam.ScreenToWorldPoint(Input.mousePosition);
        _lineR.SetPosition(1, _endP);
 
        // Getting the Distance and correct angle
        Vector3 _deltaDrag = _endP - transform.position;
        float _angle = Mathf.Atan2(_deltaDrag.y, _deltaDrag.x) * Mathf.Rad2Deg;
 
        // Setting the arrows direction
        arrowAssembly.transform.rotation = Quaternion.Euler(new Vector3(0, 0, _angle));
        transform.rotation = Quaternion.Euler(new Vector3(0, 0, _angle));
 
        // Getting the distance (Scale-Power for the shot
        _scaleAmmount = _deltaDrag.magnitude * scaleFactor;
        
        // Checking for maximum scale power
        _forceScale = _scaleAmmount;
        if (_scaleAmmount > maxScale)
        {
            _forceScale = maxScale;
        }
    }
 
    private void OnMouseUp()
    {
        arrowAssembly.SetActive(false);
        isShot = true;
        rB.isKinematic = false;
        _lineR.enabled = false;
 
        // Applying the force
        rB.AddForce(force: _forceScale * shootForce * -transform.right, mode: ForceMode2D.Impulse);
    }
 
    private void FixedUpdate()
    {
        // If the Velocity is below a certain point, stop the object
        if (isShot)
        {
            if (rB.velocity.magnitude < 1.5f)
            {
                StopPuk();
            }
        }
    }
 
    public void StopPuk()
    {
        isShot = false;
        rB.isKinematic = true;
        rB.velocity = Vector2.zero;
        rB.angularVelocity = 0f;
 
    }
}

The issue is that Input.mousePosition has a z value of -10 when converted to world point. Therefore, your _deltaDrag variable will have a z=-10, which will cause _deltaDrag.magnitude to be at least 10. The easiest fix would be to cast _deltaDrag to Vector2 to get rid of z value:

_scaleAmmount = ((Vector2)_deltaDrag).magnitude * scaleFactor;