Hi, i’m developing a 2D game. I am having a lot of difficulty on how to make my player move. I need it so when i tap the screen it jumps up in that direction. And every time tap the screen the player jumps again but in that direction
This is the first game I am creating, i have done some tutorials but none have prepared me for this moment
I am sorry if my question was not clear, please feel free to ask for any details. Also if it helps i am coding in c#.
Take my script. Learn it. Master it. Also, do not ever ask for scripts on these forums. You have to put effort in your part. Try googling it because I promise you that the answer is there. The only reason I am giving you this script is because I happened to have it opened here and I am feeling a bit generous today. Go ahead and use it. If it doesnt work (which I promise you it won’t at first), then do not ask the Answers site. Figure it out yourself. If you can’t figure it out, then you have to start on something easier and learn how to program in general at first. Knock yourself out:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
//Player variables====================================
public float speed; // How fast the player will go
public float jumpHeight; // Amount of force added when the player jumps.
public LayerMask whatIsGround; // A mask determining what is ground to the character
//Checks==============================================
private bool facingRight = true; // For determining which way the player is currently facing.
private bool grounded = false; // Whether or not the player is grounded
//References==========================================
private Transform groundCheck; // A position marking where to check if the player is grounded.
private Rigidbody2D playerRigidbody; // A reference to the rigidbody
void Awake()
{
// Setting up references.
groundCheck = transform.Find("GroundCheck");
playerRigidbody = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
// The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
grounded = Physics2D.OverlapCircle(groundCheck.position, 0.2f, whatIsGround);
}
void Update()
{
Move();
}
void Move()
{
//Get inputs
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
Vector2 movement = new Vector2();
//If the player is grounded and presses up, then set velocity to jumpHeight. If not, keep the velocity the same
if (grounded && v > 0f)
movement.y = jumpHeight;
else
movement.y = playerRigidbody.velocity.y;
//Apply horizontal movement
movement.x = h * speed;
//Apply the movement vector
playerRigidbody.velocity = movement;
//Flip the player based on input
if (h > 0f && !facingRight)
Flip();
else if (h < 0f && facingRight)
Flip();
}
void Flip()
{
// Switch the way the player is labelled as facing.
facingRight = !facingRight;
// Multiply the player's x local scale by -1.
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
EDIT: I just saw your other thread asking how to do camera movement. That is extremely unnecessary and you could have simply asked it right here. That thread just shows me that you didn’t learn anything from the “lots of tutorials you had gone through”. Do not post another thread like that, especially when it’s in the wrong forum. Learn to do it yourself because no one is going to do all your work for you.
You’re right! Sorry OP. That link I took massive amounts of time to grab and provide that goes into detail about jumping, double jumping and moving a 2D character was cold.
Instead, to show I care, I’ll redirect you to google your question because it has been asked more times than I would like to count so the answer is out there waiting. P.S. Love you and please come back when you have another question.
Sure it teaches you how to jump and double jump, but it doesn’t teach you to move player. If you saw technique used to achieve an infinite runner it was scrolling textures, no moving player involved.
Instead, to show OP that you care, explain him how things work on the forum instead of adding a link which took you “massive amount of time to find and grab” without any explanation whatsoever.
If you had watched the video before commenting you would know how wrong you are but I wasn’t providing the link for you, so I don’t care.
Regardless, I supplied the OP with more information than you cared to share. It’s a forum. A place to converse and ask questions. You chose a quick elitist redirection to google and I chose to provide only a link. Both were cold and impersonal but one actually provides an answer.
Thanks a lot everyone!
An I would like to Apologize for posting a thread on camera movement instead of the just posting it on here.
I will make sure to do my research and only use the forums for coding help as an last resort.