I have 2 planes one with a grass texture and one with water. I am trying to skip the ball across the water. I am not sure the best way to achieve this. Should I add a upward force on trigger with water? I wrote a script that sort of works but not well. The problem I am having is every bounce on water the ball goes higher. Any one know of any better ways to do this.
#pragma strict
var noOfHitsOnWater : int; // Number of times ball hits the water
var ahhhhSound : AudioClip; // ahhh Sound Played when ball stops on water
function Start () {
}
function OnCollisionEnter(other : Collision) {
if (other.gameObject.tag == "water"){
rigidbody.AddForce(Vector3.up * other.relativeVelocity.magnitude * 100);
noOfHitsOnWater ++;
// rigidbody.AddForce (0,1000,0);
// if(rigidbody.velocity.magnitude == 0){
// transform.position = RayCasterJS.hitPosition;
// }
}
if (other.gameObject.tag == "Course"){
noOfHitsOnWater = 0;
}
}
function OnCollisionExit(other: Collision){
}
function Update () {
if(noOfHitsOnWater >= 4){
transform.position = RayCasterJS.hitPosition;
audio.PlayOneShot(ahhhhSound); // Play sound
rigidbody.velocity = Vector3(0,0,0);
noOfHitsOnWater = 0;
}
}
I would just separate the Up and Forward velocities and lerp them to 0 over time, probably with some specific damping for each direction. ( ball would lose speed and altitude, probably not at the same rate ).
So, once your force is supplied, use something like “rigidbody.velocity = Vector3.Lerp(rigidbody.velocity,newVelocityWithDampingEffects,Time)”
where newVelocityWithDampingEffects is a vector3 where the relative up and forward values are also lerped to 0.
I am not sure I understand. Would it look like this
var newVelocityWithDampingEffects : Vector3;
var Time : float;
function OnCollisionEnter(other : Collision) {
if (other.gameObject.tag == "water"){
rigidbody.velocity = Vector3.Lerp(rigidbody.velocity,newVelocityWithDampingEffects,Time);
}
}
I set the newVelocityWithDampingEffects to have a Y = 10 but I am not seeing any upward force being applied.
Not exactly, that will not have time to lerp as it will only be called when a collision occurs.
I would start a co-routine when you enter a collision, which applies the initial upwards force, then yields until another collision.( probably using a bool ). The coroutine will do the actual lerping as it would be called continuously. I’m not on a dev pc at the minute so I can’t really provide much concrete examples, also I write in C#. But…
That’s generally what I meant. Obviously there are problems with this (e.g. bounce routine will possibly not exit a loop if the collided flag is not reset on a previous frame ). There are many ways around this but I just wanted to show you the general idea.