x


Adding physics to various terrain

Is there a way to add physics to various terrain so that (for example) when your character is wading in water his speed becomes 80% of normal speed. Or when your character is wading through forest, 85%. You get the picture.

To clarify, I'm looking for a way to dynamically add physics to terrain. I haven't been able to find anything on the subject. It might be that I dunno what to look for, but any help is appreciated.

more ▼

asked Oct 20 '11 at 10:05 AM

samzen gravatar image

samzen
1 2 3 4

These things you are talking about... aren't physics. There is no such thing as a 'slow field' (as you seem to be talking about), as such, only substances with more or less friction. I think you should think carefully about what you are trying to do here, and make sure there isn't a better way (like manually defining trigger volumes, which cause different behaviours on the player).

Oct 20 '11 at 08:59 AM syclamoth
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

As @syclamoth said, that's not a physics stuff: this should be implemented in the movement script of your character. If you use a CharacterController (most usual approach) you can define triggers for different terrain regions. For water, for instance, you can "bury" a cube under the water plane and adjust its dimensions to cover the whole water portion. Set isTrigger in the collider, and tag it as "Water". In the character script, add OnTriggerEnter and OnTriggerExit events and change the character speed according to the terrain - for instance:

var normalSpeed: float = 6.0;
var waterSpeed: float = 4.5;
var forestSpeed: float = 5.0;
var inWater: boolean = false;
var inForest: boolean = false;

function Update(){
    // get the movement direction (see CharacterController.Move example)
    // move the character according to controls and region
    var speed = normalSpeed;
    if (inWater){
        speed = waterSpeed;
    }
    else if (inForest){
        speed = forestSpeed;
    }
    character.Move(moveDirection * speed * Time.deltaTime);
}

function OnTriggerEnter(other: Collider){
    if (other.tag == "Water"){
        inWater = true;
    }
    else if (other.tag == "Forest"){
        inForest = true;
    }
}

function OnTriggerExit(other: Collider){
    if (other.tag == "Water"){
        inWater = false;
    }
    else if (other.tag == "Forest"){
        inForest = false;
    }
}
more ▼

answered Oct 20 '11 at 10:45 AM

aldonaletto gravatar image

aldonaletto
41.3k 16 42 195

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

x1865
x1467
x809
x388
x51

asked: Oct 20 '11 at 10:05 AM

Seen: 959 times

Last Updated: Oct 20 '11 at 10:47 AM