• 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
Question by Major · Jun 29, 2014 at 04:03 AM · c#physicsvector3modelsimple

Punching objects (C#)

Contrary to the title I mean taking an object, such as a cube or fist model, and say when you $$anonymous$$t mouse button 1 the fist or cube "punches." I have been trying to figure t$$anonymous$$s out for several hours and I know it should be pretty simple but I don't know what to do. Help is much appreciated, thanks!

Comment

People who like this

0 Show 2
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 robertbu · Jun 29, 2014 at 04:14 AM 0
Share

Punch as in out and back motion. How you do it will depend on your requirements. I would be easy to do with an Animation.

avatar image Major · Jun 29, 2014 at 04:39 AM 0
Share

Yeah out and back motion. I want to do it through script so that it is easier to control.

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by robertbu · Jun 29, 2014 at 05:57 AM

Here is a simple punc$$anonymous$$ng script. Note if you want the punch to interact with Rigidbodies, it would be best to rewrite the manipulation of the transform to use Rigidbody.MovePosition().

 #pragma strict
 
 var punc$$anonymous$$ng = false;
 
 function Update() {
     if (!punc$$anonymous$$ng && Input.GetKeyDown(KeyCode.Space)) {
         Punch(0.5, 1.25, transform.forward);
     }
 }
 
 function Punch(time : float, distance : float, direction : Vector3 ) {
     punc$$anonymous$$ng = true;
     var timer = 0.0;
     var orgPos = transform.position;
     direction.Normalize();
     
     w$$anonymous$$le (timer <= time) {
         transform.position = orgPos + (Mathf.Sin(timer / time * Mathf.PI) + 1.0) * direction;
         yield;
         timer += Time.deltaTime;
     }
     transform.position = orgPos;
     punc$$anonymous$$ng = false;
 }
Comment

People who like this

0 Show 3 · 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 Major · Jun 29, 2014 at 06:58 PM 0
Share

Is it possible for you to write it in C#?

avatar image robertbu · Jul 01, 2014 at 09:23 PM 0
Share
 using UnityEngine;
 using System.Collections;
 
 public class Punching : MonoBehaviour {
 
     private bool punching = false;
     
     void Update() {
         if (!punching && Input.GetKeyDown(KeyCode.Space)) {
             StartCoroutine(Punch(0.5f, 1.25f, transform.forward));
         }
     }
     
     IEnumerator Punch(float time, float distance, Vector3 direction) {
         punching = true;
         var timer = 0.0f;
         var orgPos = transform.position;
         direction.Normalize();
         Debug.Log("Above the loop");
         while (timer <= time) {
             Debug.Log("----");
             transform.position = orgPos + (Mathf.Sin(timer / time * Mathf.PI) + 1.0f) * direction;
             yield return null;
             timer += Time.deltaTime;
         }
         transform.position = orgPos;
         punching = false;
     }
 }
avatar image Major · Jul 02, 2014 at 12:34 AM 0
Share

thanks a lot!

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to apply in opposite direction to transform? 1 Answer

Raycast from an objects co-ordinates 1 Answer

Using Lerp in a Constant Moving Object 1 Answer

Rigidody.velocity = Direction * speed; How to get direction? 1 Answer

Distribute terrain in zones 3 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