- Home /
Player movement script for a stickman
Hello there! I am currently working on a 2D Stickman game, and you are, well, a stickman. I made the stickman following this tutorial: https://www.youtube.com/watch?v=93yUC5ASdv8&t=549s I just need a movement script. Now, im a C# newblet. So i cant make a movement script for the stickman. I found one in the comments of the stickman tutorial video, but it wasnt really good. If anyone can send me a C# movement script for my stickman, that would be great, thanks!
Answer by Maddoxs74 · Jun 01, 2020 at 12:50 AM
Make player with collider 2d and rigidbody 2d. add new emptyobject as child to player. Place it in the bottom from player (not in the players collider). Put this empty in the rayorigin and adjust raycheckdistance (maybe 0.1)
using UnityEngine; using System.Collections;
public class Move : MonoBehaviour {
public float speed;
public float jump;
public GameObject rayOrigin;
public float rayCheckDistance;
Rigidbody2D rb;
void Start () {
rb = GetComponent <Rigidbody2D> ();
}
void FixedUpdate () {
float x = Input.GetAxis ("Horizontal");
if (Input.GetAxis ("Jump") > 0) {
RaycastHit2D hit = Physics2D.Raycast(rayOrigin.transform.position, Vector2.down, rayCheckDistance);
if (hit.collider != null) {
rb.AddForce (Vector2.up * jump, ForceMode2D.Impulse);
}
}
rb.velocity = new Vector3 (x * speed, rb.velocity.y, 0);
}
}
check the scene for setting 5.5.2 Unity
or other way with physic2D.overlap...
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Move : MonoBehaviour {
public LayerMask whatIsGround;
public Transform groundCheck;
public bool isGrounded;
public float jumpForce;
public float speed;
Rigidbody2D rb;
void Start ()
{
rb = GetComponent <Rigidbody2D> ();
}
void Update () {
if (Input.GetButtonDown ("Jump") && isGrounded) {
rb.AddForce (Vector2.up * jumpForce, ForceMode2D.Impulse);
isGrounded = false;
}
}
void FixedUpdate ()
{
isGrounded = Physics2D.OverlapPoint (groundCheck.position, whatIsGround);
float x = Input.GetAxis ("Horizontal");
Vector3 move = new Vector3 (x * speed, rb.velocity.y, 0f);
rb.velocity = move;
}
}
Hey there. Thanks for the script again. I`ve tried it now and i`ve got some questionable stuff. For now, im just gonna leave it at this, and come back at it later, and maybe make a movement script myself inspired by yours. I truly appriciate the time and effort you`ve put into this, but its sadly not working for me. As i said, im going to come back at it later and maybe make a movement script inspired by yours. Thanks for helping, have a nice day.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
[2D] Moving the player 1 tile at a time using rigidbody movement 0 Answers
2D player keeps getting dragged to the left for some reason. 0 Answers
How do i make a cube move (Continuosly without stopping) when i press a button once in unity 2D 2 Answers
How to make the sprite move a certain amount of pixels when a key is pressed? 1 Answer