x


Could I get some help with this code?

#pragma strict

var speed = 5.0;

function Start (){}

function Update () { 
    //moving forward/back and left/right
    var x = Input.GetAxis("Vertical") * Time.deltaTime * speed * -1;
    var y = Input.GetAxis("Horizontal") * Time.deltaTime * speed * -1;
    transform.Translate(x, y, 0);

    //mouse controls
    if (Input.GetKeyDown ("escape"))
        Screen.lockCursor = false;

    transform.Rotate(0, 0, (Input.GetAxis("Mouse X") * 2));

    //jumping
    if (Input.GetButtonDown ("Jump"))
        rigidbody.AddForce(0, 100, 0);  
}  

//stop clipping issues
function OnTriggerStay (touch : Collider){  
    if (touch.gameObject.GetComponent("clipcheck"))
        speed = 0.01;    
}

function OnMouseDown(){ 
    Screen.lockCursor = true; 
}

I have my character model, which has a box collider encompassing it. The character model has an invisible cylinder around its waist that has a circle collider attached.

I'm trying to lower to speed variable when the circle collider touches something. How do I do this?

more ▼

asked May 26 '12 at 02:36 PM

Sting_Auer gravatar image

Sting_Auer
32 4 6 7

So the thing your colliding with needs to have a clipcheck script on it right? I'm guessing it has that.

Also can you reformat your code by editing your question, highlighting the code, and clicking the "code" button (the one with the 1 and 0 on it). Thanks.

May 26 '12 at 03:51 PM whydoidoit

Actually I don't have a Clipcheck code on anything else, I named the cylinder "clipcheck", so I'm trying to find out how to detect a trigger from that cylinder, and have it change the speed variable.

May 26 '12 at 04:04 PM Sting_Auer
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Is the circle collider actually a trigger?

more ▼

answered May 26 '12 at 02:55 PM

Ben Ezard gravatar image

Ben Ezard
229 2 6 8

Yes, it is a trigger.

May 26 '12 at 03:33 PM Sting_Auer
(comments are locked)
10|3000 characters needed characters left

Ok so you need to change your code in OnTriggerStay to use the name of the object you are hitting. The code you have at the moment is trying to find a script called clipcheck.

function OnTriggerStay (touch : Collider){

    if (touch.name =="clipcheck") {

      speed = 0.01;
    }

}
more ▼

answered May 26 '12 at 04:13 PM

whydoidoit gravatar image

whydoidoit
33.1k 11 23 100

Note that you will only get a trigger firing if one of the items has a rigidbody attached

May 26 '12 at 04:15 PM whydoidoit

I replaced the faulty part of my code with this, and it isn't working. It doesn't affect the speed of my character.

EDIT: Never mind, I'll assign it a rigidbody now and see if it works.

EDIT AGAIN: How do I make the sphere collider not touch the main box collider? The problem I have now is that the sphere collider is touching the box collider on the actual character model, causing the speed to always be super low.

May 26 '12 at 04:18 PM Sting_Auer

Also the sphere collider might well be colliding with the box collider - was this why you were checking for something in OnTriggerStay?

May 26 '12 at 04:21 PM whydoidoit

All I was trying to do was detect if the character model was against a wall.

That's why I created a sphere collider around its waist that was attached to a different object. I can refer to the sphere collider around the cylinder using the 1 script.

The problem I have now is that the sphere collider is touching the box collider that it is inside. How can I make the 2 colliders ignore each other?

May 26 '12 at 04:24 PM Sting_Auer

If you don't want physical colliders colliding with one another, you can give them a tag "No Collision", then go into your Physical Settings (Edit > Project Settings > Physics), and uncheck the No Collisions with whatever other tags you don't want it to collider with

May 26 '12 at 04:32 PM oliver-jones
(comments are locked)
10|3000 characters needed characters left

Sphere collider, not circle. Circle is 2D.

Why do you need an invisible Cylinder ? A gameObject with only the collider would do the same.

Is that code on that gameobject, the one with the sphere collider ?

Are there any game object physically moved that can enter that object, or is it the one moving ? The latter case will not raise trigger events. There is a way, though.

more ▼

answered May 26 '12 at 04:16 PM

Berenger gravatar image

Berenger
11k 12 19 53

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

x2495
x534
x357

asked: May 26 '12 at 02:36 PM

Seen: 392 times

Last Updated: May 26 '12 at 04:32 PM