How to fix a small raycast issue?

Hi there. I just started working with raycast and now encountered an interesting problem.

I have this code snipet so that when a ray hits a collider the object that emitted the ray would move in a certain direction.You can find the snipet below.

This worked fine in my test scene.However when I put this in the actual script that is attached to my game object the ray is drawn above the game object,rather than coming from the game object it self.Which results in the raycast completely missing it’s target.

I tried using Vector3() to position the ray in the correct position,but this didn’t work as the ray seems to remain in the same position no matter what and not move with the object that is emitting it.

Just wondering is there something I am missing or is there something I could do so that the ray will always be drawn from the game object instead of above it?

All help is appreciated :smiley:

var forward = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;    

Debug.DrawRay(transform.position,-forward*15, Color.green);
 
if((Physics.Raycast(transform.position,-forward,hit,15))&&(onPlatform==true))
	{
	    Debug.Log("Hit");
		if(hit.collider.gameObject.tag == "MainPlatform")
		{
			MovePlatform();
		}
	} 

If something is unclear ask away :smiley:

Just use -transform.forward and place your raycast statement in Update(). Also, no need for so many parentheses.

#pragma strict

var hit : RaycastHit;
var onPlatform : boolean;

function Update()
{
	if(Physics.Raycast(transform.position, -transform.forward, hit, 15) && onPlatform)
	{
		Debug.Log("Hit");
		if(hit.collider.gameObject.tag == "MainPlatform")
		{
			MovePlatform();
		}
	} 
}

function MovePlatform()
{
	//Code
}

What about this?

var forward = transform.TransformDirection(transform.forward);