Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by goyaniharsh3939 · May 11, 2020 at 04:19 PM · movementinputplayer

Player move along fixed line with inputs of keybord keys for increasing and decreasing the speed and up and down for take turn!

hello, I am trying to make game in which`there is line created by me before the start of game(not like in running game) and player(circle ) only can move along that line by arrow keys like if I press forward key it go forward in line , if I press backward it goes backward in line , if I press up or down it turn in the line if any turn there has. That means that forward, backward, up,down is globally and also if my line is in horizontal direction and i press vertical arrow (Up Key) then it will not move from the line in vertical direction of line path. so, it is like line following (with input )but contain physics component because i have to use force on that object for other matters, so what kind of code it has to take.. I don't know whether I use boxcolider on adjacent to line or anything else after using box collider it does not look good so what should I use for this type of problem? Thank you for Advance!

[1]: /storage/temp/159226-img-20200512-020040.jpg

img-20200512-020040.jpg (35.6 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by darksider2000 · May 11, 2020 at 09:34 PM

If I understand you correctly, you want the ball to be tethered to he line, so it can only go forward/backward along the line and can't break away.

This can be done with points along the line, to give your character the direction of travel. alt text


If you have a LineRenderer line, you can use its vertices as points. Otherwise you can make your own points.

The idea is that your ball will always be travelling between two points, the direction will always be A -> B. The input won't control direction of travel but rather a velocity/force multiplier (That will go from -1 to 1).

 private Vector2 nextPoint; // The point ahead of us
 private Vector2 lastPoint;  // The point behind us
 
 void MoveOnLine () {
        Vector2 direction = nextPoint - lastPoint; // The direction of travel
        float horizontalInput = Input.GetAxisRaw("Horizontal"); // Input
        float verticalInput = Input.GetAxisRaw("Vertical");           // Input
        float horizontalAngle = Vector2.Angle(Vector2.right, direction);
        float verticalAngle = Vector2.Angle(Vector2.up, direction);
        // Calculate magnitude based on the angle of direction and input
        float magnitude = Mathf.Cos(horizontalAngle) + Mathf.Cos(verticalAngle);
        magnitude = Mathf.Clamp(magnitude, -1f, 1f); // This is to avoid further math calculations
 
        rigidbody.velocity = direction * magnitude; // Replace rigidbody.velocity with preferred method of movement
 }



This function will use nextPoint and lastPoint to determine direction, and calculate how much (and also forward/backward) to move based on input.

This is the simple form of the idea, you'll have to write the rest of the code. Namely you'll need a function to fetch new points as you travel down the line, that would depend on how you make your points.

Good luck!


moveonline.png (37.4 kB)
Comment
Add comment · Show 1 · Share
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
avatar image goyaniharsh3939 · May 12, 2020 at 06:05 PM 0
Share

Thanks , But can you tell me that how the velocity and change vertex has to operate in this i try to know about how to change vertx and run but i can not do ,, Here is $$anonymous$$y code that i tried but it is not working good, **

   public Vector2 nextPoint; // The
        point ahead of us
            public Vector2 startPoint;  // The point behind us
            public Rigidbody2D rigidbody;
        
        void $$anonymous$$oveOnLine()   {  Vector2
        direction = nextPoint - startPoint;
        // The direction of travel
                    float horizontalInput = Input.GetAxis("Horizontal");  
                    float verticalInput = Input.GetAxis("Vertical");           
                    float horizontalAngle = Vector2.Angle(Vector2.right,
        direction);
                    float verticalAngle = Vector2.Angle(Vector2.up, direction);
              float magnitude_Horizontal = horizontalInput*
        $$anonymous$$athf.Cos(horizontalAngle);
             float magnitude_Vertical = $$anonymous$$athf.Cos(verticalAngle) *
        verticalInput;
                    rigidbody.velocity = direction * magnitude_Vertical +
        direction * magnitude_Horizontal;}


 

 

   

but it will not move for incline line and also i don't know how to change the vertex and move the object so please give me some guidelines .i am trying to solve this for two days and i didnt make progress . if it is not possible then please tell me where can i learn this type of thing . thank you for Healping me!

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

224 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

Related Questions

Stopping an object immediately 1 Answer

Player moves left even when not pressing any buttons, help! 4 Answers

,Look rotation viewing vector is zero and character always facing 1 direction when idle 0 Answers

Player(Squre) move along fixed line (which can be straight , incline, circuler etc) with inputs of joystick ! 1 Answer

Problems with the movement of the player 1 Answer


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