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
0
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 hit mouse button 1 the fist or cube "punches." I have been trying to figure this 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
Add comment · 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
0
Best Answer

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

Here is a simple punching 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 punching = false;
 
 function Update() {
     if (!punching && Input.GetKeyDown(KeyCode.Space)) {
         Punch(0.5, 1.25, transform.forward);
     }
 }
 
 function Punch(time : float, distance : float, direction : Vector3 ) {
     punching = true;
     var timer = 0.0;
     var orgPos = transform.position;
     direction.Normalize();
     
     while (timer <= time) {
         transform.position = orgPos + (Mathf.Sin(timer / time * Mathf.PI) + 1.0) * direction;
         yield;
         timer += Time.deltaTime;
     }
     transform.position = orgPos;
     punching = false;
 }
Comment
Add comment · 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 : $$anonymous$$onoBehaviour {
 
     private bool punching = false;
     
     void Update() {
         if (!punching && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.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 + ($$anonymous$$athf.Sin(timer / time * $$anonymous$$athf.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.

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

Using Lerp in a Constant Moving Object 1 Answer

Distribute terrain in zones 3 Answers

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

Multiple Cars not working 1 Answer

Reflect bullets in 3D world space 1 Answer


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