Need help with Player Movement
Hey guys,
I am very new to Unity and am just learning. For my class I need to create a 2D game and I'm already stuck as I am not good with coding at all. I have no idea how to go about coding my movement script for my 2D sprite nor do I know how to code it to pick up the objects I have set around on my game. I have gone over the Roll a Ball tutorial again but since it is in 3D it doesn't really help me..Would anyone be able to help and explain to me how to get these going? I know somewhere along the line I need to use input.get button but I have no idea how to do so nor do I know what most of the code means. It all has to be in C#
Edit: I need to use Input.GetAxis to move horizontally and vertically. However, when I go to do so, the character will not move (but it says it is). It just falls out of my plane
Answer by Zoogyburger · Mar 01, 2017 at 07:49 PM
I highly suggest watching some tutorials such as:
https://www.youtube.com/watch?v=XZDjkQ8wEd0&list=PL_4rJ_acBNMH3SExL3yIOzaqj5IP5CJLC∈dex=4
The below script should work for you.
using UnityEngine;
using System.Collections;
public class CharacterMovement : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float input_x = Input.GetAxisRaw("Horizontal");
float input_y = Input.GetAxisRaw("Vertical");
bool isWalking = (Mathf.Abs(input_x) + Mathf.Abs(input_y)) > 0;
if(isWalking)
{
transform.position += new Vector3(input_x, input_y, 0).normalized * Time.deltaTime;
}
}
}
Thank you! The character is now moving. I plan on looking at those videos after class. Do you know if they include how to make pickups? On top of this, would you happen to know why my games walls can be walked through? I have a maze and all the walls have a collider on them and on top of this I have the sprite I put in set to "is trigger" but it can still walk through my pick ups, the maze, the enemy, etc..
Your answer
Follow this Question
Related Questions
MoveTowards not moving accurately. Getting stuck on target. 2 Answers
C# Script question 0 Answers
2D AI movement 0 Answers
[I still need help even though there is 2 replies] Help with shooter script 3 Answers
Player animation FSM stopped working 0 Answers