- Home /
Issue with direction and rigibody2D
Hello,
I have an issue when I'm trying to send a projectile from a position to an unkown position but driven by a direction. Imagine a bubble game, I want the same behaviour as a bubble you send by clicking on the screen.
So I have a projectile, wall which change direction of this projectile and some block objects where the projectile will collide with and stop moving. This projectile has a Rigidbody2D on and is non-kinematic, that's why I'm using velocity to launch it. The projectile go where I want it to go (toward click direction) but it stops at click position (x and y aixs). I want it to continue until it touches a block object. So the direction is alright (target = click), but I don't know how to tell the projectile to keep going.
There is a piece of code I write to get this result :
// way = 1 or -1 if the object bounce on a wall it inverse direction in x axis)
//Target = mouse position (direction I want the projectile to follow
target = new Vector2((way * _mousePosX), _mousePosY);
direction = (target - (Vector2)_itemToThrow.transform.position).normalized;
//I tested with moveposition and lookat same result as velocity
//_itemToThrow.GetComponent<Rigidbody2D>().MovePosition((Vector2)_itemToThrow.transform.position + direction* 10 * Time.deltaTime);
/*_itemToThrow.transform.LookAt(target);
_itemToThrow.GetComponent<Rigidbody2D>().velocity = _itemToThrow.transform.forward * 500;
_itemToThrow.transform.rotation = new Quaternion(0, 0, 0, 0);*/
// This is the actual code, work the same with transform direction or without
_itemToThrow.GetComponent<Rigidbody2D>().velocity = direction * 100 * Time.deltaTime; // transform.TransformDirection(direction* 50 * Time.deltaTime);
Well if you have any idea how I can send my projectile to a define direction but an "infinite" position at the end it will be appreciated.
Thank you for your help and time !
Answer by MayIAsk · Jan 30, 2017 at 07:40 AM
I don't know if anyone read this but I'm still wondering how to launch an object with no limit. Sorry I don't want to spam or flood.
Thank you
Your answer
Follow this Question
Related Questions
Set Object Rotation based on Collision (2D) 0 Answers
Move car at same speed 0 Answers
How can I get local 2d Velocity? 2 Answers
Rigidbody2D velocity in random direction with exceptions 1 Answer