Question

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.

Était-ce utile?

La solution 2

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.

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top