- Home /
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;
}
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;
}
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;
}
}
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;
}
hey thank you so much and next noob question how do i set your comment as the answer?
i am happy to hear that :) and watch this http://video.unity3d.com/video/7720450/tutorials-using-unity-answers
Answer by dallasxiong · Dec 31, 2013 at 08:16 AM
add a rigidbody to the zombie.
Try using
rigidbody.$$anonymous$$ovePosition(rigidbody.position + myTransform.forward * moveSpeed * Time.deltaTime)
ins$$anonymous$$d.
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;
}
}
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);
}
}
Your answer
Follow this Question
Related Questions
Stopping AI's animation and movement 0 Answers
Enemy walking through walls/terrain 1 Answer
Moving Enemies? 1 Answer