Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by santoshadari · Feb 09, 2013 at 12:21 PM · scriptableobject

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image santoshadari · Feb 10, 2013 at 10:22 AM 0
Share

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.

avatar image SBull · Jul 08, 2013 at 06:24 PM 0
Share

Is there any way to do this exact same thing using a characterController ins$$anonymous$$d of a rigidbody?

avatar image aldonaletto · Jul 09, 2013 at 02:39 AM 0
Share

$$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
   } 
 }
avatar image SBull · Jul 09, 2013 at 03:09 AM 0
Share

Thanks! I got it to work with the characterController.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to add a cooldown to a move. 1 Answer

How to make my game objects spawn when i walk through collider 3 Answers

How Do I Make A Health Bar 6 Answers

Procedurally generating terrain? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges