Question by
kofficoffee · Sep 13, 2018 at 06:33 PM ·
2d gamerigidbody2dvelocitytopdownz-axis
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
Good day.
This script was done by you? How is posible you dont know how to do it if you wrote al lthis script?
If the script is not yours, and you just copy paste it, you should go learn more about movement, vectors, addforce and velocity,
Bye!
Your answer
Follow this Question
Related Questions
How to stop cube from sliding (rg2d.velocity) 1 Answer
Acceleration/Deceleration on a Rigidbody2D 0 Answers
How to reverse a reading of degrees 2 Answers
No overload for method. (Question) 0 Answers