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 zombieninjajack · Dec 31, 2013 at 02:48 AM · enemymodelswalking

Enemy Model Floating in the Air

ok so basically i have no clue why this is happening but if someone could help me out that would be the best thing in the world. so everything else in my code works fine except the fact when the zombie starts to chase me it starts walking on air or floating. so heres my code

 var target : Transform; //the enemy's target
 var moveSpeed = 3; //move speed
 var rotationSpeed = 3; //speed of turning
 
 var myTransform : Transform; //current transform data of this enemy
 var isNotDead : boolean = true;
 var health : float = 100;
 function Awake()
 {
     myTransform = transform; //cache transform data for easy access/preformance
 }
 
 function Start()
 {
      target = GameObject.FindWithTag("Player").transform; //target the player
 
 }
 
 function Update () {
     
     if(health < 1){
     
         isNotDead = false;
         animation.Play("die");
         Destroy(gameObject, 1);
     }
     
     if(isNotDead){
     
         //rotate to look at the player
         myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
         Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
     
     
         
         var distance = Vector3.Distance(target.position, myTransform.position);
         if (distance < 3.0f) {
             animation.Play("attack1");
         }
         else{   
             //move towards the player
                myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
                animation.Play("walk1");
         }
 
     }
 }
 
 function ApplyDamage(dmg : float){
 
     health -= dmg;
 
 }

  
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by seckincengiz · Jan 07, 2014 at 01:46 PM

Calculate the direction, then zero the y component to make the direction vector completely horizontal. And try freeze rotation X and Z axis in your rigidbody attribute settings. This way zombie don't fly :)

Heres sample js code:

 var target : Transform;
 var myTransform : Transform;
 var moveSpeed = 3;
 var rotationSpeed = 3;
 
 function Awake(){
 myTransform = transform;
 }
 
 function Start(){
 target = GameObject.FindWithTag("Player").transform;
 }
 
 function Update () {
     var lookDirection = target.position - myTransform.position;
     lookDirection.y = 0;
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(lookDirection), rotationSpeed*Time.deltaTime);
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
 }
Comment
Add comment · Show 4 · 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 zombieninjajack · Jan 07, 2014 at 07:04 PM 0
Share

What would i do if i were using this script insted but having the same problem var TheDamage = 25;

 private var attackTime : float;
  
 var controller : CharacterController;
 var gravity : float = 20;
 private var $$anonymous$$oveDirection : Vector3 = Vector3.zero;
  
 function Start ()
 {
     attackTime = Time.time;
 }
  
 function Update ()
 {
     Distance = Vector3.Distance(Target.position, transform.position);
  
     if (Distance < lookAtDistance)
     {
        lookAt();
     }
  
     if (Distance > lookAtDistance)
     {
        renderer.material.color = Color.green;
     }
     if (Distance < attackRange)
     {
        attack ();
     }
     else if (Distance < chaseRange)
     {
        chase ();
     }
 }
  
 function lookAt ()
 {
     renderer.material.color = Color.yellow;
     var rotation = Quaternion.LookRotation(Target.position - transform.position);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
 }
  
 function chase ()
 {
     renderer.material.color = Color.red;
  
     moveDirection = transform.forward;
     moveDirection *= moveSpeed;
  
     moveDirection.y -= gravity * Time.deltaTime;
     controller.$$anonymous$$ove(moveDirection * Time.deltaTime);
 }
 function attack ()
 {
     if (Time.time > attackTime)
     {
        Target.Send$$anonymous$$essage("ApplyDamage", TheDamage, Send$$anonymous$$essageOptions.DontRequireReceiver);
        Debug.Log("EnemeyAttacked");
        attackTime = Time.time + attackRepeatTime;
     }  
 }
avatar image seckincengiz · Jan 08, 2014 at 05:01 AM 0
Share

Replace lookAt and chase function in your code like below and don't use rigidbody. And try adding "charachter motor" script component to your zombie. You can find it in the first person controller prefab.

 function lookAt ()
     {
     var lookPos = Target.position - transform.position;
     lookPos.y = 0;
     var rotation = Quaternion.LookRotation(lookPos);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);;
     }
      
 function chase ()
     {
     //renderer.material.color = Color.red; 
     var lookPos = Target.position - transform.position;
     lookPos.y = 0;
     var rotation = Quaternion.LookRotation(lookPos);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);;
     transform.position += transform.forward * moveSpeed * Time.deltaTime; 
     }
