- Home /
AI Helicopter type Movement
I'm trying to get an AI aircraft to tilt in the direction it is moving and face the player. However I'm running into two problems I can't seem to solve. The first one is that the if statements aren't checking the increments correctly, so sometimes it will tilt left but will also tilt left if it's moving right. I'm not sure how to check for the direction it is moving in otherwise. The other problem is when I add in the lookAt function the rotations for tilt don't work at all. I also don't like the way it instantly moves to always look at the player, it should move slowly.
This is for a top down scrolling game where the environment moves beneath to give the illusion of speed.
#pragma strict
var player : Transform;
var distance : float;
var rotationLeft = Quaternion.Euler(0, 0, -30);
var rotationRight = Quaternion.Euler(0, 0, 30);
var rotationForward = Quaternion.Euler(-30, 0, 0);
var rotationBackward = Quaternion.Euler(30, 0, 0);
var rotationCenter = Quaternion.Euler(0, 0, 0);
var moveRightLeft : float;
var moveForwardBack : float;
function Update () {
distance = Vector3.Distance(transform.position, player.position);
moveRightLeft = transform.position.x;
moveForwardBack = transform.position.z;
if((player != null) && (distance >= 45)){
transform.position = Vector3.Lerp(transform.position, player.position, Time.deltaTime * 0.4);
transform.rotation = Quaternion.Slerp(transform.rotation, rotationForward, Time.deltaTime * 4);
}
else{
transform.rotation = Quaternion.Slerp(transform.rotation, rotationCenter, Time.deltaTime * 4);
}
if((moveRightLeft ++) && (distance >= 45)){
transform.rotation = Quaternion.Slerp(transform.rotation, rotationLeft, Time.deltaTime * 4);
}
if((moveRightLeft --) && (distance >= 45)){
transform.rotation = Quaternion.Slerp(transform.rotation, rotationRight, Time.deltaTime * 4);
}
//var targetPosition : Vector3 = Vector3(player.position.x, player.position.y, 0);
//transform.LookAt(targetPosition);
}
Your code is incredibly strange.
moveRightLeft++ will actually increment that value, after you check (if it is zero ??)
You are a long ways from code that will work, but I suggest (to begin with) learn to write "break away" code which is simpler. So along the lines ..
if player is null .. return
so that's out of the way
next,
if distance > 45 ... do nothing, print a Debug.Log statements, and return
so you can see that case is out of the way
And so on .. carry on like that
Thanks. I need a lot more help though. I know my code is strange (: not sure what "break away" code means, but that null is to check whether the player has blown up, so it only moves when the player is alive. It also moves until it is 45 units away so that the enemy doesn't get right up on the player aircraft, it keeps some distance between the the two.
How do I check if a value increments or decrements and then "Do something" I've been commenting out code and checking if each works using Debug.Log
Your answer
Follow this Question
Related Questions
Rotation without tilt around a globe. 1 Answer
Bank Camera Based On Rotation Delta 1 Answer
Slight Problem in the Camera Rotation Script 0 Answers
Smooth tilt accelerometer 1 Answer