So I've managed to modify some code that I have found for ray casting so that my enemy moves towards the co-ordinates of my first person camera. However, I need to change the way the ray casting moves the enemy around the obstacles in my scene. I'm getting a little confused from the myTransform
/transform
/target
variables.
So this is my code;
var speed : float = 10.0;
private var dir : Vector3;
private var dirFull : Vector3;
var target : Transform;
var rotationSpeed = 3;
var myTransform : Transform;
function Awake()
{
myTransform = transform;
}
function Start()
{
target = GameObject.FindGameObjectWithTag("Player").transform; //Target the player
}
function FixedUpdate()
{
// the directional vector to the target
dir = (target - transform.position).normalized;
var hit : RaycastHit;
// more raycasts
var leftRay = transform.position + Vector3(-0.125, 0, 0);
var rightRay = transform.position + Vector3(0.125, 0, 0);
// check for forward raycast
if (Physics.Raycast(transform.position, transform.forward, hit, 5)) /
{
if (hit.transform != this.transform)
{
Debug.DrawLine (transform.position, hit.point, Color.white);
dir += hit.normal * 20; // 20 is force to repel by
}
}
// check for leftRay raycast
if (Physics.Raycast(leftRay, transform.forward, hit, 5))
{
if (hit.transform != this.transform)
{
Debug.DrawLine (leftRay, hit.point, Color.red);
dir += hit.normal * 20; // 20 is force to repel by
}
}
// check for rightRay raycast
if (Physics.Raycast(rightRay, transform.forward, hit, 5))
{
if (hit.transform != this.transform)
{
Debug.DrawLine (rightRay, hit.point, Color.green);
dir += hit.normal * 20; // 20 is force to repel by
}
}
// rotation
var rot = Quaternion.LookRotation (dir);
transform.rotation = Quaternion.Slerp (transform.rotation, rot, Time.deltaTime);
//position
transform.position += transform.forward * (2 * Time.deltaTime); // 20 is speed
}
I'm almost certain the only think I need to change is the dir
variable so that it uses the new transform code. But as I say, I've been getting confused with which variable to use.
EDIT: My understanding is that I need change the code at the bottom of the update so that it takes into account rotation that occurs when an obstacle is met via the ray casts. Which is why I think it's just the "dir" and "rot" that needs changing and then used in the position and rotation code.