Movement and Control Problems with understanding
Hi all! :) I want to make a game like Contra3/Metalslug. Classic 2D Sidescrolling Run&Gun Action.
You can shoot in 8 directions in games like this (Left, Right, Up, Down, Upright, Downright, Upleft and Downleft) In this games the left joystick is for both, facing direction and moving at the same Time (I don´t mind moving with the left joystick and facing direction with the right one.
Right now I am able to walk and shoot left and right. Kinda.. not what I wanted to do.
I searched for like a week now. Every link is purple and the next time I see Top Down Shooter Movement in my google feed I´m gonna throw my pc into the trashcan haha. Apparently I am searching for the wrong stuff (At least I hope so?)
How can you make a bullet from a Spawnpoint attached to the player movein the same direction as the player is facing?
How do velocity, rotation and movement in general work?
Suggestions or even a slight hint at where and how to start would be great. I´d be grateful for tutoriallinks or some small script example to get an idea of how stuff works, Thanks in regards, Dominik!
Answer by sid4 · Oct 08, 2016 at 06:09 PM
the bullets should be coming out of a object connected to the player so when he moves it will also move most likely it will be attached to the player as a child. check parent -child relationship this will work for you
but you have to adjust player to move in those positions first can u post what code u have so far for the player and shooting
Hi! First off - thanks for your reply!
Since I´m experimenting quite some time now to manage this, I came up with 2 separate scripts at the moment.
I have a firePoint/gun$$anonymous$$uzzle attached to the player and a separate script for the bullet/object which I want to instantiate.
Code Example 1:
//$$anonymous$$ovementCode in PlayerController
void Start() { active$$anonymous$$oveSpeed = moveSpeed; myRigidbody = GetComponent();
}
// Update is called once per frame
void Update()
{
if (Time.time >= timestamp && (Input.GetButton("Fire2")))
//if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Return)) //shooting stuff
{
Instantiate(ninjaStar, firePoint.position, firePoint.rotation);
timestamp = Time.time + timeBetweenShots;
}
//$$anonymous$$OVE
if (Input.GetAxisRaw("Horizontal") > 0f && can$$anonymous$$ove)
{
myRigidbody.velocity = new Vector2(active$$anonymous$$oveSpeed, myRigidbody.velocity.y);
transform.localScale = new Vector3(1f, 1f, 1f);
}
else if (Input.GetAxisRaw("Horizontal") < 0f && can$$anonymous$$ove)
{
myRigidbody.velocity = new Vector2(-active$$anonymous$$oveSpeed, myRigidbody.velocity.y);
transform.localScale = new Vector3(-1f, 1f, 1f);
}
else
myRigidbody.velocity = new Vector2(0, myRigidbody.velocity.y);
moveSpeed = Input.GetButton("Fire3") ? initialSpeed * run$$anonymous$$ultiplier : initialSpeed;
// and the Projectilecode in Projectile public class NinjaStarController : $$anonymous$$onoBehaviour {
public float speed;
public PlayerController player;
public GameObject impactEffect;
public GameObject DeathParticle;
void Start() {
player = FindObjectOfType<PlayerController>();
if(player.transform.localScale.x <0)
{
speed = -speed;
}
}
// Update is called once per frame
void Update() {
GetComponent().velocity = new Vector2(speed, GetComponent().velocity.y);
Answer by misanthropowitsch · Oct 10, 2016 at 03:20 PM
I managed to screw up posting the code didn´t I? Somehow this forums run very slow in my browser.
Example 2:
//PlayerMovement and instantiating the projectile
public class PlayerController : MonoBehaviour { public float maxSpeed;
private Rigidbody2D myRB;
private bool facingRight;
public Transform FirePoint;
public GameObject bullet;
// Use this for initialization
void Start () {
myRB = GetComponent<Rigidbody2D>();
facingRight = true;
}
// Update is called once per frame
void Update () {
if (Input.GetAxisRaw("Fire1") >0) fireRocket();
float move = Input.GetAxis("Horizontal");
myRB.velocity = new Vector2(move*maxSpeed, myRB.velocity.y);
if(move>0 && !facingRight)
{
flip();
} else if (move <0 && facingRight)
{
flip();
}
}
void flip()
{
facingRight=!facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
void fireRocket()
{
if(facingRight)
{
Instantiate(bullet, FirePoint.position, Quaternion.Euler (new Vector3 (0,0,0)));
}else if (!facingRight)
{
Instantiate(bullet, FirePoint.position, Quaternion.Euler (new Vector3 (0,0,180f)));
}
}
}
//Projectile
public class Projectile : MonoBehaviour { public float rocketSpeed;
Rigidbody2D myRB;
// Use this for initialization
void Awake () {
myRB = GetComponent<Rigidbody2D>();
if(transform.localRotation.z>0)
myRB.AddForce(new Vector2(-1,0) * rocketSpeed, ForceMode2D.Impulse);
else myRB.AddForce(new Vector2(1, 0) * rocketSpeed, ForceMode2D.Impulse);
}
// Update is called once per frame
void Update () {
}
}
According to your answer, I guess the best thing to do is to work with facing direction bools for like every single direction? (8 since I want to instantiate the projectile basically in 45 degrees for every direction)
By calling a parent you mean, the Child (firePoint in my case) would be moving with the direction the player is facing? Would be great, if that worked out. Apparently, I can´t even manage to make my character "look" Up and down as of now.
I probably don´t fully understand the whole direction Concept of Unity.
The second thing I don´t get about this is, how do I make my character move in the correct direction, to recognize, which angle should be used for the projectile to fire?
If something is unclear, pls ask. I know, this got fuzzy. Especially since I posted 2 Codes at once. I would be supergrateful if one thing worked out. I think, I get something terribly wrong with directions and parenting stuff/facing directions.
Thanks for your answer!
Your answer
Follow this Question
Related Questions
Very weird. Transform position is at two places at the same time. 1 Answer
Changing transform.position through a reference 1 Answer
How to Rotate and move a character like in WOW 0 Answers
return all models to original location (image target) 0 Answers
Check for current transform so the sprite can rotate properly? 0 Answers