- Home /
odd behavior with click to move and ridgedbody 2d.
this is in 2D when I use this script it works fine if it is not dynamic. I want to use physics and gravity. I add a ridgidbody and box collider to the player and freeze the Z axis so it wont rotate and box colliders to the ground. it still moves but now it does funny things. sometimes when it stops it starts to bounce, most of the time the moving is just weird and I don't know how to fix it. I have tried many scripts from google as well as watch videos from YouTube but none of them really go into 2D movement with dynamic ridgidbody. Any help would be nice.
public float speed = 1.5f;
private Vector3 target;
void Start () {
target = transform.position;
}
void Update () {
if (Input.GetMouseButtonDown(0)) {
target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
target.z = transform.position.z;
}
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
Answer by toficofi · Mar 22, 2021 at 09:24 AM
When you are dealing with a dynamic Rigidbody, you will want to set the position on the Rigidbody instead of the transform so it can handle its physics stuff.
public float speed = 1.5f;
private Vector3 target;
private Rigidbody2D rb;
void Start () {
target = transform.position;
rb = GetComponent<Rigidbody2D>();
}
void Update () {
if (Input.GetMouseButtonDown(0)) {
target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
target.z = transform.position.z;
}
rb.position = Vector3.MoveTowards(rb.position, target, speed * Time.deltaTime);
}
Answer by xintaris · Mar 23, 2021 at 10:15 AM
Thanks for the answer but it still did not work. On the bright side I fixed with this code. Now I have one problem. The stop condition never seems to get called so he keeps moving. Is there a better way to do this?
public float speed = 3f;
private Vector3 target;
private Rigidbody2D rb;
private bool isMoving = false;
private float dir;
private Vector3 fingerPos;
void Start()
{
target = transform.position;
fingerPos = transform.position;
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
fingerPos = Input.mousePosition;
fingerPos.z = 10;
fingerPos = Camera.main.ScreenToWorldPoint(fingerPos);
}
checkPos(fingerPos);
}
private void FixedUpdate()
{
if (isMoving)
{
Move(dir);
}
}
private void checkPos(Vector3 targetPos)
{
if (transform.position.x < targetPos.x)
{
Debug.Log("less then moving right");
dir = 1;
isMoving = true;
}
if (transform.position.x > targetPos.x)
{
Debug.Log("greater then moving left");
dir = -1;
isMoving = true;
}
if (Vector3.Distance(transform.position, target) < 0.0001f)
{
Debug.Log("stopping");
isMoving = false;
fingerPos = transform.position;
}
}
private void Move(float dir)
{
//sets value of X using dir and speed
float xVal = dir * speed * 100 * Time.deltaTime;
//creates a vector2 for the velocity
Vector2 targetVelocity = new Vector2(xVal, rb.velocity.y);
//sets players velocity
rb.velocity = targetVelocity;
}
Your answer
Follow this Question
Related Questions
2D dynamic collider for dynamic shape 0 Answers
Can I arbitrarily move a dynamic Rigidbody 2D? 1 Answer
2D Dynamic Reflections ReadPixel Issue 0 Answers
Dynamic Health Bar in unity 4.6 0 Answers
How do you make fallback fonts work? 1 Answer