Mechanic that propels character in a certain direction
Hi all! I am creating a 2D platformer and I can't seem to wrap my head around how to code the mechanic that is in my head. Basically, The player has a pickaxe and when the player throws it and it connects with the ground, a wall, or the ceiling, I want the tip of the pickaxe to stay at the point of contact with the surface, the player should be propelled towards that spot, like a rubber band, and the pickaxe's handle should be pointing towards the player.
This is my code so far, I know it's a huge mess:
void Throw(){
//Throw axe if fire1 is pressed
if (Input.GetButtonDown ("Fire1")) {
GameObject axe = Instantiate (pickaxeObject, playerTrans);
pickaxerb = axe.GetComponent<Rigidbody2D> ();
PointToMouse (axe);
//Find mouse position
Vector3 mousePos = Input.mousePosition;
//change z axis of mouse position
mousePos.z = 10f;
//Convert to world space
mousePos = Camera.main.ScreenToWorldPoint (mousePos);
//Find player position
Vector3 playerPos = axe.transform.position;
//Calculate difference between target and player
Vector3 diff = mousePos - playerPos;
//Throw Axe
diff = diff.normalized; //Normalize diff
pickaxerb.AddForce (diff * thrust, ForceMode2D.Impulse);
Destroy (axe, range); //Destroy the axe after x seconds
}
if(Input.GetButtonUp("Fire1")){
Destroy(GameObject.FindGameObjectWithTag("axe"));
}
}
What this does: The player throws the pickaxe in the direction of the mouse, and it is destroyed after a time period, or when the user lets go of the mouse button. The pickaxe doesn't collide with anything, which is another problem.
The hardest part of all this? Its getting the pick to stick via tip and handle to face the player everytime without looking bad. The actual tether and pull should be easy. You need to break this down and not handle it as a single problem.
Answer by Navarrox · Feb 24, 2018 at 12:05 AM
Humm what you describe sounds like a grappling hook to me. Have you tried this tutorial: https://www.youtube.com/watch?v=rhNmjKedcjw
There are assets that seem to work like what you described as well. Does your pickaxe have a collider? You could add a collider to it, check for collision with OnCollisionEnter/OnTriggerEnter (what works best for you), get the info about the collision (check if the object your pickaxe collided is a valid target, get the point of collision, etc), attach the pickaxe to your target (maybe use joints?) and then you could calculate the force vector you need to add to your player so he flies toward the pickaxe (get target point, get player point, calculate vector multiply by a "speed" variable).
Hope this helps!
I never even though that my idea was just a grappling hook haha. I'll try out this tutorial and get back to you!
Yeah, i was trying to do something similar some time ago (a projectile that attachs to an object, if it's another player it gradually pulls the target closer, else if it's, let's say, a pillar the player itself is propelled towards the target) but ins$$anonymous$$d of simply searching for "grappling hook" i tried all different kinds of weird stuff like "magic tether" or "magic lasso" lol. In the end i scrapped that and decided to start with simpler stuff, like a fireball, a lightning bolt, a slow moving projectile that chases you to the depths of hell, etc. Good luck and let me know if i can help. EDIT: fail adding strikethrough to my text
It is a great tutorial, but for some reason, my location variable which is the point of the raycasthit2D always returns my world origin (0, 0, 0), and it pulls the players to the center of the game.