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 Blue-Mirror · Aug 28, 2016 at 10:47 AM · aishooting

Enemy shooting speed not precise

I have a script that makes the AI shoot at the current Player position. The part that works, is that he shoots at the Player. But the speed of the bullet isnt always the same ... and for some reason I cant get, how it should be. Well this is how I calculate the speed:

target = GameObject.FindGameObjectWithTag("Player").transform;

direction = (target.position - transform.position).normalized;

direction = direction speed Time.deltaTime;

So its pretty simple, first I find the Player, then I calculate the vector between the Player and the Enemy that is shooting, and then I normalize it, so that I can give it the desired speed.

The Problem is, sometimes the bullet is really really slow, and sometimes its normal.

I added Debug.Log(direction) and it told me numbers like this: (-0.2, 0) (0.4, 0) (-0.2, 0) (0.2, 0) (0, 0) <- yes the bullet had no speed at all o.O (-0.2, 0.1) (0, 0,2)

These numbers dont make sense to me. Shouldnt the x and the y add up to a specific value?

If there is something that I should show tell me I hope this isnt too long or to easy :P

Thanks for helping me in advance

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
Best Answer

Answer by Blue-Mirror · Aug 28, 2016 at 01:58 PM

using UnityEngine; using System.Collections;

public class BasicShot : RaycastController {

 private Vector3 oldPosition;        //The Position we had last frame
 private Vector3 targetPlace;        //The gameObjects Transform to chase (will be reseted every frame, so that the bullet keeps on flying to the same direction)
 private int aliveSince = 0;         //Counts up every frame

 private float speed;                //The speed this gameObject has
 private int lifeSpann;              //The time this gameObject will be alive for

 public void SetStatsAndStart(float Speed, int LifeSpann, string thisName)
 {
     aliveSince = 0;
     this.name = thisName;   //The Name this gameObject will have. This is be needed for the GameMaster
     speed = Speed;
     lifeSpann = LifeSpann;

     StartTheMovement();
 }

 //Starts the chasing of the Player
 public void StartTheMovement()
 {
     targetPlace = GameObject.FindGameObjectWithTag("Player").transform.position;

     StartCoroutine(TowardsPlayer());
 }


 IEnumerator TowardsPlayer()
 {
     while (aliveSince < lifeSpann)
     {
         aliveSince++;

         oldPosition = transform.position;

         transform.position = Vector3.MoveTowards(transform.position, targetPlace, speed * Time.deltaTime);

         targetPlace = targetPlace + (transform.position - oldPosition);

         yield return 0;
     }
     Destroy(gameObject);
 }

}

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
avatar image
1

Answer by Ali-hatem · Aug 28, 2016 at 11:54 AM

because target.position - transform.position will not be the same depending of the distance between the transform & the target which will be different each time they move . so rather than that why don't you use Vector3.MoveTowards so the shoots move always to the target with the same speed.

Comment
Add comment · Show 9 · 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 Blue-Mirror · Aug 28, 2016 at 12:12 PM 0
Share

$$anonymous$$y problem is, that the bullet then only flies to where the Player was, but I want it to go further. But I guess I could somehow make the Vector bigger, so that the bullet flies to that point :/ I will try. Thanks :)

avatar image Ali-hatem Blue-Mirror · Aug 28, 2016 at 12:28 PM 0
Share

you mean only the old position & not the new positions of the player if so the Vector3.$$anonymous$$oveTowardswill work fine . so let me know if every thing is fine or not good luck.

avatar image Blue-Mirror Ali-hatem · Aug 28, 2016 at 01:06 PM 0
Share

It almost does. What Im doing is, I save the position of the player and then use this:

transform.position = Vector3.$$anonymous$$oveTowards(transform.position, targetPlaceOld, speed * Time.deltaTime);

But he does only go to the position (obviously he does thats what I tell him to do).

Show more comments
avatar image Blue-Mirror · Aug 28, 2016 at 01:43 PM 0
Share

I cant find out why it doesnt work XD

The picture is what I have for now. The AI doesnt hit me, it always shoots somewhere else. If I take out these lines:

         targetPlace = targetPlace + (transform.position - oldPosition); 

         oldPosition = transform.position;

It shoots perfectly at me, but the bullets stay at the point where I was.alt text

ice-screenshot-20160828-153946.png (38.4 kB)
avatar image Blue-Mirror Blue-Mirror · Aug 28, 2016 at 01:56 PM 0
Share

$$anonymous$$ Im stupid I fixed it (obviously have to set oldPosition before I calculate with it ... )

avatar image Blue-Mirror Blue-Mirror · Aug 28, 2016 at 01:57 PM 0
Share

One last thing though the GameObject doesnt get destroyed for some reason do you know why? I will post my whole class as "your answear"

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do i make an ai shoot at player? 2D 0 Answers

Instantiating object at wrong location, Unity bug? 0 Answers

Raycast Enemy AI shooting script 1 Answer

Interesting Enemy AI issue 2 Answers

Enemy AI for Shooting Game 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