Hookshot in Unity 2D not working,Hookshot 2D not working
I want to start by saying i am by no means a coder. :)) I recently saw CodeMonkey s video about a hookshot mechanic and i tried to modify the code so it suits my 2d game. But as expected i got a lot of errors. If somebody with more experience can help a little bit i would be very grateful. I attached an image to this question. What I want is the piggy to move just by hookshot when you press on the sun. Here is what i ve done :
public class Hook : MonoBehaviour {
[SerializeField] private Transform debugHitPointTransform;
[SerializeField] private Transform hookshotTransform;
private float characterVelocityY;
private Vector2 characterVelocityMomentum;
private State state;
private Vector2 hookshotPosition;
private float hookshotSize;
private enum State
{
Normal,
HookshotThrown,
HookshotFlyingPlayer,
}
// Apply gravity to the velocity
float gravityDownForce = -60f;
characterVelocityY += gravityDownForce* Time.deltaTime;
// Apply Y velocity to move vector
characterVelocity.y = characterVelocityY;
// Apply momentum
characterVelocity += characterVelocityMomentum;
// Move Character Controller
characterController.Move(characterVelocity* Time.deltaTime);
// Dampen momentum
if (characterVelocityMomentum.magnitude > 0f)
{
float momentumDrag = 3f;
characterVelocityMomentum -= characterVelocityMomentum* momentumDrag * Time.deltaTime;
if (characterVelocityMomentum.magnitude< .0f)
{
characterVelocityMomentum = Vector2.zero;
}
}
}
private void ResetGravityEffect()
{ characterVelocityY = 0f; }
private void HandleHookshotStart() { if (TestInputDownHookshot()) { Ray ray = camera.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { // Hit something debugHitPointTransform.position = raycastHit.point; hookshotPosition = raycastHit.point; hookshotSize = 0f; hookshotTransform.gameObject.SetActive(true); hookshotTransform.localScale = Vector2.zero; state = State.HookshotThrown; } } }
private void HandleHookshotThrow() { hookshotTransform.LookAt(hookshotPosition);
float hookshotThrowSpeed = 500f;
hookshotSize += hookshotThrowSpeed * Time.deltaTime;
hookshotTransform.localScale = new Vector2(1, 1, hookshotSize);
if (hookshotSize >= Vector2.Distance(transform.position, hookshotPosition))
{
state = State.HookshotFlyingPlayer;
}
}
private void HandleHookshotMovement() { hookshotTransform.LookAt(hookshotPosition);
Vector2 hookshotDir = (hookshotPosition - transform.position).normalized;
float hookshotSpeedMin = 10f;
float hookshotSpeedMax = 40f;
float hookshotSpeed = Mathf.Clamp(Vector2.Distance(transform.position, hookshotPosition), hookshotSpeedMin, hookshotSpeedMax);
float hookshotSpeedMultiplier = 5f;
float reachedHookshotPositionDistance = 1f;
if (Vector2.Distance(transform.position, hookshotPosition) < reachedHookshotPositionDistance)
{
// Reached Hookshot Position
StopHookshot();
}
else bool v = (TestInputDownHookshot());
{
// Cancel Hookshot
StopHookshot();
}
}
private void StopHookshot() { state = State.Normal; ResetGravityEffect(); hookshotTransform.gameObject.SetActive(false); }
private bool TestInputDownHookshot() { return Input.GetMouseButtonDown(0)); }
}
Your answer
Follow this Question
Related Questions
Making a GameObject follow the direction of a RayCast 1 Answer
RayCastHit 2d with layerMask not workin 0 Answers
How to change the position of a Raycast (on Center of the Player 2D)? 0 Answers
RaycastHit2D problem 0 Answers
Raycast2D 360 Rotation 0 Answers