Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Need help, enemy ai's animation show run-time error and doesnt chase player

$
0
0
Please read and help. I'm still newbie in this, and I dont know how to modelling yet, so I asked my friend to create a snake with animation in it. Then he created, but it just the chase animation. Since I need the idle animation too, I told him and then he created it. He didnt give me the previous snake object with chase animation in it, yet he created new object (but still same texture and model) with idle animation. I thought it could works, then I tried and imported both of it into my game. So it is actually 2 same objects, which having different animation in each object. At first, I thought I needed to use the animator for it to work with the script. But then I realized, i cant use animator, animator used just only for an object with some animations in it then we choose the condition to make transition between the animations. Am I wrong? Then I decide to call it from the script using animationclip function or whatever its called. I applied rigidbody and charactercontroller to the object, and the animations works fine and the enemy chase the player as the script instructed. But when I played, it keep showing run-time errors, it says "There is no 'Animation' attached to the "Snake" game object, but a script is trying to access it." and blabla. After that, I removed the rigidbody since I think it is useless to use it. But then the problems occurs. The snake moves with chase animation in it, without idling, then it doesnt want to chase the player. I tried many times to reimport the object from the asset to the scene, but still the same. Here are the lines I used. ---------- var Distance; var Target : Transform; var lookAtDistance = 25.0; var chaseRange = 15.0; var attackRange = 1.5; var moveSpeed = 5.0; var Damping = 6.0; var attackRepeatTime = 1; var TheDamage = 40; private var attackTime : float; var chaseAnim : AnimationClip; var idleAnim : AnimationClip; var controller : CharacterController; var gravity : float = 20.0; private var MoveDirection : Vector3 = Vector3.zero; function Start() { attackTime = Time.time; } function Update () { var playerStatus : Component = GetComponent(PlayerStats); { if (playerStatus.alive == true) { Distance = Vector3.Distance(Target.position, transform.position); if (Distance < lookAtDistance) { lookAt(); } if (Distance > lookAtDistance) { animation.Play(idleAnim.name); } if (Distance < attackRange) { attack (); } else if (Distance < chaseRange) { chase (); } } } } function lookAt () { animation.Play(idleAnim.name); var rotation = Quaternion.LookRotation(Target.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping); } function chase () { animation.Play(chaseAnim.name); moveDirection = transform.forward; moveDirection *= moveSpeed; moveDirection.y -= gravity * Time.deltaTime; controller.Move(moveDirection * Time.deltaTime); } function attack () { if (Time.time > attackTime) { Target.SendMessage("ApplyDamage", TheDamage); Debug.Log("The enemy has attacked"); attackTime = Time.time + attackRepeatTime; } } function AfterHit () { chaseRange += 30; moveSpeed += 2; lookAtDistance += 40; } ---------- Should I create new snake object with 2 animations within attached to it? Or there is a way to make it works with these objects that I have now? Please help me. :(

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>