- Home /
C# sticking to Cube corners position (raycasting AI script)
I'm trying to use raycasting to make an AI script but I have to cast rays from the forward face. Right now I'm stuck as you can see below, I created colored sphere to see the actual positions i'm gathering. (Raycast origin)
So I tried to get the four corners of the forward face but as you can see, only half of it is actually working properly.
Here's my script: (I might have declared a few things i'm not currently using, sorry for the mess)
using UnityEngine;
using System.Collections;
public class AIscript4 : MonoBehaviour {
Vector3 DTarget;
Vector3 DSource;
float Swidth;
MeshFilter mshFilt;
Mesh gridMesh;
GameObject[] goa=new GameObject[4];
// Use this for initialization
ArrayList Waypoints =new ArrayList();
ArrayList goA=new ArrayList();
void Start () {
Transform nz=GameObject.Find ("TargetCube").GetComponent<Transform>();
Vector3 SC=new Vector3(0.3f,0.3f,0.3f);
mshFilt = (MeshFilter)GetComponent<MeshFilter>();
gridMesh = mshFilt.mesh;
DSource=transform.position;
DTarget=nz.position;
Vector3 fixdirection=DTarget-DSource;
Debug.DrawRay(DSource,fixdirection,Color.red,555f);
CreateDebugSphere(Vector3.one,Color.red,SC);
CreateDebugSphere(Vector3.one,Color.yellow,SC);
CreateDebugSphere(Vector3.one,Color.green,SC);
CreateDebugSphere(Vector3.one,Color.magenta,SC);
}
int inc;
void CreateDebugSphere(Vector3 pos,Color col,Vector3 scale){
GameObject go = Instantiate(Resources.Load("DebugSphere")) as GameObject;
go.renderer.material.color=col;
go.transform.position=pos;
go.transform.localScale=scale;
Debug.Log (inc);
goa[inc]=go;
inc+=1;
}
// Update is called once per frame
void Update () {
Vector3 br1=transform.TransformPoint(gridMesh.bounds.min);
Vector3 br2=transform.TransformPoint(gridMesh.bounds.max);
// Red
Vector3 DebugS1=new Vector3(
br2.x,
br1.y,
br2.z
);
// Yellow
Vector3 DebugS2=new Vector3(
br2.x,
br2.y,
br2.z
);
// Green
Vector3 DebugS3=new Vector3(
br1.x,
br2.y,
br2.z
);
// Magenta
Vector3 DebugS4=new Vector3(
br1.x,
br1.y,
br2.z
);
goa[0].transform.position=DebugS1;
goa[1].transform.position=DebugS2;
goa[2].transform.position=DebugS3;
goa[3].transform.position=DebugS4;
}
}
I spent some time searching around for an answer but can't find anything anymore to help myself =\\
I would love some explanation why its acting like that!
To recapitulate, all spheres should stick to the sky blue face corners, in any rotation.
Eventually, I want to cast rays from the bottom of the blue face, two on the corners and one in the middle, towards a target.
Also, if you have better solution or better ways of doing AIs capable of moving and dodging static and/or non-static objects I'd love to know, although ultimately it will be for iPhones, so can't be too much CPU intensive.
Answer by robertbu · Oct 28, 2013 at 04:10 PM
I haven't got my brain around exactly why it is happening. It has to do with how the rotation is applied in the TransformPoint(). But you can fix the problem doing the TranslatePoint() after you've defined the points:
// Update is called once per frame
void Update () {
Vector3 br1=gridMesh.bounds.min;
Vector3 br2=gridMesh.bounds.max;
// Red
Vector3 DebugS1=new Vector3(
br2.x,
br1.y,
br2.z
);
// Yellow
Vector3 DebugS2=new Vector3(
br2.x,
br2.y,
br2.z
);
// Green
Vector3 DebugS3=new Vector3(
br1.x,
br2.y,
br2.z
);
// Magenta
Vector3 DebugS4=new Vector3(
br1.x,
br1.y,
br2.z
);
goa[0].transform.position=transform.TransformPoint(DebugS1);
goa[1].transform.position=transform.TransformPoint(DebugS2);
goa[2].transform.position=transform.TransformPoint(DebugS3);
goa[3].transform.position=transform.TransformPoint(DebugS4);
}
Note that you use GameObject.Find() to find 'TargetCube', but you don't save 'nz' and use 'transform' to reference other items. As it is now, this script must be attached to the cube to work.
BTW: Well asked question. Without the animated GIF I would not have understood.
That is insanely great of you, thank you so much! It's working perfectly now, I'll remember to use TransformPoint only after defining the positions. And yes, I will fix those issues right away, it won't be attached to the cube.
Your answer
![](https://koobas.hobune.stream/wayback/20220613120807im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Smooth change of camera position goes wrong 1 Answer
Cube rotation and force 2 Answers
Multiple Cars not working 1 Answer
Checki f Player is within area? C# 1 Answer