Heres my submarineAi script, and 2 questions:
Im trying to get the sub to come within a set distance of the Player, and the shoot. I am trying to fiwrite a “HoldPosition” function, because I want to slow the sub’s motion down which is based on force… I guess thats quite dependent on how Im calling Both the Move Towards and HoldPosition functions also, because I dont want to be calling MoveTowards and Hold position at the same time I dont think…either one or the other…I guess even opinions there would be helpful right now…
I figure ForceMode.VelocityChange is best to slow the objects speed down, but I dont know to get it into code. So the Ai subs’ existence is to spawn, Find enemy and move towards, get to a set distance from the player, and start shooting…
Thanks for any help!
var Speed = 3.0;
var rotationSpeed = 5.0;
var shootRange = 15.0;
var attackRange = 30.0;
var difference = 4.0;
var shootAngle = 4.0;
var dontComeCloserRange = 5.0;
var delayShootTime = 0.35;
var target : Transform;
private var lastShot = -10.0;
function Update(){
if (target == null GameObject.FindWithTag("Player")) {
target = GameObject.FindWithTag("Player").transform;
}
var targetDirection = target.transform.position - transform.position;
var forward = transform.TransformDirection(Vector3.forward);
var angle = Vector3.Angle(targetDirection, forward);
var distance = Vector3.Distance(transform.position, target.position);
var targetRotation = Quaternion.LookRotation(targetDirection.normalized);
var str = Mathf.Min(rotationSpeed * Time.deltaTime, 1);
transform.rotation = Quaternion.Lerp (transform.rotation, targetRotation, str);
if (distance < shootRange angle < shootAngle)
StartCoroutine("Shoot");
if (distance > shootRange angle < shootAngle)
MoveTowards();
}
function Shoot ()
{
// Fire gun
BroadcastMessage("Fire");
// Wait for the rest of the animation to finish
yield WaitForSeconds(delayShootTime);
}
function RotateTowards (position : Vector3)
{
var direction = position - transform.position;
direction.y = 0;
if (direction.magnitude < 0.1)
return;
// Rotate towards the target
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);
}
function MoveTowards ()
{
rigidbody.AddForce (0, 0, (Speed*10));
if (distance < shootRange angle < shootAngle)
HoldPosition();
}
function HoldPosition(){
rigidbody.AddForce
//In here I am not sure how to apply thr force to slow down the object...
//?ForceMode.VelocityChange?//
);
}
Thanks
AC