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 03gramat · Aug 27, 2012 at 12:58 PM · collisionaienemybounce

AI enemy bouncing with no collision

Hello, I have been hunting and have found a script that works in short, I would prefer if my enemy would avoid walls but I could easily work around this. My problem is that the enemy will not come close enough to my player to look like it has actually attacked me. It is almost like there is a cushion around my player but I have checked all colliders and they are no way near each other when the enemy pushed back:

 var enemy : Transform;
 var player: GameObject;
 var dir: Vector3;
 var speed: float;
 var observed: boolean;
 var sightDistance: int;
 
 function Update () {
     if(Vector3.Distance(enemy.position, transform.position)< sightDistance){
     
      transform.LookAt(enemy);
     
 if (observed){
 dir = player.transform.position - transform.position;
 dir = dir.normalized;
 
 transform.Translate(dir * speed, Space.World);
 }
 else {
     transform.eulerAngles.y = Mathf.PingPong(Time.time*20,90) -45;
     
 }
 }
 }
 
 function OnTriggerEnter(other:Collider) {
     if(player) observed =true;
 }
 
 function OnCollisionEnter(collision : Collision)
 
 {   
 
     if(collision.collider.gameObject.name == "arrow(Clone)")
 
     {
         Destroy(this);
     }
 
     
 
 }


Any help on the matter would be great!

Cheers, Matt

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

Answer by Griffo · Aug 27, 2012 at 01:22 PM

Here is a script to watch, follow and attack player, it might help you out.

 #pragma strict
 
     var distance : float = 20;
     var target : Transform;    
     var lookAtDistance : int = 30.0;
     var attackRange : int = 10.0;
     var moveSpeed : int = 3.0;
     var damping : int = 6.0;
     private var isItAttacking = false;
 
     function Update () 
     {
     distance = Vector3.Distance(target.position, transform.position);
 
     if(distance < lookAtDistance)
     {
     isItAttacking = false;
     lookAt ();
     }   
     if(distance < attackRange)
     {
     attack ();
     }
 }
 
 
 function lookAt ()
 {
 var rotation = Quaternion.LookRotation(target.position - transform.position);
 transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
 }
 
 function attack ()
 {
     isItAttacking = true;
     transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
 }
Comment
Add comment · Show 5 · 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 03gramat · Aug 27, 2012 at 03:48 PM 0
Share

I tried this but it had a material error. Thankyou for the response though! What I think I need is some sort of way to allow the enemy to come to collision with the player, if that is impossible with my script I will need to revamp the script completely, so if you can tell what I am doing wrong with your script I would appreciate it!

avatar image 03gramat · Aug 27, 2012 at 04:20 PM 0
Share

I tried this script but it didn't seem to do anything. I assume its because I have no renderer on my enemy. All I want is for the enemy to come to contact with the player, it already follows when in range.

Thanks for the quick reply though and if it is me that is making an error with your code, please explain as I am open to new ways of following if that will fix the problem!

avatar image Griffo 03gramat · Aug 27, 2012 at 05:04 PM 0
Share

I've edited my answer above, but a cube in the scene, put the script onto it and if you move towards it, it will chase you, you then can add a trigger to do stuff when it either collides with you or get within a distance you set ..

avatar image 03gramat 03gramat · Aug 27, 2012 at 05:22 PM 0
Share

Thankyou so much, its a much tidier script than my previous one too! you have been a great help!

avatar image Griffo 03gramat · Aug 27, 2012 at 06:27 PM 0
Share

Glad I could help .. Please mark it as answered .. Thanks.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Enemy Ai Problem with the collider and gravity 1 Answer

enemy AI avoiding collision 1 Answer

Precise hitboxes for characters 1 Answer

Enemy AI don't collide with objects or rotate. 0 Answers

Player pushback when collide with enemy 2 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