- Home /
Question by
sebastianregelmann03 · Nov 24, 2020 at 08:58 PM ·
2d game2d-platformer2d sprites
Hey Guys. I am creating my first 2d Game right now an i run into a problem with sprites.
Hey Guys. I am creating my first 2d Game right now an i run into a problem with sprites. I am implementing a grappling hook and got everything run like i want. The only thing left is that i want a sprite from the player to the point on the wall. And the best would be when i could use a repetitiv sprite.
Thanks to everybody who trys to help
Sebastian
public class GrapplingHook : MonoBehaviour {
[Header("Positions and Player")]
public LayerMask whatIsGround;
public Transform Player;
public float StopGrapRange = 0.5f;
public float GrapplingRange;
public float GrapSpeed;
private Vector3 MousePosition;
private RaycastHit2D hit, hitOld;
public Ray ray, rayOld;
public Rigidbody2D rb;
private float step;
private float time = 0.0f;
private bool GrapToPoint = false;
void Update()
{
if(Input.GetMouseButtonDown(1) && time > 2f)
{
GetGrappPoint(); //Get GrappPoint
print("hit.distance: " + hit.distance);
rb.bodyType = RigidbodyType2D.Static; //Deactivate the Rigidbody "Physics"
time = 0f;
}
else if (Input.GetMouseButtonDown(0))
{
GrapToPoint = false;
rb.bodyType = RigidbodyType2D.Dynamic; //Reactivate the Rigidbody "Physics"
}
else if (hitOld.distance < StopGrapRange)
{
GrapToPoint = false;
rb.bodyType = RigidbodyType2D.Dynamic; //Reactivate the Rigidbody "Physics"
}
if(GrapToPoint)
{
float step = GrapSpeed * Time.deltaTime;
Player.transform.position = Vector2.MoveTowards(transform.position, new Vector2(hit.point.x, hit.point.y), GrapSpeed * Time.deltaTime); //Move Player to the point
}
CheckRay(); //Check if Player is still away from the Wall
time = time + Time.deltaTime;
}
private void GetGrappPoint()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition); //Get Mouseposition raltiv to the world
rayOld = ray;
hit = Physics2D.Raycast(ray.origin, ray.direction, GrapplingRange, whatIsGround); //get next wall(collider)
if(hit.collider != null)
{
Debug.DrawLine(Player.transform.position, new Vector3(hit.point.x, hit.point.y, 0.1f), Color.red, 100f);
GrapToPoint = true;
}
}
void CheckRay() //Check if Player is still away from the Wall
{
hitOld = Physics2D.Raycast(transform.position, rayOld.direction, GrapplingRange, whatIsGround);
print("RayCheckLenght" + hitOld.distance);
}
}
Comment
Your answer
Follow this Question
Related Questions
How to fix random lines that appear in my background. 1 Answer
Sprite turning around repeatedly against a wall. How to have "Wallslide" flip sprite? 0 Answers
Help Me! I want to make a pixel fairy! 1 Answer
How to create soil or sand?? 1 Answer
"Create New Palette" doesn't actually create a palette at all 1 Answer