Move Object around a platform of tiles in Unity - unity3d

I want to make a spinning spike to move around a platform made out of tiles like in the following
I have written every possible state on where to move when platform tiles block the spike's way. It would look something like this
for (int i = 0; i < platformTileMap.Length; i++)
{
if (platformTileMap[i].HasTile(cellPosition + new Vector3Int(0, -1, 0))) // BOTTOM
{
moveX = moveDir;
}
else if (platformTileMap[i].HasTile(cellPosition + new Vector3Int(0, 1, 0))) // TOP
{
moveX = -moveDir;
}
else if (platformTileMap[i].HasTile(cellPosition + new Vector3Int(-1, -1, 0))) //BOT LEFT
{
if (moveDir == 1)
{
moveY = -1;
}
else moveX = moveDir;
}
else if (platformTileMap[i].HasTile(cellPosition + new Vector3Int(1, 1, 0))) //TOP RIGHT
{
if (moveDir == 1)
{
moveY = 1;
}
else moveX = -moveDir;
}
else if (platformTileMap[i].HasTile(cellPosition + new Vector3Int(1, -1, 0))) // BOT RIGHT
{
if (moveDir == 1)
{
moveX = moveDir;
}
else
{
moveY = -1;
}
}
else if (platformTileMap[i].HasTile(cellPosition + new Vector3Int(-1, 1, 0))) // TOP LEFT
{
if (moveDir == -1)
{
moveY = 1;
}
else
{
moveX = -moveDir;
}
}
I feel like there has to be a more efficient way to solve this. Do I really have to write every possibility in if statements? Can I achieve this with pathfinding?

How about this
Use raycast
Place Empty GameObject and rotate spike when it arrives them

You could create an array of points to move to.
public Vector3[] movePoints;
You can set movePoints in inspector or in code (such as Start function) user choice
In the update loop lerp to the next point in sequence, when arrived pull the next point, unless we are at the end of the array then pull the first point in the array, rinse and repeat forever.
https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html
Set this up properly once, when you make more blades and different configurations or want to change speed it will be ez pz.

For anyone interested. Here is how i solved it:
GridLayout platformGridLayout;
Grid platformGrid;
Tilemap[] platformTileMap;
[SerializeField] float rotationSpeed;
[SerializeField] float moveSpeed;
[SerializeField] private LayerMask platformLayerMask;
Vector3Int startPos;
Vector3Int currentCellPosition;
Vector3 raycastPlatformDir;
Vector3 raycastMoveDir;
float platformRaycastDist;
// Start is called before the first frame update
void Start()
{
movePoints = new List<Vector3Int>();
platformGridLayout = transform.parent.GetComponentInParent<GridLayout>();
platformGrid = transform.parent.GetComponentInParent<Grid>();
startPos = platformGridLayout.WorldToCell(transform.position);
Debug.Log("Cell Startposition: " + startPos);
PlatformToMoveOn();
GetStartRaycastDir();
platformRaycastDist = platformGridLayout.cellSize.x;
Debug.Log("CellCenterToWorld of Startposition: " + platformGrid.GetCellCenterWorld(startPos));
Debug.Log(platformGrid.GetCellCenterLocal(currentCellPosition + Vector3Int.FloorToInt(raycastPlatformDir) + Vector3Int.FloorToInt(raycastMoveDir)));
}
private void PlatformToMoveOn()
{
platformTileMap = new Tilemap[2];
platformTileMap[0] = GameObject.Find("Platform").GetComponent<Tilemap>();
platformTileMap[1] = GameObject.Find("MovingPlatform").GetComponent<Tilemap>();
}
private void GetStartRaycastDir()
{
for (int i = 0; i < platformTileMap.Length; i++)
{
if (platformTileMap[i].HasTile(startPos + new Vector3Int(0, -1, 0))) // BOTTOM
{
raycastPlatformDir = Vector3.down;
}
else if (platformTileMap[i].HasTile(startPos + new Vector3Int(0, 1, 0))) // TOP
{
raycastPlatformDir = Vector3.up;
}
else if (platformTileMap[i].HasTile(startPos + new Vector3Int(1, 0, 0))) // RIGHT
{
raycastPlatformDir = Vector3.right;
}
else if (platformTileMap[i].HasTile(startPos + new Vector3Int(-1, 0, 0))) // LEFT
{
raycastPlatformDir = Vector3.left;
}
}
raycastMoveDir = Quaternion.Euler(0, 0, 90) * raycastPlatformDir * Mathf.Sign(moveSpeed);
//raycastMoveDir = new Vector3(raycastPlatformDir.y, raycastPlatformDir.x) * Mathf.Sign(moveSpeed);
}
// Update is called once per frame
void Update()
{
MoveSpike();
}
private void MoveSpike()
{
currentCellPosition = platformGridLayout.WorldToCell(transform.position); // + raycastPlatformDir * platformGridLayout.cellSize.y / 2;
// Debug.Log(cellPosition);
//Debug.Log(raycastMoveDir);
transform.Rotate(0, 0, 300 * rotationSpeed * Time.deltaTime);
RaycastHit2D raycastMove = Physics2D.Raycast(platformGrid.GetCellCenterLocal(currentCellPosition),raycastMoveDir,0.01f,platformLayerMask);
RaycastHit2D raycastPlatform = Physics2D.Raycast(platformGrid.GetCellCenterLocal(currentCellPosition), raycastPlatformDir, platformRaycastDist, platformLayerMask);
Debug.DrawRay(transform.position, raycastMoveDir * 0.01f, Color.red);
Debug.DrawRay(transform.position, raycastPlatformDir * platformRaycastDist, Color.green);
if (currentCellPosition != startPos) { // Check on Platform corners
Debug.Log("Checking");
if (raycastMove.collider != null)
{
// reassign raycastsdirections
RotateRaycastDirections(1);
Debug.Log("Spike Collision");
}
else if (raycastPlatform.collider == null)
{
RotateRaycastDirections(-1);
Debug.Log("Spike on Platform");
}
startPos = currentCellPosition;
}
/*transform.position = Vector3.MoveTowards(transform.position,
platformGrid.GetCellCenterLocal(currentCellPosition + Vector3Int.FloorToInt(raycastPlatformDir) + Vector3Int.FloorToInt(raycastMoveDir)), moveSpeed * Time.deltaTime); */
transform.Translate(raycastMoveDir.x * Mathf.Abs(moveSpeed) * Time.deltaTime, raycastMoveDir.y * Mathf.Abs(moveSpeed) * Time.deltaTime, 0, Space.World);
}
private void RotateRaycastDirections(int angle)
{
raycastPlatformDir = Quaternion.Euler(0, 0, 90) * raycastPlatformDir * angle * Mathf.Sign(moveSpeed);
raycastMoveDir = Quaternion.Euler(0, 0, 90) * raycastMoveDir * angle * Mathf.Sign(moveSpeed);
// raycastPlatformDir = new Vector3(raycastPlatformDir.y, raycastPlatformDir.x) * angle;
//raycastMoveDir = new Vector3(raycastMoveDir.y, raycastMoveDir.x) * -angle;
}
Edit:
This doesnt work on moving Platforms though. Any ideas how i could fix that? I tried changing the raycast position to the tilemapscentercell position but it doesnt work.

Related

How to prevent automatic moving of an object when touched by other object

I have a car in my project, when I hit other objects by it, it goes to it's opposite axis, but when I use cube to hit other objects, it works correctly. Here is the code for moving the car:
// Update is called once per frame
void Update()
{
if (Input.GetButton("Vertical"))
{
VerticalMove = Input.GetAxis("Vertical") * Time.deltaTime * 10;
transform.Translate(0, 0, VerticalMove);
if (Input.GetButton("Horizontal"))
{
if (Input.GetAxis("Vertical") > 0)
{
HorizontalMove = Input.GetAxis("Horizontal") * Time.deltaTime * 50;
transform.Rotate(0, HorizontalMove, 0);
}
else
{
float x = Input.GetAxis("Horizontal");
if (x > 0) {
x -= (x*2);
} else {
x = x * (-2) / 2;
}
HorizontalMove = x * Time.deltaTime * 50;
transform.Rotate(0, HorizontalMove, 0);
}
}
}
and the car is downloaded form asset store named "ARCADE Free Racing Car"

Power of trajectory prediction Gold2d

I would like to add a force to my ball in my golf game, so if the player clicks the ball and holds a left mouse button, the power of the shot is growing, so is the trajectory. When the power reaches a maximum value the ball is shot immediately. I have a huge problem with it, and I'm stuck on it for a few days now. Maybe someone can help me? Code below:
internal Rigidbody2D ballRB;
internal Vector3 startPos = Vector3.zero;
int dotCount;
internal bool isClicked = false;
bool trajectoryVisible = false;
bool paraboleReachedEndOfScreen = true;
GameObject trajectoryDots;
GameObject ballClick;
//TrajectoryDots
List<Transform> dots = new List<Transform>();
//shot velocity
Vector2 shotForce = Vector2.zero;
public float startValShootingPower = 3f;
public float shootingPower = 3f;
float minBallPointerDist = 0.5f;
float dotSeparation = 10f;
float dotShift = 5f;
Transform lastDotTransform;
void Start()
{
startPos = transform.position;
startValShootingPower = shootingPower;
ballRB = GetComponent<Rigidbody2D>();
trajectoryDots = GameObject.Find("Trajectory Dots");
ballClick = transform.Find("Ball Click Area").gameObject;
dotCount = trajectoryDots.transform.childCount;
foreach (var dot in trajectoryDots.GetComponentsInChildren<Transform>())
{
dots.Add(dot);
}
dots.Remove(trajectoryDots.transform);
}
void Update()
{
foreach (var dot in dots)
{
if (dot.GetComponent<SpriteRenderer>().enabled)
{
lastDotTransform = dot;
}
}
paraboleReachedEndOfScreen = lastDotTransform.position.x <= -UsefulReferences.CameraViewFrustum.x / 2 || lastDotTransform.position.x >= UsefulReferences.CameraViewFrustum.x / 2;
ballClick.SetActive(!isClicked);
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if (Input.GetButtonDown("Fire1"))
{
if (hit.collider != null)
{
//check if the mouse is hovering ball click area
if (hit.collider.transform.root.name == name)
{
trajectoryVisible = true;
}
}
}
if ((Input.GetButtonUp("Fire1") || (paraboleReachedEndOfScreen && Input.GetButton("Fire1"))) && !isClicked && trajectoryVisible) //player released the mouse button or the parabole reached the end of the screen
{
trajectoryVisible = false;
isClicked = true;
UsefulReferences.ballScript.ballRB.constraints = RigidbodyConstraints2D.FreezeRotation;
ballRB.velocity = shotForce;
foreach (var dot in dots)
dot.transform.position = Vector3.zero;
Invoke("GroundCheckAfterShot", 0.1f);
}
if (trajectoryVisible && !isClicked)
{
shotForce = (transform.position - Camera.main.ScreenToWorldPoint(Input.mousePosition)) * shootingPower;
for (int i = 0; i < dotCount; i++)
{
dots[i].gameObject.SetActive(true);
dots[i].transform.position = new Vector2(transform.position.x + shotForce.x * Time.fixedDeltaTime * (dotSeparation * i + dotShift),
transform.position.y + shotForce.y * Time.fixedDeltaTime * (dotSeparation * i + dotShift) - (-Physics2D.gravity.y / 2f * Time.fixedDeltaTime * Time.fixedDeltaTime * (dotSeparation * i + dotShift) * (dotSeparation * i + dotShift)));
}
}
else
{
foreach (var dot in dots)
{
dot.gameObject.SetActive(false);
}
}
//if the ball tried to fly out of the screen
if (transform.position.y > UsefulReferences.CameraViewFrustum.y / 2 || transform.position.x > UsefulReferences.CameraViewFrustum.x / 2 || transform.position.x < -UsefulReferences.CameraViewFrustum.x / 2)
{
OnCollisionEnter2D(null);
}
if (isClicked && UsefulReferences.ballScript.ballRB.constraints == RigidbodyConstraints2D.FreezeAll)
{
UsefulReferences.gcsScript.gameOver = true;
}
}
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Hole")
{
UsefulReferences.gcsScript.Score();
ballRB.constraints = RigidbodyConstraints2D.FreezeAll;
}
}
void OnCollisionEnter2D(Collision2D collision)
{
ballRB.constraints = RigidbodyConstraints2D.FreezeAll;
}
void GroundCheckAfterShot()
{
if (transform.position.y - startPos.y < 0.05f)
ballRB.constraints = RigidbodyConstraints2D.FreezeAll;
}
}

