Raycast collision help? (fails in build)

I’m trying to cast a ray above the player to detect if when the player moves up has it hit an object using a particular tag it is important that it detects the normals of the object as the objects it is hitting have varying surfaces.

the objective here is to detect the surface and overwrite the player controls to push the player away from the surface if they colide otherwise the player will just pass through the colider of the object regardless of collision mesh.

i cant seem to get my head around the the docs for raycast, i’m new to raycast i understand the theory and though the theory of raycasting seems to be the way to go. but it makes no sense to me at all i have no idea how to implement it.

In C#

public void Update()
{
  RaycastHit hit;
  if (Physics.Raycast(transform.position, transform.up, out hit, 10))
  {
     if (hit.transform.gameObject.tag == "mytag")
     {
       // do whatever you need to the player here
     }
  }

}

The object hit also has a property with normal vector at the point it collided with.

Cheers dart your a savior
I assume that the 10 is the raycast distance.
Any idea’s on converting this to js?
I’ll play around with it anyway

JS then

function Update()
{
  var hit : RaycastHit;
  if (Physics.Raycast(transform.position, transform.up, hit, 10))
  {
     if (hit.transform.gameObject.tag == "mytag")
     {
       // do whatever you need to the player here
     }
  }

}

ok so heres what it looks like

var VerticalMovementSpeed = 120; // Speed of Vertical 
Movement force aplied to the PC.

var Player : Transform;

function Update () 
{
var hit : RaycastHit; 
  if (Physics.Raycast(transform.position, transform.up, hit, 5)) 
  { 
     if (hit.transform.gameObject.tag == "GameController") 
     { 
       Debug.Log ("Hello");
	   Player.transform.Translate(0, -VerticalMovementSpeed*Time.deltaTime, 0, Space.World); 
	   
     } 
  }

so it works pefectly but also i cant seem to get it to work down now so it detects collisions below itself :frowning:

If you use

if (Physics.Raycast(transform.position, -transform.up, hit, 5)) {
    ...
}

…you should get the result you want (or is this what you’ve tried and found it doesn’t work?)

thanks andeeee thats perfect but now my upwards collision dosn’t work despite the downwards being an adapted copy paste of the upwards collision and the whole script fails to work in the build

heres the script to date.

var VerticalMovementSpeed = 120; // Speed of Vertical Movement force aplied to the PC.
var Player : Transform;
function Update () 
{
var hit : RaycastHit; 
  if (Physics.Raycast(transform.position, transform.up, hit, 10)) 
  { 
     if (hit.transform.gameObject.tag == "GameController") 
     { 
       Debug.Log ("Hello");
	   Player.transform.Translate(0, VerticalMovementSpeed*Time.deltaTime, 0, Space.World); 
	   
     } 
  } 
	if (Physics.Raycast(transform.position, -transform.up, hit, 10)) 
	{	 
		if (hit.transform.gameObject.tag == "GameController") 
		{ 
			Debug.Log ("Hello");
			Player.transform.Translate(0, -VerticalMovementSpeed*Time.deltaTime, 0, Space.World); 
	   
		} 
	} 
   

}

once this works i intend to mate it with my controller script

Ok so i fixed a problem with an unrelated and unused script and the upwards collision now works but the entire script still fails to work in the build :evil:

ok so i’ve changed the script and messed about with the layers to make sure it works %100 in the editor and it does but still i get zero response when i build and run has anybody any idea why the build fails?

the code looks like this.

var VerticalMovementSpeed = 120; // Speed of Vertical Movement force aplied to the PC.
var Player : Transform;
function Update () 
{
var hit : RaycastHit; 
  if (Physics.Raycast(transform.position, transform.up, hit, 10)) 
  { 
     if (hit.transform.gameObject.tag == "Level Collider") 
     {
	 
	   Player.transform.Translate(0, VerticalMovementSpeed*Time.deltaTime, 0, Space.World); 
	   
     } 
  } 
	if (Physics.Raycast(transform.position, -transform.up, hit, 10)) 
	{	 
		if (hit.transform.gameObject.tag == "Level Collider") 
		{ 
		
			Player.transform.Translate(0, -VerticalMovementSpeed*Time.deltaTime, 0, Space.World); 
	   
		} 
	} 
   

}

Its my birthday and i want it to be a good one so please if you have ever had this problem and solved it please share.

I also note the oddity of programming on my birthday but i prefer this to getting smashed :twisted: