Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. On February 28th the Feedback website will shut down and be redirected to the Unity forums. See the full post for more information.
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Unity 2018.3 is now released.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Add force to object at specific angle issue

Discussion in 'Scripting' started by allardkeij, Jul 14, 2018.

  1. allardkeij


    Jun 2, 2018
    Hey guys i recently started with Unity (and C#) and i am trying to pull of a specific drag and shoot mechanism.

    As i am figuring it out i am simply to unknown with the whole world axis and i'm not very good with math.

    Therefore i can't figure this one out on my own.

    The idea is simple. My object is located in a 3D world and i've put in a Joystick. When i drag and release the joystick my "player" object should be shot at the opposite angle. I am trying to do that with a AddForce function.

    I do get some results but they are far from what it should be.

    To calculate the angle i am using the start point of the joystick ( dead center ) and the position when it's released.

    What i think is happening is that my object actually flies in a 3D world while i am trying to let it shoot "2D" style a.k.a. 360 degrees on the x and y axis not on the Z. I simply locked the Z axis on my object.

    Long story short, applying a force to my object with the joystick works fine but the angles don't really work out like they should.

    This is my code so far:
    Code (CSharp):
    1. public void OnPointerUp(PointerEventData data)
    2.     {
    3.       float anglex = transform.position.x;
    4.       float angley = transform.position.y;
    6.       float throwAngle = Mathf.Atan2(anglex - m_StartPos.x, angley - m_StartPos.y) * Mathf.Rad2Deg;
    8.         Vector3 dir = Quaternion.AngleAxis(throwAngle, Vector3.forward) * Vector3.right;
    10.         dagger.GetComponent<Rigidbody>().AddForce(dir * thrust);
    12.         Debug.Log(throwAngle);
    14.     }
  2. Doug_B


    Jun 4, 2017
    I have just tried doing this using the mouse. The Z axis takes care of itself as the mouse cannot move along that plane.

    Here is the code. If I understand your requirement correctly, I think it is along the lines you are wanting. Maybe it can help give you some ideas? :-
    Code (CSharp):
    1. public class Test : MonoBehaviour
    2. {
    3.     void Start()
    4.     {
    5.         m_body = GetComponent<Rigidbody>();
    6.     }
    8.     void OnMouseDown()
    9.     {
    10.         if(!m_drag)
    11.         {
    12.             m_drag = true;
    13.             m_began = Input.mousePosition;
    14.         }
    15.     }
    17.     void OnMouseUp()
    18.     {
    19.         if (m_drag)
    20.         {
    21.             var now = Input.mousePosition;
    22.             m_body.AddForce((m_began - now).normalized * m_thrust);
    23.             m_drag = false;
    24.         }
    25.     }
    27.     const float m_thrust = 2f;
    28.     Rigidbody m_body;
    29.     Vector3 m_began;
    30.     bool m_drag;
    31. }