1
\$\begingroup\$

I making 2d game on unity5 .I want to give to an object starting velocity to the direction it is facing(looking).(I put it in the "start" method) how can i do it?

\$\endgroup\$
  • 1
    \$\begingroup\$ Can you show us an example of the character/sprite you're trying to move? Depending on what you're doing, you might have a different definition of "facing direction" - eg. a spaceship in a Tyrian style shooter usually faces up, while a platformer character usually faces right, but can sometimes turn around to go left... \$\endgroup\$ – DMGregory Jan 12 '16 at 17:37
  • 1
    \$\begingroup\$ Do you want it to always travel in that single direction, even if it rotates to point a new direction later (for any reason)? \$\endgroup\$ – Draco18s Jan 12 '16 at 18:06
  • \$\begingroup\$ i instantiated a 4 balls with random rotation I want that every ball will get starting velocity,to the directiin it facing.but if the ball will collide it will change the velocity direction according to the colliding(the velocity is-starting velocity-) \$\endgroup\$ – yaakov rubenchik Jan 12 '16 at 21:15
  • \$\begingroup\$ But what is the "facing direction" of a ball? Is it the local x, y, or z axis? \$\endgroup\$ – DMGregory Jan 12 '16 at 22:30
  • \$\begingroup\$ only the z axis,the other are 0 \$\endgroup\$ – yaakov rubenchik Jan 13 '16 at 5:26
1
\$\begingroup\$

Well, on DMGregory's pointing, I did realize that you want this work done in 2D. So, I did some work on this, here I'm pasting the code which should be attached to your object which you want to accelerate.

Here are some assumptions I made,

  • Your object is not child. (Otherwise it will be not a big deal though, you have to play with + 90 factor)
  • Your object should have attached Rigidbody2D.
  • Wrote this script with getting input from keyboard. Do change it according to your needs.

Code,

// Rotation Speed
public float _rotationSpeed = 1;

// Speed
public float _velocity = 1;

// Attached Rigidbody2D
Rigidbody2D _rb;

void Start ()
    {
        _rb = GetComponent<Rigidbody2D> ();
    }

void Update ()
    {
        // Getting input from keyboard (w,d / Left arrow,Right arrow)
        float horizontalInput = Input.GetAxis ("Horizontal");

        // Applying rotation according to inputs
        transform.localEulerAngles = new Vector3 (transform.localEulerAngles.x, transform.localEulerAngles.y, transform.localEulerAngles.z - (horizontalInput * _rotationSpeed));

        // Getting angle which will help to move object to forward
        // + 90 is the factor as 2d sprites have z rotation of -90 degrees while it look visually as angle with 0 degrees. PLay with it if your object is child of another.
        float theta = transform.localEulerAngles.z + 90;

        // Getting new X direction
        float newDirX = Mathf.Cos (theta * Mathf.Deg2Rad);

        // Getting new Y direction
        float newDirY = Mathf.Sin (theta * Mathf.Deg2Rad);

        // Applying velocity according to current angle
        _rb.velocity = new Vector2 (newDirX, newDirY) * _velocity;
    }

I wrote it in hurry, if you still find any problem then let me know

\$\endgroup\$
  • 1
    \$\begingroup\$ OP mentioned that they're making a 2D game, in which case they'll likely want Rigidbody2D instead of the 3D version. Interpreting the "facing direction" is a bit tricky in this case, since transform.forward points along the local z axis, which in 2D games is often into/out of the screen. For a side-scroller, transform.right might be correct, while for a spaceship in a vertical shooter, transform.up might be appropriate. \$\endgroup\$ – DMGregory Jan 12 '16 at 17:36
  • \$\begingroup\$ Sorry man, my bad. I was on mobile device, let me correct it \$\endgroup\$ – Hamza Hasan Jan 12 '16 at 17:55
  • \$\begingroup\$ @DMGregory can you endorse it? :) \$\endgroup\$ – Hamza Hasan Jan 12 '16 at 18:54
  • 2
    \$\begingroup\$ I'll pass. I usually try to avoid trigonometry-based solutions where simple vector algebra will suffice. \$\endgroup\$ – DMGregory Jan 12 '16 at 18:56
  • \$\begingroup\$ But they use to be more interesting :) \$\endgroup\$ – Hamza Hasan Jan 12 '16 at 18:58
0
\$\begingroup\$

We want to simulate wind resistance.

There is some restoring torque $\tau$ that will align your velocity $\vec{v}$ with your attitude $\vec{a}$ (where attitude is the direction you are pointing, and you means the projectile.

A torque pointing into the page will push youe body clockwise, and out of page will push your body counter-clockwise.

$\tau = k_{air} * \vec{a} \times \vec{v}$ will push the projectile's attitude to line up with its velocity. Where k is a number you make up that makes the projectile swing at an acceptable rate.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.