Determing empty space with raycast

Hi,

I have a very simple scene where there are 2 cubes of size 1x1x1 units. The cubes have a box collider of the same size. One of the cubes (cube A) have a script that allows the player to move the cube with 1 unit increments (I´m using Vector3.MoveTowards() to move the cube). Another cube (cube B) just has the collider. To prevent from cubes from intersecting, Im using Physics.Raycast to see if the new location of the cube (that was determined by listening the arrow keys) is empty.

The problem is that the Raycast seems to return true even if the cube A is nowhere near cube B. For example, if I have cube B at xyz location (3,3,0), and I move the cube A to location (2,2,0) and press up to move to position (2,3,0), raycast returns true even theres is no collider at position (2,3,0).

What am I missing?

My code:

using UnityEngine;
using System.Collections;

public class BoxMovement : MonoBehaviour 
{
    public float PlayerSpeed = 6;
    private bool isMoving = false;
    private Vector3 targetLocation;
    	    
    void Update ()
    {
        if (this.isMoving)
        {
            //move towards target
            transform.position = Vector3.MoveTowards(transform.position, targetLocation, Time.deltaTime * PlayerSpeed);

            if (this.transform.position == this.targetLocation)
            {
                this.isMoving = false; //stop moving
            }
        }

        if (this.isMoving == false)
        {            
            if (Input.GetKey(KeyCode.UpArrow))
            {                
                this.targetLocation = new Vector3(transform.position.x, transform.position.y + 1, transform.position.z);
            }
            if (Input.GetKey(KeyCode.DownArrow))
            {
                this.targetLocation = new Vector3(transform.position.x, transform.position.y - 1, transform.position.z);
            }
            if (Input.GetKey(KeyCode.LeftArrow))
            {
                this.targetLocation = new Vector3(transform.position.x - 1, transform.position.y, transform.position.z);              
            }
            if (Input.GetKey(KeyCode.RightArrow))
            {
                this.targetLocation = new Vector3(transform.position.x + 1, transform.position.y, transform.position.z);
            }

            RaycastHit hit = new RaycastHit();
            if (Physics.Raycast(transform.position, targetLocation, out hit, 1.0f) == false)
            {
                isMoving = true; //target location is empty, start moving towards it
            }
            else
            {
                targetLocation = transform.position;
                print("cannot move - theres a block at (" + hit.transform.position.x + "," + hit.transform.position.y + "). Distance: " + hit.distance);
            }
        }
    }
}

A raycast requires the starting position and direction to define the line of ray, but you are specifying the line using the two endpoints. You can use Physics.Linecast for this or else get the direction vector by subtracting the origin from the target position.

My mistake, for some reason I though I saw an overload that takes end position instead of direction as the seconds parameter. It works fine now. Thanks andeeeee.

Btw, this made me wonder why Physics.Linecast even exists if you can do exactly the same with Raycast, but Linecast just has less features. Is the under the hood implementation different or what…?