- Home /
Best way to move a Rigidbody2D from point A to point B (Mouse Position)
We have the following scenario:
We would like to know what would be the best way to move a 'prefab(bullet)' with rigidbody from point A to point B. Since point B of the equation will always be the mouse position on the screen.
We have already tested use without a rigidbody (Transform.Translate) but we have problems with FPS depending on the frame rate that the user's machine can have.
Also already we tried some solutions found in the forum, but they did not help us much. Someone would have a simple solution to this case?
We just need the prefab trace a route from Point A to Point B using a given force (speed) and interpolating the movement.
Class attached to 'Bullet prefab':
public class MovePrefab : MonoBehaviour
{
public Rigidbody2D rb2D;
void Start()
{
rb2D = GetComponent<Rigidbody2D>();
var mousePositionPointB = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
rb2D.velocity = mousePositionPointB ;
}
}
Class attached to 'Turret':
public class Turret : MonoBehaviour {
public Transform pointA;
public GameObject bulletPrefab;
void Update () {
if (Input.GetButtonDown("Jump"))
{
var pos = new Vector3(pointA.position.x, pointA.position.y);
Instantiate(bulletPrefab, pos, pointA.rotation);
}
}
}
This code does not work as it should, it can not draw a real route between points A and B.
Thanks in advance.
In your $$anonymous$$ovePrefab.Start(), try:
rb2D = GetComponent();
Vector3 mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Vector3 relativePos = mousePos - transform.position;
rb2D.velocity = relativePos.normalized * 1f; // 1f == desired speed
Thank you so much man! This really seems to work ... we still need some more tests! I take a doubt, that necessarily serves the function "normalize" on relativePos.normalized? Could you explain?
Please, read about Time.deltaTime. It fixes FPS dependency and allows to use, for example, slow motion. :)
Vector.normalized is a vector divided by it's length. So it's always 1 unit length. $$anonymous$$ultiplying it by value, when it's the velocity vector, makes it move faster (bigger velocity vector is applied to it).
In the end, I'd recommend: Transform.Translate and Time.deltaTime
Answer by IgorUnity3D · Sep 18, 2016 at 01:40 PM
It looks like there is reply nest limit. Sorry for not perfect code, it's really weird to write inside that editor in reply. Also, transform translate should fix the FPS problem if You multiply it by Time.deltatime. Like in:
gameObject.transform.Translate(translationVector*speed*Time.deltaTime);
I can try this and pass feedback to you, but if I'm not mistaken already use Time.deltatime
:\ (We will try!)
See: transform.Translate(Vector3.right * Time.deltaTime * moveSpeed);
About this:
void Update () {
if (Input.GetButtonDown("Jump"))
{
var pos = new Vector3(firePoint.position.x, firePoint.position.y);
var tmpObject = Instantiate(bullet, pos, bullet.transform.rotation) as Transform;
tmpObject.LookAt(point);
}
}
I first store my created object in some kind of variable, then get it's component to use with LookAt.
In this case, i can set the "point" to my mouse position, right?
Thanks for the help so far!
Answer by Hanoble · Sep 17, 2016 at 04:27 AM
Something basic like this should work.
public class Move : MonoBehaviour
{
public float moveSpeed;
public Transform target = null;
void Update()
{
if(target == null)
{
transform.localPosition += transform.forward * Time.deltaTime * moveSpeed;
}
else
{
transform.position = Vector3.MoveTowards(transform.position, targetPosition, moveSpeed * Time.deltaTime);
}
}
}
If you are instantiating an object, then you need to instantiate it at your "shooting point" or wherever the bullet begins. When you instantiate the object you should set the target by getting this move component, or if it is already in your scene simply drag the target via the inspector. On instantiation you can also pass a starting rotation via a quaternion, or simply do look at after instantiation and let the move script Update() take it from there.
transform.LookAt(target);
Right, but how to calculate object rotation? LookAt
dont works
Answer by Sinnii · Sep 17, 2016 at 05:52 PM
public class MovePrefab : MonoBehaviour
{
static float speedPerSec = 1f;
void Update()
{
transform.position = transform.position + transform.right * speedPerSec * Time.deltaTime;
}
}
public class Turret : MonoBehaviour {
public Transform pointA;
public GameObject bulletPrefab;
void Update () {
if (Input.GetButtonDown("Jump"))
{
Instantiate(bulletPrefab, pointA.position, pointA.rotation);
}
}
}