avatar image zombieninjajack · Jan 08, 2014 at 05:11 AM 0
Share

hey thank you so much and next noob question how do i set your comment as the answer?

avatar image seckincengiz · Jan 08, 2014 at 05:13 AM 0
Share

i am happy to hear that :) and watch this http://video.unity3d.com/video/7720450/tutorials-using-unity-answers

avatar image
1

Answer by dallasxiong · Dec 31, 2013 at 08:16 AM

add a rigidbody to the zombie.

Comment
Add comment · Show 3 · 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 zombieninjajack · Jan 06, 2014 at 04:56 AM 0
Share

that doesnt fix it

avatar image Paparakas · Jan 06, 2014 at 05:08 AM 0
Share

Try using

rigidbody.$$anonymous$$ovePosition(rigidbody.position + myTransform.forward * moveSpeed * Time.deltaTime)

ins$$anonymous$$d.

avatar image zombieninjajack · Jan 06, 2014 at 05:46 AM 0
Share

im using a new AI code but i still have the same problem and not sure where to put that line you gave me var Distance; var Target : Transform; var lookAtDistance = 25.0; var chaseRange = 15; var attackRange = 1.5; var moveSpeed = 5.0; var Damping = 6.0; var attackRepeatTime = 1;

 var TheDamage = 25;
 
 private var attackTime : float;
 
 var controller : CharacterController;
 var gravity : float = 20;
 private var $$anonymous$$oveDirection : Vector3 = Vector3.zero;
 
 function Start ()
 {
     attackTime = Time.time;
 }
 
 function Update ()
 {
     Distance = Vector3.Distance(Target.position, transform.position);
     
     if (Distance < lookAtDistance)
     {
         lookAt();
     }
     
     if (Distance > lookAtDistance)
     {
         renderer.material.color = Color.green;
     }
     if (Distance < attackRange)
     {
         attack ();
     }
     else if (Distance < chaseRange)
     {
         chase ();
     }
 }
 
 function lookAt ()
 {
     renderer.material.color = Color.yellow;
     var rotation = Quaternion.LookRotation(Target.position - transform.position);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
 }
 
 function chase ()
 {
     renderer.material.color = Color.red;
     
     moveDirection = transform.forward;
     moveDirection *= moveSpeed;
     
     moveDirection.y -= gravity * Time.deltaTime;
     controller.$$anonymous$$ove(moveDirection * Time.deltaTime);
 }
 function attack ()
 {
     if (Time.time > attackTime)
     {
         Target.Send$$anonymous$$essage("ApplyDamage", TheDamage, Send$$anonymous$$essageOptions.DontRequireReceiver);
         Debug.Log("EnemeyAttacked");
         attackTime = Time.time + attackRepeatTime;
     }    
 }
 
avatar image
0

Answer by Srinivas141290 · Jan 08, 2014 at 06:31 AM

using UnityEngine; using System.Collections;

public class EnemyAI : MonoBehaviour {

 CharacterController controller;
 Transform player;
 [SerializeField]
 float moveSpeed = 4.0f;
 [SerializeField]
 float gravity =2.0f;
 float yvelocity = 0.0f;

 // Use this for initialization
 void Start () 
 {
     GameObject playerGameobject = GameObject.FindGameObjectWithTag ("Player");
     player = playerGameobject.transform;
     controller = GetComponent<CharacterController> ();

 }
 
 // Update is called once per frame
 void Update () 
 {
     Vector3 direction = player.position - transform.position;
     direction.Normalize();
     Vector3 velocity = direction * moveSpeed;

     if (!controller.isGrounded) {
                     yvelocity -= gravity;
             }
     velocity.y = yvelocity;
     direction.y = 0;

     transform.rotation = Quaternion.LookRotation (direction);
     controller.Move (velocity * Time.deltaTime);
 
 }

}

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

22 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

Related Questions

Stopping AI's animation and movement 0 Answers

How do I make it so that a walking animation plays for the enemy AT THE SAME TIME it is moving toward the player? 0 Answers

Enemy walking through walls/terrain 1 Answer

Can someone plz tell me we're the old enemy models are like the ones in chromefxffilms first game vid. 0 Answers

Moving Enemies? 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