- Home /
Question by
kofficoffee · Sep 13, 2018 at 06:47 PM ·
2drigidbody2dvelocitytopdownroguelike
How do I make my character move on the Z axis on 2d rigidbody?
Im using rigidbody2ds velocity feature for movement and i need to move my character on the Z axis, problem is I cant figure out how to enable the Z axis for rigidbody 2d. It's a topdown roguelike sprite game with movement similar to binding of isaac. This is my current script.
using UnityEngine;
/// <summary>
/// Player controller and behavior
/// </summary>
public class PlayerScript : MonoBehaviour
{
/// <summary>
/// 1 - The speed of the ship
/// </summary>
public Vector3 speed = new Vector3(50, 50, 50);
// 2 - Store the movement and the component
private Vector3 movement;
private Rigidbody2D rigidbodyComponent;
void Update()
{
// 3 - Retrieve axis information
float inputX = Input.GetAxis("Horizontal");
float inputZ = Input.GetAxis("Vertical");
float inputY = Input.GetAxis("Y");
// 4 - Movement per direction
movement = new Vector3(
speed.x * inputX,
speed.y * inputY,
speed.z * inputZ);
// ...
// 5 - Shooting
bool shoot = Input.GetButtonDown("ArrowRight");
bool shootup = Input.GetButtonDown("ArrowUp");
bool shootdown = Input.GetButtonDown("ArrowDown");
bool shootleft = Input.GetButtonDown("ArrowLeft");
// Careful: For Mac users, ctrl + arrow is a bad idea
if (shootdown)
{
WeaponScriptDown weaponup = GetComponent<WeaponScriptDown>();
if (weaponup != null)
{
// false because the player is not an enemy
weaponup.Attack(false);
}
}
if (shootleft)
{
WeaponScriptLeft weaponup = GetComponent<WeaponScriptLeft>();
if (weaponup != null)
{
// false because the player is not an enemy
weaponup.Attack(false);
}
}
if (shootup)
{
WeaponScriptUp weaponup = GetComponent<WeaponScriptUp>();
if (weaponup != null)
{
// false because the player is not an enemy
weaponup.Attack(false);
}
}
if (shoot)
{
WeaponScript weapon = GetComponent<WeaponScript>();
if (weapon != null)
{
// false because the player is not an enemy
weapon.Attack(false);
}
}
// ...
}
void FixedUpdate()
{
// 5 - Get the component and store the reference
if (rigidbodyComponent == null) rigidbodyComponent = GetComponent<Rigidbody2D>();
// 6 - Move the game object
rigidbodyComponent.velocity = movement;
// 7 - Z axis in rigidbody
}
}
Comment
if you are 2D then the only thing that the z axis will do is decide the order of the objects being displayed/ rendered on top of other objects.
2d means 2 axises. you should only be working with x and y reguardless if you are top down or not. y should always be up and down on the screen. the only difference with top down views is you dont use gravity.