- Home /
Controlling rigidbody.AddForce direction with vector2 angle?
Hi,
Im hoping someone can help me. I am trying to figure out how to use the angle generated from this code
if (touch.phase == TouchPhase.Began)
{
fp = touch.position;
lp = touch.position;
}
if (touch.phase == TouchPhase.Moved )
{
lp = touch.position;
swipeDistanceX = Mathf.Abs((lp.x-fp.x));
swipeDistanceY = Mathf.Abs((lp.y-fp.y));
}
if(touch.phase == TouchPhase.Ended)
{
angle = Mathf.Atan2((lp.x-fp.x),(lp.y-fp.y))*57.2957795f;
to set the direction of a rigidbody.Addforce in a vector3.
If I was using translate and rotate functions I would just do this to rotate the object in the direction I'm after.
transform.Rotate(0,angle,0);
From what I have read one should avoid the use of transform.rotate when dealing with a rigidbody and physics which is why I'm trying to use the angle to direct the addforce code. Sorry I'm fairly new to coding.
Any help would be appreciated Thanks Ryan
Answer by drudiverse · Oct 06, 2014 at 07:37 PM
rigidbody.AddTorque will rotate the object, you can add torque relative to the objects rotation. you can also use rigidbody velocity torque variable on a frame by frame basis for finer control, frame by frame change the velocity torque variable relative to the angle to point change it to a the angle over a number of frames, will avoid using transform rotate, and still keep the physics active.
Your answer
Follow this Question
Related Questions
Simple rigidbody.AddForce only applying once each time key is pressed 1 Answer
Is Add Force The Best Solution 1 Answer
Move object invertically to another 0 Answers
Have player fall down with platform? 0 Answers
Smooth dashing issues 0 Answers