0

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.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
TJBaldy
  • 23
  • 1
  • 2
  • 6

2 Answers2

0

Most of the FixedUpdate() function is used to calculate your dir value. However, at the end there are comments that say that the code is updating the position, but they don't use the calculated dir value. I can't test this, but it seems that you need to change your last line to this:

transform.position += dir * (2 * Time.deltaTime);

As it currently stands, this code ignores any position of the enemy or the player:

transform.position += transform.forward * (2 * Time.deltaTime); // 20 is speed

It simply tries to move the enemy "forward," which is defined (in the Unity documentation) as "The blue axis of the transform in world space."

If you have code to rotate the enemy in the direction of dir, it seems like you need to move the enemy in that direction.

RustyTheBoyRobot
  • 5,891
  • 4
  • 36
  • 55
  • Don't think it's this. The movement/rotation code works fine. It's the code that changes the position of the enemy when he meets a obstacle that needs changing "dir += hit.normal * 20" – TJBaldy Mar 18 '13 at 17:30
  • Then where is `dir` being used? Your raycast code only modifies `dir`. It doesn't matter *what* code you use to set `dir`'s value if you never use `dir`. – RustyTheBoyRobot Mar 18 '13 at 17:33
  • Also, how can you say the "movement/rotation code works fine," if you're saying that the code to move around obstacles isn't working? – RustyTheBoyRobot Mar 18 '13 at 17:35
  • I just changed the code to what it was originally (before I started messing about with it). You can now see at the bottom of the Update function that dir is being used. What I mean by movement/rotation working is that the enemy does move towards the first person camera and rotates towards it if I might to the side of it. Now because I've changed the code back to it's original, it's broken because the transforms are conflicting. – TJBaldy Mar 18 '13 at 17:45
  • Just seen your edit, I know how to modify the position/rotation code when it doesn't take into account the rotation that is needed for the obstacles. I can get the enemy following me fine. This is where my problem occurs, I don't know how to modify the raycast code so thats it's taken into account when following me. – TJBaldy Mar 18 '13 at 18:16
0

All I had to change to get this working was the line of code;

dir = (target - transform.position).normalized;

to...

dir = target.position - myTransform.position; 

So I was right when I guessed that it was the dir variable that needed changing.

TJBaldy
  • 23
  • 1
  • 2
  • 6