• 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
0
Question by mtGameDev · Dec 10, 2012 at 10:09 AM · aim

angry bird aim

hi my friends. one body tell me how I can to make an aim for my 2d game like angry bird ?! thanx.

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
0

Answer by save · Dec 10, 2012 at 10:58 AM

You need to break it down into a couple of small steps. What you want to do is to limit the area of how far you can stretch the object before release, use that area to see where the position of the object is on release and shoot it towards the initial center with physics.

 #pragma strict
 
 /*     Slingshot physics by save
     Description: Put this script on the slingshot object.
     Creation date: 2012-12-10
     Last updated: 2012-12-10
 */
 
 static var power : float = 20.0; //Additional power to shot
 static var radius : float = 2.0; //Radius of clamp circle
 static var sensitivity : float = .2; //Sensitivity of movement
 
 private var myTransform : Transform; //Cached component
 private var myRigidbody : Rigidbody; //Cached component
 private var initPos : Vector3; //Initial position of slingshot object
 private var shot : boolean = false; //Is the object shot?
 private var resting : boolean = true; //Is the object resting?
 
 function Start () {
     myTransform = transform;
     myRigidbody = rigidbody;
     myRigidbody.isKinematic = true;
     initPos = myTransform.position;
 }
 
 function Update () {
     CheckInput();
 }
 
 function CheckInput () {
     if (shot) return;
     if (Input.GetAxis("Fire1")) {
         var inputAxis : Vector2 = Vector2(Input.GetAxis("Mouse X")*sensitivity, Input.GetAxis("Mouse Y")*sensitivity);
         var newPos : Vector3 = Vector3(myTransform.position.x+inputAxis.x, myTransform.position.y+inputAxis.y, initPos.z);
         myTransform.position = CircleClamp(newPos, initPos, radius);
     } else {
         if (!shot && !resting) {
             myRigidbody.isKinematic = false;
             myRigidbody.AddForce((initPos-myTransform.position)*power, ForceMode.Impulse);
             shot = true;
         } else {
             myTransform.position = Vector3.Lerp(myTransform.position, initPos, 10*Time.deltaTime);
         }
     }
 }
 
 function CircleClamp (pos : Vector3, center : Vector3, rad : float) : Vector3 {
     var offset : Vector2 = pos - center;
     var distance : float = offset.magnitude;
     if (distance<rad*.2) {
         resting = true;
     } else {
         resting = false;
     }
     if (rad<distance) {
         var direction : Vector2 = offset/distance;
         pos = center+direction*rad;
     }
     return pos;
 }
 
 @script RequireComponent(Rigidbody)
Comment
Add comment · Show 6 · 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 mtGameDev · Dec 10, 2012 at 11:04 AM 0
Share

thanks ... but can u make it regular ?

avatar image save mtGameDev · Dec 10, 2012 at 11:07 AM 0
Share

Please don't post comments as answers. What makes the script irregular I wonder? :-)

avatar image mtGameDev mtGameDev · Dec 10, 2012 at 11:10 AM 0
Share

now its good. for first time that i see is hasn't code tag so it was irregular and all of word was reel :-D

avatar image save mtGameDev · Dec 10, 2012 at 12:32 PM 0
Share

Great! If you found the script useful I believe it's traditional to upvote, if it was the solution to your problem mark it as correct to help others know that there's a correct answer to the problem.

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

2D Cursor Aiming 2 Answers

Getting the face direction 2 Answers

Pointing 2 empty game objects towards one point? (Raycast?) 1 Answer

addforce question for android? 0 Answers

Airplane Control, Aim ... Lerp and Vectors 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