- Home /
Mouse Movement with physics
(2D) How can I make my player be controlled by the mouse position and still being able to stop if it hits a collider?
public class Movement : MonoBehaviour
{
public float distance = 1f;
public bool useInitialCameraDistance = false;
private float actualDistance;
public Rigidbody2D rb;
public float force = 100f;
void Awake ()
{
rb = gameObject.GetComponent<Rigidbody2D>();
}
// Start is called before the first frame update
void Start()
{
if (useInitialCameraDistance)
{
Vector3 toObjectVector = transform.position = Camera.main.transform.position;
Vector3 linearDistanceVector = Vector3.Project(toObjectVector, Camera.main.transform.position);
actualDistance = (transform.position = Camera.main.transform.position).magnitude;
}
else
{
actualDistance = distance;
}
}
// Update is called once per frame
void Update()
{
Vector3 mousePosition = Input.mousePosition;
mousePosition.z = distance;
transform.position = Camera.main.ScreenToWorldPoint(mousePosition);
Vector3 pos = transform.position;
pos.z = 0;
transform.position = pos;
}
}
Answer by JusSumGuy · May 19, 2019 at 12:59 AM
Don't use transform.position to change the objects position. Use Rigidbody.MovePosition to actually move the object not teleport the object. This is how I would do it.
Vector2 mousePos;
Rigidbody2D rb;
float speed;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
mousePos = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);
rb.$$anonymous$$ovePosition(mousePos);
//or use the one below if you don't have a rigidbody
// transform.position = Vector2.$$anonymous$$oveTowards(
transform.position, mousePos,speed * Time.deltatime;);
}
Neither of the ways you showed worked using System.Collections; using System.Collections.Generic; using UnityEngine;
public class $$anonymous$$ovement : $$anonymous$$onoBehaviour
{
public float distance = 1f;
public bool useInitialCameraDistance = false;
private float actualDistance;
public Rigidbody2D rb;
public float force = 100f;
Vector2 mousePos;
public float speed = 100;
void Awake()
{
rb = gameObject.GetComponent<Rigidbody2D>();
}
// Start is called before the first frame update
void Start()
{
if (useInitialCameraDistance)
{
Vector3 toObjectVector = transform.position = Camera.main.transform.position;
Vector3 linearDistanceVector = Vector3.Project(toObjectVector, Camera.main.transform.position);
actualDistance = (transform.position = Camera.main.transform.position).magnitude;
}
else
{
actualDistance = distance;
}
}
// Update is called once per frame
void Update()
{
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 pos = transform.position;
//rb.$$anonymous$$ovePosition(mousePos);
pos.z = 0;
transform.position = pos;
//or use the one below if you don't have a rigidbody
transform.position = Vector2.$$anonymous$$oveTowards(
transform.position, mousePos,speed* Time.deltaTime);
}
}
The pos stuff is just to help with camera problems The player moves but doesn't stop at colliders
I would recommend to use rigidbody.$$anonymous$$ovePosition(newPosition);
This will definitly work if you make sure to not set the position with another method again and you have your rigidbody and colliders set up correctly.
If you want to quickly test if collisions with your rigidbody and colliders work:
Activate gravity on the rigidbody
Let the gameobject fall on a wall / ground
$$anonymous$$ay you show me how to do this? would newPosition be the mousePosition?
Your answer
Follow this Question
Related Questions
Make Ray hit own collider 1 Answer
Trouble with Physics.IgnoreCollision 0 Answers
Physics.IgnoreCollision not working 0 Answers