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 /
avatar image
0
Question by Ilias931 · Mar 29, 2019 at 06:58 PM · movementtransform2d game2d sprites2d rotation

Bullet follows player mouvement when It instintiates.

I want it to just translate to the direction i give it not follow my mouvement. Thanks for your help! bullet script using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Bullet : PhysicsObject {

 public float Speed;
 public float LifeTime;
  
   

 public GameObject destroyEffect;
 public Rigidbody2D rg2D;
   

 void Start()
 {
     Invoke("DestroyEffect", LifeTime);
 }


 private void Update()
 {






     
     if ( GameObject.Find("ak").GetComponent<SpriteRenderer>().flipX )
         {





         transform.Translate(-transform.right * Speed * Time.deltaTime);
         GetComponent<SpriteRenderer>().flipX = true;
           
         
        

          }
     else 
     {

         transform.Translate(transform.right * Speed * Time.deltaTime );
         GetComponent<SpriteRenderer>().flipX = false;
         


     }
     


     

             
         
     






 }






 void DestroyProjectile()
 {

     Instantiate(destroyEffect, transform.position, Quaternion.identity);
     Destroy(gameObject);




 }



   

}

Weapon script

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class weapon : MonoBehaviour { public GameObject Projectile; public Transform ShootPoint;

 private float TimeBtwShots;
 public float StartTimeBtwShots;


 public float MaxSpeed = 7f;
 public float jumpTakeOffSpeed = 7;
 private SpriteRenderer SpriteRenderer;
 private float MouvementSpeed;





 // Update is called once per frame
 private void Update()
 {


     Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    





     if (TimeBtwShots <= 0) ;
     {
         if (Input.GetButtonDown("Fire1"))
         {

             Instantiate(Projectile, ShootPoint.position, transform.rotation);


             TimeBtwShots = StartTimeBtwShots;





             

             




         }
         else
         {

             TimeBtwShots -= Time.deltaTime;


         }
     }









 }


 void FixedUpdate()
 {


     if (GetComponent<Rigidbody2D>().velocity.x < 0)
     {


         GetComponent<SpriteRenderer>().flipX = true;
     }


     if (GetComponent<Rigidbody2D>().velocity.x > 0)
     {


         GetComponent<SpriteRenderer>().flipX = false;
     }


 }



















}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Tsaras · Mar 30, 2019 at 08:57 AM

If I understand correctly, you are basing the projectile's movement based on the facing of your ak in the Update method (which means in every frame). This is not a good solution because obviously your facing can change. What you can easily do is have the weapon script communicate to the bullet script the direction with which to move.

In bullet script:

 int direction;
 
 public void SetDirection(int _direction)
 {
     direction = _direction;
 }
 
  void Start()
  {
      Invoke("DestroyEffect", LifeTime);
      if (direction < 0)
      {
         GetComponent<SpriteRenderer>().flipX = true;
      } 
      else 
      {
         GetComponent<SpriteRenderer>().flipX = false;
      }
  }
 
 private void Update()
 {
     transform.Translate(transform.right * direction * Speed * Time.deltaTime);
 }

In weapon script:

 int direction;
 SpriteRenderer rend;    //cache this, don't look for it every fixed update
 Rigidbody2D rb2d;    //same
 
 void Start()
 {
     rend = GetComponent<SpriteRenderer>();
     rb2d= GetComponent<Rigidbody2D >();
 }

  void FixedUpdate()
  {
      if (rb2d.velocity.x < 0)
      {
          rend.flipX = true;
          direction = -1;
      }
      if (rb2d.velocity.x > 0)
      {
          rend.flipX = false;
          direction = 1;
      }
  }
  
   private void Update()
  {
      Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
     
      if (TimeBtwShots <= 0) ;
      {
          if (Input.GetButtonDown("Fire1"))
          {
             
             GameObject bullet = Instantiate(Projectile, ShootPoint.position, transform.rotation);
             Bullet bulletScript = bullet.transform.GetComponent<Bullet>();    //get Bullet script
             bulletScript.SetDirection(direction);    //set direction same as the direction of weapon
             TimeBtwShots = StartTimeBtwShots;
          }
          else
          {
              TimeBtwShots -= Time.deltaTime;
          }
      }
  }


Comment
Add comment · Show 4 · 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 Ilias931 · Mar 30, 2019 at 12:57 PM 0
Share

Thank you alot for the help hope it works!

avatar image Ilias931 · Mar 31, 2019 at 05:03 PM 0
Share

I apparently have a problem the bullet is not translating it just stands still. I forgot to tell that I have made the bullet a prefab is that a problem?

avatar image Tsaras Ilias931 · Mar 31, 2019 at 07:21 PM 0
Share

Check if the bulletScript is indeed getting cached and SetDirectoin is called. Use a breakpoint to pause execution and move line by line to see what the variables hold. If the bullet is not moving then something in this line is probably 0. transform.Translate(transform.right * direction * Speed * Time.deltaTime);

avatar image Ilias931 · Apr 01, 2019 at 07:00 PM 0
Share

I just realized that the firepoint is not rotating

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

122 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

Related Questions

My player is not rotating upwards when it moves upwards. Any advice? 1 Answer

Shooter 2D - Problem with rotation of bullet to face direction (velocity) 2 Answers

2D RPG boomerang instantiation help. 2 Answers

How do I flip a sprite based on parent's rotation? 1 Answer

Slow Sprite Rotation Toward Movement Direction - Top-Down 2D Game 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