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 Juliord · Feb 08, 2017 at 09:48 PM · c#scripting problemshootingdirection2d platformer

Trying to have bullets travel in the direction the player is facing

Help me out!

I'm trying to do a very basic side scroller where the player may shoot. I already got to the point where I can make the player turn depending on the direction she is moving, but when I fire the bullet always travelled right.

After making a few modifications to the code I realized I could get the reference of the x scale of the player to know where she is facing and then use that to cast the ray line in that direction. I'm not really sure how to do it though, I'm currently trying this code from the two scripts:

Player script: using UnityEngine; using System.Collections;

public class PlayerController : MonoBehaviour {

 public Rigidbody2D rb2d;

 [SerializeField]
 private float speed;
 private bool facingRight;
 private Animator anim;


 void Start () 
 {
     rb2d = GetComponent<Rigidbody2D> ();
     facingRight = true;
     anim = GetComponent<Animator> ();

 }

 void FixedUpdate()
 {
     float horizontal = Input.GetAxis ("Horizontal");
     HandleMovement (horizontal);

     Flip (horizontal);
 }

 void Update () 
 {
     
 }

 private void HandleMovement (float horizontal)
 {
     rb2d.velocity = new Vector2 (horizontal * speed, rb2d.velocity.y);
     anim.SetFloat ("Speed", Mathf.Abs(horizontal));
 }

 private void Flip (float horizontal)
 {
     if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight) 
     {
         facingRight = !facingRight;

         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 }

}

Weapon script:

using UnityEngine; using System.Collections;

public class Weapon : MonoBehaviour {

 public float fireRate = 0;
 public float weaponDamage = 10;
 public LayerMask whatToHit;
 public Transform bulletTrailPrefab;
 


 float timeToFire = 0;
 Transform firePoint;
 Transform faceDirection;

 void Awake () 
 {
     faceDirection = GameObject.FindGameObjectWithTag ("Player").transform;
     firePoint = transform.FindChild ("FirePoint");
     if (firePoint == null) 
     {
         Debug.LogError ("No fire point");
     }
 
 }

 void Update () 
 {
     if (fireRate == 0) 
     {
         if (Input.GetButtonDown ("Fire1")) 
         {
             Shoot ();
         }
     } 
     else 
     {
         if (Input.GetButtonDown ("Fire1") && Time.time > timeToFire) 
         {
             timeToFire = Time.time + 1 / fireRate;
             Shoot ();
         }
     }
 }

 void Shoot ()
 {
     Vector2 fireDirection = new Vector2 (faceDirection.localScale.x, 0.0f);
     Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
     Effect ();
     RaycastHit2D hit = Physics2D.Raycast (firePointPosition, fireDirection, 100, whatToHit);
 }

 void Effect ()
 {
     Instantiate (bulletTrailPrefab, firePoint.position, firePoint.rotation);
 }

}

Comment
Add comment · Show 3
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 Juliord · Feb 08, 2017 at 09:50 PM 0
Share

I am getting this error BTW

NullReferenceException: Object reference not set to an instance of an object Weapon.Awake () (at Assets/Scripts/Weapon.cs:19)

avatar image Juliord · Feb 09, 2017 at 12:23 AM 0
Share

I figured out the error, I hadn't tagged the player.

It still shoots only to the right though :(

avatar image Juliord · Feb 09, 2017 at 12:55 AM 0
Share

got it to work!!!! :):):):)

0 Replies

· Add your reply
  • Sort: 

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

321 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 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 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 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 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

Move parent without moving the child 1 Answer

C# 2D Shooting a bullet based on player direction 1 Answer

Jitter when moving around a circular path 0 Answers

NEED HELP PLEASE! Unexpected symbol. 0 Answers

How can i make the right choice of implementation of my shooting fire script ? 1 Answer


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