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 KingSloth · Jun 23, 2017 at 08:16 PM · c#aienemyshootingenemyai

[I REALLY NEED HELP FAST]Help with enemy Shooting

Hello! I just need help with my Enemies. The bullet only facing a certain direction. I made an empty game object, and added the script to it. I put the game object as a child to the enemy. No matter how much I rotate the Fire Point, the bullet won't move. Any Help is appreciated. Here is my script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class AIBullet : MonoBehaviour {
     
     public GameObject referenceToBulletPrefab;
     public float timeToWaitBetweenShots;
 
     void Start(){
         InvokeRepeating("ShootBullet", timeToWaitBetweenShots, timeToWaitBetweenShots);
     }
     void ShootBullet(){
         Vector3 positionToShootFrom = transform.position + transform.forward;
 
         Instantiate(referenceToBulletPrefab, positionToShootFrom, Quaternion.Euler(Vector3.zero));
         }
 
     
     // Update is called once per frame
     void Update () {
         
     }
 }

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 Nomenokes · Jun 23, 2017 at 08:46 PM 0
Share

Which thing are you changing? The positionToShootFrom? The bullet spawn is entirely based on that, so if you don't change that, it will always emerge from in front.

Also, what's your bullet movement script? Or are you not focusing on that right now?

avatar image KingSloth Nomenokes · Jun 23, 2017 at 08:50 PM 0
Share

No I am rotating the Game object

avatar image Vollmondum · Jul 02, 2017 at 06:12 AM 0
Share

Remove animator. Animations prevent movement

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by cstooch · Jun 23, 2017 at 09:01 PM

It's hard to tell because your code is pasted so weird here, but... I don't see anything here where you're actually moving the bullet (are you doing it in your bullet script?).

You'll want a rigidbody (not kinematic) on your bullet then apply some force to move it. Ex. set Rigidbody.velocity (not in update), or Rigidbody.AddForce to name two ways.

On my bullet on current project, I use Rigidbody.velocity... https://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html

You either set this in the Awake or Start (for example) of your bullet's script (not the script you're instantiating the bullet with), or you can set it right in the script where you instantiate, just after you instantiate. Instantiate function returns a reference to the gameobject's reference, so you can return into a variable and then set the velocity on that.

ex.

 bullet = Instantiate(yadayada);
 bullet.GetComponent<RigidBody>().velocity = transform.forward*10.0f;

Edit: here's a good example of using rigidbody, actually. It's a multiplayer tutorial, but ignore that and skip to the part on firing a bullet. It short and sweet: https://unity3d.com/learn/tutorials/temas/multiplayer-networking/shooting-single-player

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 Nomenokes · Jun 23, 2017 at 09:09 PM 0
Share

Agreed, this is the only problem I see. Sloth's "forward" instantiation idea works fine.

@$$anonymous$$ingSloth You could also have a simple translation script transform.position += transform.forward * speed * Time.deltaTime; ins$$anonymous$$d of dealing with rigidbodies. Up to you.

avatar image cstooch Nomenokes · Jun 23, 2017 at 09:17 PM 0
Share

That will work, but I think the only issue with that is then you're moving a static collider around, which apparently is less efficient, according to this: https://unity3d.com/learn/tutorials/topics/physics/physics-best-practices?playlist=17120

(jump to the Rigidbody section)

And moving a rigidbody without physics I believe can cause issues with the collider.

avatar image cstooch cstooch · Jun 23, 2017 at 09:21 PM 0
Share

Actually I Googled this up, and I guess in Unity 5+ it isn't an issue to move rigidbody without physics anymore by the sounds of it. edit: O$$anonymous$$.. maybe.. conflicting reports out there. LOL. I just do it the way I'm familiar with. Rigidbody + physics for bullets.

Show more comments
avatar image cstooch Nomenokes · Jun 23, 2017 at 09:38 PM 0
Share

Yeah, honestly, don't quote me on any of that. lol I'm still learning, myself. And it doesn't help that some of the info you find on the net is dated!

$$anonymous$$oving an object without a rigidbody by simply moving it's position is most likely more efficient than moving a rigidbody with physics. But I thought I'd read somewhere that moving an object with a collider without a rigidbody on it could cause issues with collision detection. i very well could be wrong, or that could be dated info.

If it works, it works though, and I would just ignore my ramblings on this! :)

avatar image Nomenokes · Jun 23, 2017 at 09:16 PM 0
Share

bullet.GetComponent().velocity = bullet.transform.forward * 10.0f;

You're rusty :P

avatar image Nomenokes Nomenokes · Jun 23, 2017 at 09:17 PM 0
Share

ah it's just a kink in the forum's dealing with <>. You were right, never $$anonymous$$d

avatar image cstooch Nomenokes · Jun 23, 2017 at 09:18 PM 0
Share

Yeah, I edited it.. stupid thing :) Has to go in code tags.

Show more comments

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

353 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 AI, Aim at player - even when jumping? 1 Answer

Send an enemy back to its spawn point using waypoints 2 Answers

Enemy/Partner AI without rotation 1 Answer

How do I stop my enemies from rapidfiring? 1 Answer

get enemy to chase and shoot at player wherever hes facing. 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