Question by
Goubermouche · Dec 31, 2020 at 10:53 PM ·
rigidbody2dphysics2drocketsnegate
negating a force 2D
Hi! I'm working on a rocket launcher script, and I'm having this problem: I fire the rocket, there is an initial "warmup" period where the rocket flies up, and after that I want it to go to its target (the mouse pos), however, the force from the instantiation period offsets the rocket, and it becomes inaccurate - how can I fix this, while still using rigidbodies?
thx for any help!
ps: ik the code is probably a mess
{
StartCoroutine(timer());
rigidbody = GetComponent<Rigidbody2D>();
Destroy(gameObject, 5);
var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mouseDir = mousePos - gameObject.transform.position;
mouseDir.z = 0.0f;
mouseDir = mouseDir.normalized;
trailRenderer.enabled = false;
transform.parent = parent.transform;
}
void Update()
{
if (lauch)
{
rigidbody.AddForce(mouseDir * MoveSpeed);
Vector2 v = rigidbody.velocity;
float angle = Mathf.Atan2(v.y, v.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.AngleAxis(angle, Vector3.forward), Time.deltaTime * lerpTime);
trailRenderer.enabled = true;
transform.parent = null;
}
else
{
rigidbody.AddForce(transform.up * MoveSpeed2);
}
}
Comment
Your answer
Follow this Question
Related Questions
How create a Tunnel for gameobject to follow the tunnel path. 0 Answers
Accessing Rigidbody2D inspector info in code. 1 Answer
Limiting backwards speed 0 Answers
Collision sticking? 1 Answer