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
1
Question by colsonenderby · Jul 16, 2011 at 06:23 AM · collisioncolliderai

I need help with my enemy collision.

My player dies when it collides with enemys but when my player sits still he takes no damage from enemys colliding with the player, here is my damage and ai script

DAMAGE SCRIPT.js

var Damage = 10;

function OnTriggerEnter (col : Collider) { var player : FPSPlayer = col.GetComponent(FPSPlayer);

 if (player) {
     player.ApplyDamage(Damage);
 } else if (col.rigidbody) {    
     //Destroy(col.rigidbody.gameObject);
 } else {
     //Destroy(col.gameObject);
 }

}

function Reset () { if (collider == null)
gameObject.AddComponent(BoxCollider); collider.isTrigger = true; }

AI Script.cs

using UnityEngine; using System.Collections;

public class EnemyAI : MonoBehaviour { public Transform target; public int moveSpeed; public int rotationSpeed; public int maxDistance;

 private Transform myTransform;
 
 void Awake() {
     myTransform = transform;
 }

 // Use this for initialization
 void Start () {
     GameObject go = GameObject.FindGameObjectWithTag("Player");
     
     target = go.transform;
     
     maxDistance = 1;
 }
 
 // Update is called once per frame
 void Update () {

// Debug.DrawLine(target.position, myTransform.position, Color.yellow);

     //look at target
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
 
     if(Vector3.Distance(target.position, myTransform.position) > maxDistance) {
         //move towards target
         myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
     }
 }}
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 The_r0nin · Jul 16, 2011 at 01:13 PM

Do both have Kinematic Rigidbodies? If not (from http://unity3d.com/support/documentation/Components/RigidbodySleeping.html):

When Rigidbodies fall to rest - a box landing on the floor - they will start sleeping. Sleeping is an optimization which allows the Physics Engine to stop processing those rigidbodies. This way you can have huge amounts of rigidbodies in your scene as long as you make sure that they normally don't move.

Rigidbody sleeping happens completely automatically. Whenever a rigidbody is slower than the sleepAngularVelocity and sleepVelocity it will start falling asleep. After a few frames of resting it will then be set to sleep. When the body is sleeping, no collision detection or simulation will be performed anymore. This saves a lot of CPU cycles.

...

Kinematic rigidbodies wake up sleeping rigidbodies. Static Colliders do not. If you have a sleeping rigidbody and you move a static collider (A collider without a Rigidbody attached) into the rigidbody or pull it from underneath the rigidbody, the sleeping rigidbody will not awake.

My best guess:

So, since you are moving the enemies by transform rather than by rigidbody, I would assume you have just a collider on them and not rigidbodies. When the character is moving, its rigidbody is awake for collisions. When he is still, his rigidbody falls asleep and no longer detects collisions from non-rigidbodies...

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

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

Audio play when object hit collision. 2 Answers

hide child object script - help 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Collider Error[SOLVED] 2 Answers

Cover mechanic - Restrict movement to box 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