Unity 2D Platformer - making my enemy not see through walls

basicly i'm searching for an idea. How to make my Enemy patroling, but when it reaches the Player - it rush and deal damage, but in the same time when it reach the wall and player is standing behind the wall it Don't attack. You know - i don't want to let them see through walls.
I have made simple Game Object (rectangle) to point it's sight, it is working correctly but i want to improve some of it.
To make it quckier... i just want to make my enemies attack me but to not seen me througth walls
Some code:
[DamageFromEnemy.cs]
private void FixedUpdate()
{
isThatPlayer = Physics2D.OverlapBox(PlayerDetector.position, new Vector2(playerDetectX, playerDetectY), 0, PlayerLayer);
isThatWall = Physics2D.OverlapBox(PlayerDetector.position, new Vector2(playerDetectX, playerDetectY), 0, gameObject.GetComponent<EnemyScript>().WallLayer);
if (isThatWall == true && gameObject.GetComponent<EnemyScript>().movingRight == true)
{
PlayerDetector.transform.Translate(new Vector3(changePosX, 0, 0));
//playerDetectX -= PlayerDetector.transform.position.x; // Lowering size. Not Working
changePosX = (playerDetectX / 2) * (1 / enemyScaleX) + (enemyScaleX / 2) * (1 / enemyScaleX);
Debug.Log("pozycja x: " + changePosX);
}
if (isThatWall == true && gameObject.GetComponent<EnemyScript>().movingRight == false)
{
PlayerDetector.transform.Translate(new Vector3(- changePosX, 0, 0));
//playerDetectX -= PlayerDetector.transform.position.x; // Lowering size. Not Working
changePosX = (playerDetectX / 2) * (1 / enemyScaleX) + (enemyScaleX / 2) * (1 / enemyScaleX);
Debug.Log("pozycja x: " + changePosX);
}
DetectPlayer();
AttackTimer();
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.blue;
Gizmos.DrawWireCube(PlayerDetector.position, new Vector3(playerDetectX, playerDetectY));
}
[EnemyScript.cs]
void Update ()
{
trap = Physics2D.OverlapCircle(ColideDetector.position, detectorRadius, TrapLayer);
otherEnemy = Physics2D.OverlapCircle(ColideDetector.position, detectorRadius, EnemyLayer);
if (health <= 0)
{
Destroy(gameObject);
}
transform.Translate(Vector2.right * speed * Time.deltaTime );
RaycastHit2D groundInfo = Physics2D.Raycast(groundDetection.position, Vector2.down, distance);
RaycastHit2D wallInfoR = Physics2D.Raycast(wallDetection.position, Vector2.right, distance, WallLayer);
RaycastHit2D wallInfoL = Physics2D.Raycast(wallDetection.position, Vector2.left, -distance, WallLayer);
if (groundInfo.collider == false || trap == true || otherEnemy == true || wallInfoR == true || wallInfoL == true)
{
if(movingRight == true)
{
transform.eulerAngles = new Vector3(0, -180, 0);
movingRight = false;
}
else
{
transform.eulerAngles = new Vector3(0, 0, 0);
movingRight = true;
}
}
}
Some Gif Feedback
At last gifs frames you can see that it bugs sometimes.
public PlayerControls player;
public LayerMask WallLayer;
player = FindObjectOfType<PlayerControls>();
RaycastHit2D checkWallsToHero = Physics2D.Raycast(wallToHeroRay.position, player.transform.position, 150,WallLayer);
if (checkWallsToHero == true)
{
playerCheck = false;
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.blue;
Gizmos.DrawWireCube(PlayerDetector.position, new Vector3(playerDetectX, playerDetectY));
Gizmos.DrawLine(wallToHeroRay.position, player.transform.position);
}
[New Code sample - propably wrong]:
isThatPlayer = Physics2D.OverlapBox(PlayerDetector.position, new Vector2(playerDetectX, playerDetectY), 0, PlayerLayer);
isThatWall = Physics2D.OverlapBox(PlayerDetector.position, new Vector2(playerDetectX, playerDetectY), 0, gameObject.GetComponent<EnemyScript>().WallLayer);
RaycastHit2D checkWallsToHero = Physics2D.Raycast(wallToHeroRay.position, player.transform.position, 0, gameObject.GetComponent<EnemyScript>().WallLayer);
if (checkWallsToHero.collider != true /*&& isThatPlayer == true*/)
{
Debug.Log("Pierwszy state");
Debug.Log(checkWallsToHero.collider);
//Debug.Log(isThatPlayer);
//Debug.Log("Hit: " + checkWallsToHero.collider.gameObject.name);
playerCheck = false;
enemyAttackReady = false;
coolDownTimer = 0;
enemyAttackCD = 0;
}
else if (checkWallsToHero.collider == true && isThatPlayer == true)
{
Debug.Log(checkWallsToHero.collider);
Debug.Log("Drugi state");
ReadyToAttack(); //charging enemy
DetectPlayer(); //bool to true when OverlapBox hits player
AttackTimer(); //cd between enemy attacks
}
if (Physics2D.Raycast2D
(wallToHeroRay.position,player.transform.position,out hit,10)
&&hit.transform.gameObject.tag == "Player")
{
RayHit = true;
}
else {
RayHit = false;
}
to use this you will have to tag your player as 'Player' then your ray will only give you a hit if it hits the player itself, and not a wall
Edit: this is code i threw together following #Draco18s's idea in the comments, if it works for you, tell him thank you. i just thought it made enough sense he should have put it as an answer

