Im making a top down 2D game and i just need to know how to control a player and make it move.
Thanks
Im making a top down 2D game and i just need to know how to control a player and make it move.
Thanks
In Standard Assests/Prefabs there is a first person controller prefab you can probably modify for your needs.
Problem is, iâm really bad at this stuff, so how could i modify it for my needs?
Iâm not really the techie type with this either, but you could delete the camera in the prefab and set a new camera over your scene and the player should move around with the WASD and arrow keys in the scene which you could watch from above where you have placed the new camera.
Iâll give it a shot, thanks
It didnât work, when i move the mouse it moves it back and forth and as soon as i press one of the WASD keys it starts moving down slowly and doesnât stop.
What is the âitâ you are referring to, the camera or the player?
The player
A top down controller could look like this:
Create an empty game object and name it âcharacterâ or the like.
Add the following components:
Rigidbody with weight 50-100
Capsule collider with radius 0.4 and length 1.0 on the Y axis as direction.
A simple mesh representation (this is your character mesh)
(Alternatively to the below, just use the camera that is set up in the default scene)
Add a game object and name it âcameraâ. Move it so it is a child of the object above.
Add the following components:
Camera
Move the âcameraâ gameobject around so that it is looking down on top of the âcharacterâ object.
Create a script named âTopDownCharacterControllerâ and add it to the âcharacterâ game object.
Open the âTopDownCharacterControllerâ script and add some code to control the movement.
The control script might look like:
using UnityEngine;
public class TopDownCharacterController : MonoBehavkiour
{
public transform Camera;
public float MoveSpeed = 10f;
private Vector3 _lastPosition;
private void Start()
{
if(!Camera)
Camera = Camera.main;
_lastPosition = transform.position;
}
private void Update()
{
Vector3 movement = new Vector3(Input.GetAxis ("Horizontal"), 0, Input.GetAxis("Vertical"));
transform.Translate(movement*Time.deltaTime*MoveSpeed);
transform.rotation = Quaternion.LookRotation(transform.position - _lastPosition, Vector3.up);
_lastPosition = transform.position;
}
}
This will rotate the camera along with the character object, which is probably not what you want, and it doesnât smooth any rotations, but it might get you started. Additionally, Iâm at work, so I canât test it, but it should do the trick
Happy coding
/Klum
I give up, it doesnt seem to work for me
Iâm sticking to FPS games thanks to the trusty First person controller prefab
Have a look at this movie, the setup is as I mentioned above, using the FPC prefab, but moving the camera out of the prefab (or creating a new one) and placing it in position above the scene. Iâm thinking the problem you are having is that you are losing orientation in your movement. Putting an object on the front of the cylinder will show you which way the object will move when you press the keys.
Iâm not quite sure what iâm doing wrong but i give up :?
I know it is easy to get frustrated when you donât understand how things are working and they seem to be working against you.
Maybe try again a little later when the inspiration again arises.
Or do some research on the game type to see if you can get some insight.
Was the example in the movie in the direction of what you were looking for?
I would post the scene but itâs using the version 3 b2 program so unless you have that version I donât think it would be usable to you.