- Home /
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
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.
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!
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
![](https://koobas.hobune.stream/wayback/20220612233947im_/https://answers.unity.com/themes/thub/images/avi.jpg)