Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 MagMince · Mar 15, 2015 at 07:06 PM · enemy ai

Enemy shooting at spawn point not player

The enemy that spawn into my game from the Prefab folder does not shoot at the player, but where the player spawns.

//enemy shooting script

 var projectile: Rigidbody;
  
 var speed = 25;
  
 var player : Transform;
  
  
  
 
 function Start() {
     var rendum = Random.Range(1F,3F);
     InvokeRepeating("Shuut", 2, rendum);
 }
  
  
  
 function Update() {
     transform.LookAt(player);
 }
  
 function Shuut () {
  
  
 clone = Instantiate(projectile, transform.position, transform.rotation);
 clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
  
 Destroy (clone.gameObject, 50);
 }



//enemy movement script

     var startMarker: Transform;
     var endMarker: Transform;
     
     // Movement speed in units/sec.
     var speed = 10.0;
     
     // Time when the movement started.
     private var startTime: float;
     
     // Total distance between the markers.
     private var journeyLength: float;
     
     var target : Transform;
     var smooth = 5.0;
     
     function Start() {
         // Keep a note of the time the movement started.
         startTime = Time.time;
         
         // Calculate the journey length.
         journeyLength = Vector3.Distance(startMarker.position, endMarker.position);
     }
     
     // Follows the target position like with a spring
     function Update () {
         // Distance moved = time * speed.
         var distCovered = (Time.time - startTime) * speed;
         
         // Fraction of journey completed = current distance divided by total distance.
         var fracJourney = distCovered / journeyLength;
         
 
         transform.position=Vector3.Lerp(startMarker.position, endMarker.position, Mathf.PingPong(Time.time*speed, 1.0f));
     }





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
1

Answer by Socrates · Mar 15, 2015 at 08:20 PM

Looking at your shooting script, you are turning your enemy toward the "player" target. Have you confirmed that you put the actual player transform into that public variable and not the player's spawn point?

For further testing, use the Project View to watch the enemy after you have moved the player away from the starting position. Is the enemy actually rotating to face the player position as that enemy moves between the two ends of its movement path?

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 MagMince · Mar 15, 2015 at 09:54 PM 0
Share

Yes I have put the player in the variable but the player is a gameobject not transform, could that be affecting the enemy. The enemy is facing the player spawn point as it moves just not at the player.

avatar image Socrates · Mar 15, 2015 at 10:18 PM 0
Share

When you drop a GameObject into a Transform public variable in the Inspector, it automatically gets the Transform component. So, that part should be fine.

However, if the enemy is shooting at the player's spawn location and continually rotating to face that location, it still sounds like the "player" variable is not set correctly. How are you assigning that variable? If it is assigned in the Inspector, try removing what you currently have set and then make sure you assign the same GameObject that is being moved by whatever script is moving your player.

Also make sure you are setting that GameObject in the enemy shooting script as "player" and not in the enemy movement script as "target". You don't seem to use "target" anywhere in the enemy movement script.

If trying to set the "player" object from scratch does not solve it, perhaps posting a screenshot or link to one showing your script settings here would be provide more information.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Enemys jittering when moving 1 Answer

Enemy turn AI 2 Answers

Enemy behavior help 1 Answer

2D Top Down 4-directional RPG: Enemy Animation / Follow (beginner) 2 Answers

Spaceship AI for 3d 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