This is intended to be a very simple script where if the raycast hits something it rotates the object in the direction away from it. however this doesn not seem to be working as expected. I have tried a few different things and none of them seem to work. I think my understanding of raycats may be wonky,
anyway here is my code
var speed : float = 8;
var player : GameObject;
var playerPosition : Vector3;
var hit : RaycastHit;
var collide : boolean = false;
function Update (){
var leftFrontRay = transform.TransformDirection(Vector3(-1,0,2));
var rightFrontRay = transform.TransformDirection(Vector3(1,0,2));
Debug.DrawRay(transform.position, leftFrontRay * 3, Color.green);
Debug.DrawRay(transform.position, rightFrontRay * 3, Color.cyan);
if(Physics.Raycast(transform.position,leftFrontRay, hit, 3)){
if(hit.collider.gameObject != "FakePlayer" && hit.collider.gameObject != "Sphere(Clone)" && hit.collider.gameObject != "Enemy1(Clone)"){
Debug.Log("Hit leftfront");
transform.Rotate(Vector3.Right * Time.deltaTime * 30);
collide = true;
}}
if(Physics.Raycast(transform.position,rightFrontRay, hit, 3)){
if(hit.collider.gameObject != "FakePlayer" && hit.collider.gameObject != "Sphere(Clone)" && hit.collider.gameObject != "Enemy1(Clone)"){
Debug.Log("Hit leftfront");
transform.Rotate(Vector3.Left * Time.deltaTime * 30);
collide = true;
}}
player = GameObject.FindWithTag("Player");
playerPosition = player.transform.position;
var target : Vector3 = playerPosition;
var moveDirection : Vector3 = target - transform.position;
var velocity = rigidbody.velocity;
if(moveDirection.magnitude<1){
velocity = Vector3.zero;
}
else{
velocity = moveDirection.normalized*speed;
}
rigidbody.velocity = velocity;
if (collide == false){
transform.LookAt(
}
player = GameObject.FindWithTag("Player");
playerPosition = player.transform.position;
}
I am teaching myself to program, which is easier said than done haha, please dont burn me
any advice appreciated.
more info:
The rays are attached to a rigid body that is instantiated in a code to make them spawn. it also has a basic health system attached to it.
The lines fan out of the front of the enemy. and rotate with it as expected.
When these spawn enemies they meet a wall they dont really do anything except sort of freeze. it is hard to describe.
My player name is FakePlayer, the bullets I create are called Sphere(Clone), the other enemies are called Enemy1(Clone).