I'm trying to create an object I can pick up and move in a 2D platformer.
I am trying to create a pickup that I can move and place somewhere else on the map on my 2D platformer.
public float ThrowForce;
public Transform HoldPoint;
public LayerMask NotGrabbed;
public float DistanceToGrabFrom = 1f;
private bool _grabbed = false;
public RaycastHit2D Hit;
void Update () {
if (Input.GetKeyDown(KeyCode.E))
{
if (!_grabbed)
{
Physics2D.queriesStartInColliders = false;
Hit = Physics2D.Raycast(transform.position, Vector2.right * transform.localScale.x);
if (Hit.collider != null && Hit.collider.tag == "Grabbable")
{
_grabbed = true;
}
}
else //if (!Physics2D.OverlapPoint(HoldPoint.position))
{
_grabbed = false;
if (Hit.collider.gameObject.GetComponent<Rigidbody2D>() != null)
{
Hit.collider.gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(transform.localScale.x, DistanceToGrabFrom) * ThrowForce;
}
}
}
if (_grabbed)
{
}
This is my code, And it grabs the pickup, what it doesn't do is manipulate it, the pickup stays at the position where I grabbed it, then I can throw it, but from its starting position, regardless of where I am on the map. Don't really understand how I manipulate it. All I want to do is to be able to move it from point A to point B
Your answer
Follow this Question
Related Questions
What is the best way to script a trigger that moves a object from point a to point b in C# (unity 5) 0 Answers
Is there a way to make an Object pass over a Text? 0 Answers
Death from collision 1 Answer
My player won't let go of the object 0 Answers
What is the best way to script a trigger that moves a object from point a to point b in C# (unity 5) 1 Answer