New user question about 2D top down game and collision.
So I'm very, very new to Unity and game design and I'm having a problem as I started experimenting. I'm trying to create a top down 2D game like the original Zelda or Final Fantasy and I'm stuck ong etting the character's movement script working.
I wrote the following code and it was working nicely until I noticed that the character can't collide with any 2D colliders.
I also noticed he is unaffected by gravity (which I was planning to turn off, but it's currently on).
The character with the following script has both a box collider2D and a rigidbody2D attached.
I've been able to determine that the issue is coming from the way I'm updating the movement because if I comment out the "transform.position = pos;" line, then the character falls and collides, but I'm not entirely sure why my code isn't working. Some feedback would be really great!
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Character : MonoBehaviour
{
// configuration parameters
[SerializeField] float speed = 1f;
Vector2 pos;
// Use this for initialization
void Start()
{
pos = new Vector2(transform.position.x, transform.position.y);
}
// Update is called once per frame
void Update()
{
Movement();
}
// Fluid movement
private void Movement()
{
if (Input.GetKey(KeyCode.W))
{
pos.y += speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.S))
{
pos.y += -speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.A))
{
pos.x += -speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.D))
{
pos.x += speed * Time.deltaTime;
}
transform.position = pos;
}
}