Raycasts not working as needed

I have a character with two raycasts on it. The movement controls change depending on which raycast is in contact with a collider. This is done with a boolean (in Java Script):

(Not actual code!)

if raycast 1 {control-set 1 = true;
              control-set 2 = false;}

if raycast 2 {control-set 2 = true;
              control-set 1 = false;}

The problem is, the controls will begin as control-set 1. when the 2nd raycast hits a collider, it will change to control-set 2. When raycast 1 then hits a collider, it won’t change the controls.

So what i need is: If raycast 1 is touching a collider and then raycast 2 comes into contact with a collider, automatically default to raycast 2 and vice-versa.

Hope that’s clear enough :slight_smile:

2 Answers

2

Code please, im having a hard time understanding what you mean.

You could just make an if-sentence stating that if raycast 2 is null, then it should use raycast 1.

Edit:

private GameObject hGO;
private GameObject h2GO;

void Update () {
	RaycastHit hit;
	RaycastHit hit2;
	
	//cast the first ray, this one goes down
	if(Physics.Raycast(transform.position, -Vector3.up, out hit, 100f)){
		hGO = hit.collider.gameObject;
	}else{
		hGO = null;
	}
	
	//cast the second ray, this one goes up
	if(Physics.Raycast(transform.position, Vector3.up, out hit2, 100f)){
		h2GO = hit2.collider.gameObject;
	}else{
		h2GO = null;
	}
	
	
	//prioritize between the different controls
	if(h2GO != null)
	{
		Debug.Log("using set two");
	}
	else if(hGO != null)
	{
		Debug.Log("using set one");
	}
	else
	{
		Debug.Log("using something else");
	}
 }

this would have worked if you had only had 3 controls, you should be able to adapt it into 4 controls if you play around with it for a bit.
playing around with as many bools as you’ve got is just silly :stuck_out_tongue:

I wanted to keep things simple with the question, there are actually 4 raycasts :stuck_out_tongue:

    private var myFloor : boolean = true;	
	private var myCeling : boolean = false;
	private var myLeftwall : boolean = false;
	private var myRightwall : boolean = false;
	
	var myLine : float = 1.5;
			
	var moveSpeed : float = 1.0;
	var height : float = 10.0;

	var gravity : float = 100.0;

function Update () {
    //Raycast for floor contrls
	var up = transform.TransformDirection(Vector3.up);
	Debug.DrawRay(transform.position, -up * myLine, Color.green);
if(Physics.Raycast(transform.position, -up, myLine)){
	myFloor = true;
	myCeling = false;
	myLeftwall = false;
	myRightwall = false;
	
	Debug.Log("Floor");
	
	//rigidbody.velocity = Vector3(0, 0, 0);
	//rigidbody.AddForce(Vector3(0, -gravity, 0));
}
	//Raycast for celing controls
	var down = transform.TransformDirection(Vector3.up);
	Debug.DrawRay(transform.position, down * myLine, Color.green);
if(Physics.Raycast(transform.position, down, myLine)){		
	Debug.Log("Celing");
	myCeling = true;
	myFloor = false;
	myLeftwall = false;
	myRightwall = false;
	
	//rigidbody.velocity = Vector3(0, 0, 0);
	//rigidbody.AddForce(Vector3(0, gravity, 0));
	
	}
	
	//Raycast for left wall controls
	var myLeft = -transform.TransformDirection(Vector3.right);
if(Physics.Raycast(transform.position, myLeft, myLine)){		
	Debug.Log("LeftWall");
	myLeftwall = true;
	myFloor = false;
	myCeling = false;
	myRightwall = false;
		
//rigidbody.velocity = Vector3(0, 0, 0);
//rigidbody.AddForce(Vector3(-gravity, 0, 0));
}
	//Raycast for right wall controls
	var myRight = transform.TransformDirection(Vector3.right);
if(Physics.Raycast(transform.position, myRight, myLine)){		
	Debug.Log("RightWall");
	myRightwall = true;
	myFloor = false;
	myCeling = false;
	myLeftwall = false;
	
	//rigidbody.velocity = Vector3(0, 0, 0);
	//rigidbody.AddForce(Vector3(gravity, 0, 0));
	}

	//Floor controls
	if(myFloor == true){
		
		if(Input.GetButton("right"))
	{
			transform.position += transform.right *moveSpeed* Time.deltaTime;
	}
		if(Input.GetButton("left"))
	{
			transform.position += -transform.right *moveSpeed* Time.deltaTime; 
	}
		if(Input.GetButton("jump"))
	{
			transform.position += transform.up *height* Time.deltaTime; 
	}
		
			}
			
	//Celing controls		
	if(myCeling == true){
	
		if(Input.GetButton("right"))
	{
			transform.position += transform.right *moveSpeed* Time.deltaTime;
	}
		if(Input.GetButton("left"))
	{
			transform.position += -transform.right *moveSpeed* Time.deltaTime; 
	}
		if(Input.GetButton("jump"))
	{
			transform.position += -transform.up *height* Time.deltaTime; 
	}
			
	    }
	
	//Left Wall controls
	if(myLeftwall == true){
		
		if(Input.GetButton("up"))
	{
			transform.position += transform.up *moveSpeed* Time.deltaTime;
	}
		if(Input.GetButton("down"))
	{
			transform.position += -transform.up *moveSpeed* Time.deltaTime; 
	}
		if(Input.GetButton("jump"))
	{
			transform.position += -transform.right *height* Time.deltaTime; 
	}
	
			}
			
		
	if(myRightwall == true){	
	
		if(Input.GetButton("up"))
	{
			transform.position += transform.up *moveSpeed* Time.deltaTime;
	}
		if(Input.GetButton("down"))
	{
			transform.position += -transform.up *moveSpeed* Time.deltaTime; 
	}
		if(Input.GetButton("jump"))
	{
			transform.position += transform.right *height* Time.deltaTime; 
	}
			
	}
		}

holy batshit, you could also just have edited into your own post, instead of making a new answer :)

–

but yeah, i think i understand what it is you want, basically you want a game where you can walk on walls and cielings, if im not wrong.

–

Check my first answer, i've updated it with some code that might help, its in c# though, you only need to change it into javascript, shouldnt be too hard :)

–

oh yeah, also you would want to look at "input.getaxis" http://unity3d.com/support/documentation/ScriptReference/Input.GetAxis.html

–