x


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 :)

more ▼

asked Oct 06 '11 at 12:37 PM

cthugan gravatar image

cthugan
1 1 3 5

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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 :P

more ▼

answered Oct 06 '11 at 12:41 PM

Kacer gravatar image

Kacer
705 14 17 31

(comments are locked)
10|3000 characters needed characters left

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

    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; 
    }

    }
       }
more ▼

answered Oct 06 '11 at 12:53 PM

cthugan gravatar image

cthugan
1 1 3 5

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

Oct 06 '11 at 12:54 PM Kacer

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.

Oct 06 '11 at 01:08 PM Kacer

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 :)

Oct 06 '11 at 01:24 PM Kacer

yeah, thats right :)

I'll mess about with the code you posted above, thanks for your help :) :) :)

Oct 06 '11 at 01:28 PM cthugan

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

Oct 06 '11 at 01:34 PM Kacer
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1528
x240

asked: Oct 06 '11 at 12:37 PM

Seen: 513 times

Last Updated: Oct 06 '11 at 01:34 PM