Unity 3rd person controller, rotation nightmare

I am trying to make a MMO character controller like the one of this Youtube video:https://www.youtube.com/watch?v=fOvf7gRO_aM
Basically, you use WASD to move around.
You can move the camera by mouse click and drag, and when moving, you character will now move in the new camera direction.
The thing is, i would like that when i press WASD, that the character (not the camera) would face the direction of the mouvement.
I tried to use this:
if (Input.GetAxis("Vertical") > 0 | Input.GetAxis("Vertical") < 0){
Quaternion turnAngle = Quaternion.Euler(0, centerPoint.eulerAngles.y, 0);
character.localRotation = Quaternion.Slerp(character.rotation, turnAngle, Time.deltaTime * rotationSpeed);
}
The character was not facing the right direction, so i tried this
if (Input.GetAxis("Vertical") > 0 | Input.GetAxis("Vertical") < 0){
Quaternion turnAngle = Quaternion.Euler(0, centerPoint.eulerAngles.y, 0);
character.rotation = Quaternion.LookRotation(movement);
}
but that does not seem to work. I am a noob after all :D
Here is the full code of the controller's move part:
private void Move()
{
moveFrontBack = Input.GetAxis("Vertical") * moveSpeed;
moveLeftRight = Input.GetAxis("Horizontal") * moveSpeed;
Vector3 movement = new Vector3(moveLeftRight, 0, moveFrontBack);
movement = character.rotation * movement;
characterController.Move(movement * Time.deltaTime);
//Animation on move
if (movement.magnitude != 0)
{
anim.SetBool("isWalking", true);
anim.SetBool("isIdle", false);
}
if (movement.magnitude == 0)
{
anim.SetBool("isWalking", false);
anim.SetBool("isIdle", true);
}
centerPoint.position = new Vector3(character.position.x, character.position.y + mouseYPosition, character.position.z);
//The place where things go south it seems
if (Input.GetAxis("Vertical") > 0 | Input.GetAxis("Vertical") < 0)
{
Quaternion turnAngle = Quaternion.Euler(0, centerPoint.eulerAngles.y, 0);
character.rotation = Quaternion.Slerp(character.rotation, turnAngle, Time.deltaTime * rotationSpeed);
}
I had a previous version on the controller, without the change of camera with mouse, but the right character's behaviour facing the direction of the input:
private void Move()
{
moveFrontBack = Input.GetAxis("Vertical") * moveSpeed;
moveLeftRight = Input.GetAxis("Horizontal") * moveSpeed;
Vector3 movement = new Vector3(moveLeftRight, 0, moveFrontBack);
characterController.Move(movement * Time.deltaTime);
if (movement != Vector3.zero) transform.rotation = Quaternion.LookRotation(movement);
if (movement.magnitude != 0)
{
anim.SetBool("isWalking", true);
anim.SetBool("isIdle", false);
}
if (movement.magnitude == 0)
{
anim.SetBool("isWalking", false);
anim.SetBool("isIdle", true);
}
playerCamera.position = new Vector3(character.position.x, character.position.y + yCamera, character.position.z + zCamera);
}
Also, here is the mousemoving part:
void MouseTurnAround()
{
zoom += Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
if (zoom > zoomMin)
zoom = zoomMin;
if (zoom < zoomMax)
zoom = zoomMax;
playerCamera.transform.localPosition = new Vector3(0, 0, zoom);
if (Input.GetMouseButton(0))
{
mouseX += Input.GetAxis("Mouse X");
mouseY -= Input.GetAxis("Mouse Y");
}
mouseY = Mathf.Clamp(mouseY, -60f, 60f);
playerCamera.LookAt(centerPoint);
centerPoint.localRotation = Quaternion.Euler(mouseY, mouseX, 0);
}
I don't really have more ideas, so maybe smart people can see what they can do ! Thanks in advance..
I managed some kind of a mix of my 2 solutions, and parented my character to an empty gameobject, which receives the new rotations. Then, i leave the character be oriented to the movement with look rotation.
private void Move()
{
moveFrontBack = Input.GetAxis("Vertical") * moveSpeed;
moveLeftRight = Input.GetAxis("Horizontal") * moveSpeed;
Vector3 movement = new Vector3(moveLeftRight, 0, moveFrontBack);
movement = character.rotation * movement;
characterController.Move(movement * Time.deltaTime);
//Animation on move
if (movement.magnitude != 0)
{
anim.SetBool("isWalking", true);
anim.SetBool("isIdle", false);
}
if (movement.magnitude == 0)
{
anim.SetBool("isWalking", false);
anim.SetBool("isIdle", true);
}
centerPoint.position = new Vector3(character.position.x, character.position.y + mouseYPosition, character.position.z);
//Rotates the character to move towards new direction
if (Input.GetAxis("Vertical") > 0 | Input.GetAxis("Vertical") < 0)
{
Quaternion turnAngle = Quaternion.Euler(0, centerPoint.eulerAngles.y, 0);
character.rotation = Quaternion.Slerp(character.rotation, turnAngle, Time.deltaTime * rotationSpeed);
}
if (movement != Vector3.zero) lkModel.rotation = Quaternion.LookRotation(movement);
}

Trouble with raycasting in Unity 3

I'm trying to script my AI on a simple way. The AI does a raycast in front, left and right of it. Then it takes a random direction in a way that doesn't contain a "Boundary"-element.
First, my Update() checks if it's time to calculate a new direction. If it is, it calculates the new direction, then it moves to that.
I'm using the following code to move:
Debug.DrawLine(transform.position, transform.position + transform.forward, Color.yellow);
Debug.DrawLine(transform.position, transform.position + transform.right, Color.yellow);
Debug.DrawLine(transform.position, transform.position - transform.right, Color.yellow);
//DEBUGS START AND END POSITION ARE CORRECT
var startTime;
if (Time.time > nextUpdate) {
Debug.Log("New check");
var dirWay = MoveDirection();
//if (dirWay == 0)
//rot = Quaternion.Euler(0, 0, 0);
if (dirWay == 1) {
rot = Quaternion.Euler(0, 90, 0);
}
if (dirWay == 2) {
rot = Quaternion.Euler(0, -90, 0);
}
if (dirWay == 3) { //backwards
rot = Quaternion.Euler(0, 180, 0);
}
nextUpdate = Time.time + walkTime; //for example, 2: Every 2 seconds an update
direction.y = 1;
direction.y = 1.5 - transform.position.y;
transform.rotation = transform.rotation * rot;
transform.position = transform.position + transform.forward + transform.forward;
//Plus 2 * transform.forward because it moves 2 places
}
The function MoveDirection checks for obstacles through raycasting. My AI moves the correct distance in the correct time, but walks through walls. That means my raycasting is wrong. I'm using the following code:
var obstacles = ["Border", "Boundary", "BoundaryFlame"];
var frontAvailable = true;
var leftAvailable = true;
var rightAvailable = true;
var hitFront: RaycastHit;
if (Physics.Raycast(transform.position, transform.position + transform.forward, hitFront, 1.9)) {
for (var i = 0; i < obstacles.length; i++)
{
if (hitFront.collider.gameObject.name.IndexOf(obstacles[i]) > -1)
{
frontAvailable = false;
}
}
}
var hitLeft: RaycastHit;
if (Physics.Raycast(transform.position, transform.position - transform.right, hitLeft, 1.9)) {
for (var j = 0; j < obstacles.length; j++)
{
if (hitLeft.collider.gameObject.name.IndexOf(obstacles[j]) > -1)
{
leftAvailable = false;
}
}
}
var hitRight: RaycastHit;
if (Physics.Raycast(transform.position, transform.position + transform.right, hitRight, 1.9)) {
for (var k = 0; k < obstacles.length; k++)
{
if (hitRight.collider.gameObject.name.IndexOf(obstacles[k]) > -1)
{
rightAvailable = false;
}
}
}
So, am I right that when I want to check 2 units in front of the AI (transform.forward from the AI's point of view, not the global view!), I should use: Physics.Raycast(transform.position, transform.position + transform.forward, hitFront, 1.9) ?
http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
So, am I right that when I want to check 2 units in front of the AI
(transform.forward from the AI's point of view, not the global view!),
I should use: Physics.Raycast(transform.position, transform.position +
transform.forward, hitFront, 1.9) ?
you should have Physics.Raycast(transform.position,transform.forward,hitFront, 1.9);