- Home /
How can i get rubber effect using unity 4
As i am new to unity i want to design a game similar to angry birds i need support that how can get elastic effect using unity. I want the effect as shown in this video "http://www.youtube.com/watch?v=HYF7gyaAGT0" I heard about procedural joints if the above video deals with the same concept can anyone clearly explain how can i work with procedural joints.
Answer by aldonaletto · Feb 10, 2013 at 04:19 AM
You don't need any sophisticated elastic effect for a game like Angry Birds: simply save the position where you pressed the mouse button, drag the bird while the button is pressed, and accelerate the bird in the direction (start position - end position) when released.
That's an example:
var bird: GameObject; // drag the bird object here
var force: float = 10.0;
var stretchSound: AudioClip;
var shotSound: AudioClip;
private var startPoint: Vector3;
private var drag = false;
function Update(){
// get mouse pointer in world space:
var pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
pos.z = 0; // make sure it's at Z = 0
if (drag)){ // drag bird while button pressed
bird.transform.position = pos;
}
if (Input.GetMouseButtonDown(0)){ // mouse button pressed:
drag = true; // start dragging
startPoint = pos; // save initial position
bird.rigidbody.isKinematic = true; // deactivate bird physics
if (stretchSound){ // start stretching sound, if any
audio.clip = stretchSound;
audio.Play();
}
}
if (Input.GetMouseButtonUp(0)){ // mouse button released:
drag = false; // stop dragging
bird.rigidbody.isKinematic = false; // activate bird physics
var dir = startPoint - pos; // calculate direction and intensity
bird.rigidbody.velocity = dir * force; // apply to the rigidbody
if (shotSound){ // play shot sound, if any
audio.clip = shotSound;
audio.Play();
}
}
}
This script may be attached to the camera (add an AudioSource to the camera). The camera must be in orthographic mode and with rotation (0,0,0), and the bird must be a rigidbody with Is Kinematic checked and constraints set to freeze Z position and X,Y rotation. The action will happen at Z=0, thus you may place the slingshot a little ahead (Z = -1, for instance) and the scenery behind (Z = 10, for instance). If you don't have the appropriate sounds, download them from here: stretch sound and shoot sound.
Hi thanks for your answer i got this effect but i want to see the elastic effect in my game. I want my game player to drag the the rubber back.
Is there any way to do this exact same thing using a characterController ins$$anonymous$$d of a rigidbody?
$$anonymous$$aybe this can be done with some changes in the code (not tested):
var bird: GameObject; // drag the bird object here
var force: float = 10.0;
var gravity: float = 10.0;
var stretchSound: AudioClip;
var shotSound: AudioClip;
private var startPoint: Vector3;
private var drag = false;
private var velocity: Vector3 = Vector3.zero;
private var controller: CharacterController;
private var active = false;
function Update(){
// get mouse pointer in world space:
var pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
pos.z = 0; // make sure it's at Z = 0
if (drag)){ // drag bird while button pressed
bird.transform.position = pos;
}
if (Input.Get$$anonymous$$ouseButtonDown(0)){ // mouse button pressed:
drag = true; // start dragging
startPoint = pos; // save initial position
active = false; // disable physics emulation
velocity = Vector3.zero; // clear momentum
if (stretchSound){ // start stretching sound, if any
audio.clip = stretchSound;
audio.Play();
}
}
if (Input.Get$$anonymous$$ouseButtonUp(0)){ // mouse button released:
drag = false; // stop dragging
active = true; // activate physics emulation
var dir = startPoint - pos; // calculate direction and intensity
velocity = dir * force; // apply to the rigidbody
if (shotSound){ // play shot sound, if any
audio.clip = shotSound;
audio.Play();
}
}
if (active){ // if physics emulation enabled...
// get reference to CharacterController only once:
if (!controller) controller = GetComponent(CharacterController);
velocity.y -= gravity * Time.deltaTime; // apply gravity
controller.$$anonymous$$ove(velocity * Time.deltaTime); // move
}
}