***So i had a grappling hook working for pc and now I want to port it to android using touch input on the mobile however im having a problem where it wont work the same as the mouse as in when i would hold right click on pc it would cast the hook and drag the player towards the hitpoint. However on mobile it is draging him to the position as long as i touch along the rope and drag the player to its position (hard to explain) this is the code using mouse click and below is the code the current code***
void Update()
{
if (canGrapple == true)
{
if (joint.distance > 1f) //Pulls the player up
joint.distance -= step;
else
{
line.enabled = false;
joint.enabled = false;
}
if (Input.GetMouseButtonDown(1))
{
targetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); //sets the vector to where the mouse clicks
targetPos.z = 0;//2d game no need for z
hit = Physics2D.Raycast(transform.position, targetPos - transform.position, distance, mask); //target - the cast point , the distance, the mask it collided with
if (hit.collider != null && hit.collider.gameObject.GetComponent() != null) //if it hits a collider with a rigidboddy
{
joint.enabled = true;
joint.connectedBody = hit.collider.gameObject.GetComponent(); //connects the joint to the rigidbody of the object it collides with
joint.connectedAnchor = hit.point - new Vector2(hit.collider.transform.position.x, hit.collider.transform.position.y); // sets the postion of the anchor relative to the rigidbody to the hit point and creates a new vector as the player climbs up
joint.distance = Vector2.Distance(transform.position, hit.point); //the distance
line.enabled = true; //Displays the line
line.SetPosition(0, transform.position); //Where the line Starts from
line.SetPosition(1, hit.point);//Where the line ends(the Hit Point of the ray)
thePlayerController.isGrappling = true; //used to play animations
}
}
***Current Code***
void Update()
{
if (canGrapple == true)
{
if (joint.distance > 1f) //Pulls the player up
joint.distance -= step;
else
{
line.enabled = false;
joint.enabled = false;
}
if (Input.touchCount > 0)
{
targetPos = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position); //sets the vector to where the mouse clicks
targetPos.z = 0;//2d game no need for z
hit = Physics2D.Raycast(transform.position, targetPos - transform.position, distance, mask); //target - the cast point , the distance, the mask it collided with
if (hit.collider != null && hit.collider.gameObject.GetComponent() != null) //if it hits a collider with a rigidboddy
{
joint.enabled = true;
joint.connectedBody = hit.collider.gameObject.GetComponent(); //connects the joint to the rigidbody of the object it collides with
joint.connectedAnchor = hit.point - new Vector2(hit.collider.transform.position.x, hit.collider.transform.position.y); // sets the postion of the anchor relative to the rigidbody to the hit point and creates a new vector as the player climbs up
joint.distance = Vector2.Distance(transform.position, hit.point); //the distance
line.enabled = true; //Displays the line
line.SetPosition(0, transform.position); //Where the line Starts from
line.SetPosition(1, hit.point);//Where the line ends(the Hit Point of the ray)
thePlayerController.isGrappling = true; //used to play animations
}
}
if (Input.touchCount == 0)
{
line.SetPosition(0, castPos.transform.position);
}
if (Input.touchCount == 0)
{
thePlayerController.isGrappling = false;
joint.enabled = false;
}
}
}
↧