Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to add force in specific direction?

Discussion in 'Scripting' started by Sushant_Dhiman, Oct 1, 2020.

  1. Sushant_Dhiman

    Sushant_Dhiman

    Joined:
    Oct 1, 2020
    Posts:
    22
    Hi i have a ball i want to add an arrow to its surface in 2d which continuously rotates (see image for more clarification) how to make the arrow rotate continuously ?

    And how can i make to ball to get forced in the direction in which arrow is pointing.

    Like if arrow is in North-West and user click on screen then force get added to ball in that direction.

    Image Link = https://imgur.com/CqDfjQY
     
    Last edited: Oct 1, 2020
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    do you really need physics?
    (btw the image doesn't work)
     
  3. Sushant_Dhiman

    Sushant_Dhiman

    Joined:
    Oct 1, 2020
    Posts:
    22
    oh let me add
    new image
     
  4. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,495
    Add this Component to your arrow to make it continuously rotate.. or do you mean towards something, like the mouse position?
    Code (CSharp):
    1. using UnityEngine;
    2. public class My2DArrowRotater : MonoBehaviour
    3. {
    4.   public float speed = 50;
    5.   void Update()
    6.   {
    7.     transform.Rotate(0, 0, Time.deltaTime * speed);
    8.   }
    9. }
    Edit: removed a line
     
  5. Sushant_Dhiman

    Sushant_Dhiman

    Joined:
    Oct 1, 2020
    Posts:
    22
    caan you plea
    se see the question again because i had updated it
     
  6. Sushant_Dhiman

    Sushant_Dhiman

    Joined:
    Oct 1, 2020
    Posts:
    22
    imag
    e link given
     
  7. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,495
    And you want it to rotate towards something else, or around and around?
     
  8. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    if you don't need physics, it is enough to obtain a direction of this arrow.
    let's say you had two points in space, A, where your ball is, and B where your arrow should point at.
    obtain a direction from A to B by doing this (I'm oversimplifying a bit to get the point across)

    Vector2 difference = target - ball.position;


    now that you have a difference between two points as a vector, you need to make sure it's magnitude is 1, so that it can be considered a 'unit' vector.

    Vector2 direction = difference.normalized;


    now it's a direction in space.

    you then take the update method and update the ball position by this direction, multiplied with deltaTime
    Code (csharp):
    1. void Update() {
    2.   ball.position += speed * direction * Time.deltaTime;
    3. }
    that's all.

    the whole thing
    Code (csharp):
    1. float speed = 5f; // adjust until the speed is decent enough, I'm doing this from my head
    2. Vector2 target; // some world coordinate, you can use your mouse if you wish
    3.  
    4. void Update() {
    5.   var direction = (target - ball.position).normalized;
    6.   ball.position += speed * direction * Time.deltaTime;
    7. }
     
  9. Sushant_Dhiman

    Sushant_Dhiman

    Joined:
    Oct 1, 2020
    Posts:
    22
    i want to rotate arrows on the edges of the 2d circle see image.
     
  10. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    use Mathf.Atan2 for 2D rotation.

    by taking the difference between A and B, you get the individual components of x and y.
    pass that to Atan2 and you'll get the angle of rotation in radians. (observe that the arguments are y,x not x,y.) multiply that by Mathf.Rad2Deg to get degrees if you need them.
     
  11. Sushant_Dhiman

    Sushant_Dhiman

    Joined:
    Oct 1, 2020
    Posts:
    22
    can
    you please send me codes?
     
  12. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,495
    Put your arrow as a child of an empty GameObject, drag it away from the center in the direction it points, put the script I posted onto the empty one, and when it rotates, it's the pivot of your arrow.. which will then rotate at the distance you put it.
     
  13. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Code (csharp):
    1. Vector2 target; // some actual world coordinate
    2.  
    3. void Update() {
    4.   var difference = target - ball.position;
    5.   var angle = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
    6.   ball.rotation = Quaternion.Euler(0f, 0f, angle);
    7. }
    if you need physics, then your ball needs to a have rigidbody component, and go look for the method called AddForce
    no more code, this is too trivial. check out youtube for more.