Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by sebimihaila9 · Mar 10, 2021 at 11:28 AM · raycast2d game2d-physicsraycasthitraycasthit2d

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)); }

} alt text

example-1.png (87.7 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

239 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges