- Home /
Sliding movement script in 2D (doesn't stop)
Hello! I would need a script that can move the character up, down, right, left (in 2D) so that it slides further and doesn’t stop. Thanks in advance for the comments. :)
Answer by Anonymous620 · Jul 07, 2021 at 12:27 PM
this is a script that gives the object a steady speed:
GameObject player;
public float speed;
Vector2 direction;
// Start is called before the first frame update
void Start()
{
player = GameObject.Find("Player");
}
// Update is called once per frame
void Update()
{
if(direction.y != 0)
{
player.transform.Translate(0, speed * Time.deltaTime * direction.y, 0);
}
if(direction.x != 0)
{
player.transform.Translate(speed * Time.deltaTime * direction.x, 0, 0);
}
if (Input.GetKey(KeyCode.UpArrow))
{
direction.y = 1;
direction.x = 0;
}
if (Input.GetKey(KeyCode.DownArrow))
{
direction.y = -1;
direction.x = 0;
}
if (Input.GetKey(KeyCode.LeftArrow))
{
direction.y = 0;
direction.x = -1;
}
if (Input.GetKey(KeyCode.RightArrow))
{
direction.y = 0;
direction.x = 1;
}
}
Thanks for writing the script, it works well. I just want to be able to walk the lines. My question is, can you monitor the pixel color of the line next to the Player? Thanks for the help, I'm still a beginner.
Are the lines line renderers or gameobjects
It appears that the lines are just a single sprite
Also, in what directions do you want to detect or do you just want to detect in the direction the player is pointing
I'd like to do a similar game to Super Gridder.
https://www.youtube.com/watch?v=WU50VggB79Q
Thank you for your help.
Answer by Harry_Drew · Jul 07, 2021 at 11:54 AM
Maybe try using rigidbody.Addforce and a low friction on the surface with a physics material?
Your answer
Follow this Question
Related Questions
Player Character No Longer Moving 0 Answers
My player doesn't turn based on the mouse when it's supposed to 0 Answers
Android based ARCore example ObjectManipulation using Joystick Controller 0 Answers
Convert current rigidbody velocity to transform.forward 0 Answers
[2d] Player sometimes doesn't jump up while moving and jumping at the same time 1 Answer