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 harpoaian · Jul 27, 2017 at 04:03 PM · instantiateraycast2d-platformer2d platformerenemyai

Enemy is shooting the opposite direction of player

Hello everyone, as the question says I am having an issue with my Grunt enemy class. It shoots its bullets when the player is in range of the raycast. The problem is it is shooting opposite from the player XD I want it to shoot in the direction of the player. Below are the scripts to make the grunt work. I have been looking around online and I couldn't find a solid solution. Thank you in advance!

Kindly,

Harpoaian

Grunt Bullet Data Model:

 Grunt theGrunt;
     Vector3 pos;
     Vector3 bulletvelocity;
     // Use this for initialization
     void Start()
     {
         theGrunt = GameObject.Find("Grunt").GetComponent<Grunt>();
         bulletvelocity = new Vector3(bulletSpeed * Time.deltaTime, 0, 0);
         pos = transform.position;
         theGrunt = GetComponent<Grunt>();

     if (GameObject.Find("Grunt").GetComponent<GruntStateMachine>().colliding)
     {
         bulletvelocity = new Vector3(bulletSpeed * 1 * Time.deltaTime, 0, 0);
         //transform.position += transform.right * Time.deltaTime * bulletSpeed;
     }

     else 
     {
         bulletvelocity = new Vector3(bulletSpeed * -1 * Time.deltaTime, 0, 0);
         //transform.position -= transform.right * Time.deltaTime * bulletSpeed;
         //Debug.Log("I should shoot left");
     }



 }

 // Update is called once per frame
 void Update()
 {
     pos += bulletvelocity;
     transform.position = pos;
     // transform.position += transform.right * Time.deltaTime * bulletSpeed;




 }


Grunt Statemachine:

 void Vision()
         {
             Debug.DrawLine(sightStart.position, thePlayer.position, Color.red);
             spotted = Physics2D.Linecast(sightStart.position, sightEnd.position, 1 << LayerMask.NameToLayer("Player"));
 
         if(spotted == true)
         {
             SetState(GruntStates.FIRE);
             Debug.Log("I should fire!");
         }
         else
         {
             SetState(GruntStates.PATROL);
         }
     }
 
 void Patrol()
     {
         //enemy movement goes here
         
         GetComponent<Rigidbody2D>().velocity = new Vector2(moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
 
         colliding = Physics2D.Linecast(sightStart.position, wallEnd.position, detectWhat);
 
         if(colliding == true)
         {
             transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y);
             moveSpeed *= -1;
             
         }
 
         Debug.DrawLine(sightStart.position, wallEnd.position, Color.green);
     }
 
 void FiringState()
     {
         trackTime += Time.deltaTime;
         Vision();
         Debug.Log("In firing state!");
         //transform.LookAt(thePlayer);
 
         if(trackTime >=2)
         {
             Instantiate(bullet, transform.position, transform.rotation);
             //Instantiate(bullet, sightStart.position, thePlayer.transform);
 
             trackTime = 0;
         }
     }




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 LSPressWorks · Jul 27, 2017 at 10:29 PM

using *-1 reverses the direction of stuff

 bulletvelocity = new Vector3(bulletSpeed * -1 * Time.deltaTime, 0, 0);


has that exact effect.

try making that not say -1 and let us know

Comment
Add comment · Show 2 · 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 harpoaian · Jul 28, 2017 at 12:48 AM 0
Share

That didn't work it just made my grunts bullets go right. I have discovered something though. $$anonymous$$y Grunt does work as intended BUT he isn't consistent. Would I be able to make the projectile instantiate and go the same direction that my raycast is facing?

avatar image harpoaian · Jul 30, 2017 at 09:49 AM 0
Share

I figured out the answer. Here is the code that I used. Basically I called the public variable facingRight in my Grunt State $$anonymous$$achine. $$anonymous$$y Grunt now shoots the correct direction that it's facing and will only fire when the player is in front of it.

 if (GameObject.Find("Grunt").GetComponent().facingRight == true)
         {
             bulletvelocity = new Vector3(bulletSpeed * 1 * Time.deltaTime, 0, 0);
             //transform.position += transform.right * Time.deltaTime * bulletSpeed;
             Debug.Log("I should shoot right");
         }
 
         else if (GameObject.Find("Grunt").GetComponent<GruntState$$anonymous$$achine>().facingRight == false)
         {
             bulletvelocity = new Vector3(bulletSpeed * -1 * Time.deltaTime, 0, 0);
             //transform.position -= transform.right * Time.deltaTime * bulletSpeed;
             Debug.Log("I should shoot left");
         }


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

105 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

Related Questions

2D Platformer Shooting - Raycast Issue 0 Answers

Does anyone know how to make an object(enemy) follow a path set by the player in a 2D side scroller? 1 Answer

Issue with Raycast hit.distance Variables Reseting Itself 0 Answers

Bullets with raycast and physics 2 Answers

2d platformer stop falling platform bounce. 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