- Home /
Prefab problem...
I made my self a prefab character that is animated and is spawning from a array. Problem comes in when I start the game, the character mesh rotates forward 90 degrees.
The AI code:
var speed = 5;
var deer = 10;
var tap = 100;
var rotate;
var target : Transform;
var other : Transform;
//Movement Script
function Update () {
var dir = (target.position - transform.position).normalized;
var hit : RaycastHit;
if(Physics.Raycast(transform.position, transform.forward, hit, 15)){
if(hit.transform != transform){
dir += hit.normal * 50;
}
}
var leftR = transform.position;
var rightR = transform.position;
leftR.x -= 2;
rightR.x += 2;
if(Physics.Raycast(leftR, transform.forward, hit, 8)){
if(hit.transform != transform){
dir += hit.normal * 50;
}
}
if(Physics.Raycast(rightR, transform.forward, hit, 8)){
if(hit.transform != transform){
dir += hit.normal * 50;
}
}
var rot = Quaternion.LookRotation(dir);
transform.rotation = Quaternion.Slerp(transform.rotation, rot, Time.deltaTime);
transform.position += transform.forward * speed * Time.deltaTime;
//Destroy Object at Destination
if (other) {
var dist = Vector3.Distance(other.position, transform.position);
if(dist <= 5){
Destroy(this.gameObject);
}
}
}
//Mouse Events
function OnMouseEnter() {
speed = 15;
LevelController.Points += deer + 10;
}
function OnMouseExit () {
speed = 5;
}
function OnMouseDown () {
LevelController.Points += tap + 100;
ChangeDirection();
}
//Random Direction Script
function ChangeDirection() {
rand = Random.Range(2,-2);
switch(rand){
case -2:
rotate = -135;
break;
case -1:
rotate = -45;
break;
case 1:
rotate = 45;
break;
case 2:
rotate = 135;
break;
default:
rotate = 0;
break;
}
if(rotate!=0){
transform.Rotate(Vector3.up, rotate);
}
}
Comment