Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by misanthropowitsch · Oct 07, 2016 at 08:26 PM · transform.positiondirectioncontrolsanglestransform.rotate

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?)

  1. How can you make a bullet from a Spawnpoint attached to the player movein the same direction as the player is facing?

  2. 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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image misanthropowitsch · Oct 10, 2016 at 03:00 PM 0
Share

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);

avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

77 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges