I have a zombie that i am trying to have come at my first person controller. i am using a smooth look at script-> var target : Transform;
var damping = 6.0;
var smooth = true;
function LateUpdate () {
if (target) {
if (smooth)
{
// Look at and dampen the rotation
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}
else
{
// Just lookat
transform.LookAt(target);
}
}
}
function Start () {
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
Then adding a sphere collider and giving him a constant force on the Z axis but he seems to keep falling his side then coming at me. Anyone know how to keep him standing?
any help at all will be much appreciated
At the end of your script put eulerAngles.x = 0; This should stop the piviting and wierd rotation issues you are having. If you want some simple AI for the ZOmbie to attack your player and always chase you you could do this. NOTE all you need to do is look in the inspector pull your player onto target set the raylength, the turnspeed, speed and your ready just have a play with it. If you do not have any animations for your zombie just delete the animation.CrossFade parts. Also if you want them to constantly come at you you need to find out the size of your level/ terrain. So if it’s 500x500 Make your attack range 500 or what ever the highest value. If this has helped you then please click the thumbs up. Need my score up;)
var target : Transform;
var zombieController : CharacterController;
var speed : float = 20.5;
var backwardsSpeed : float =-20.5;
var rayLength : float;
var lookAtPlayerTime : float;
var turnSpeed : float;
var hit2 : RaycastHit;
var distance : float;
var rangeToAttack : float;
var beenHit : boolean = false;
function Start()
{
zombieController = GetComponent("CharacterController");
}
function Update()
{
transform.eulerAngles.x = 0;
distance = Vector3.Distance(transform.position, target.transform.position);
if(distance <= rangeToAttack)
{
animation["attack"].speed = 0.4;
animation.CrossFade("attack");
}
else
{
animation.CrossFade("walk");
}
var dir = transform.TransformDirection(Vector3.forward);
var dir2 = transform.TransformDirection(Vector3.right );
var hit : RaycastHit;
Debug.DrawRay(transform.position, dir * rayLength, Color.black);
Debug.DrawRay(transform.position, dir2 * rayLength, Color.black);
if(Physics.Raycast(transform.position, dir, hit, rayLength))
{
if(hit.collider.gameObject.tag == "Prop")
{
transform.Rotate(0,30 * Time.deltaTime,0);
var forward1 : Vector3 = transform.TransformDirection(Vector3.forward);
var curSpeed1 : float = speed * Time.deltaTime;
zombieController.SimpleMove(forward1 * curSpeed1);
}
if(hit.collider.gameObject.tag == "Player")
{
var forward2 : Vector3 = transform.TransformDirection(Vector3.forward);
var curSpeed2 : float = speed * Time.deltaTime;
zombieController.SimpleMove(forward2 * curSpeed2);
transform.eulerAngles.x = 0;
}
}
else
{
var forward : Vector3 = transform.TransformDirection(Vector3.forward);
var curSpeed : float = speed * Time.deltaTime;
zombieController.SimpleMove(forward * curSpeed);
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * turnSpeed);
transform.eulerAngles.x = 0;
}
}