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 MadJohny · Jun 13, 2014 at 12:20 PM · c#2dlerpsword

Move object forward then backwards

Hi, I've been trying to do this for hours and I just can't get it right, the sword is always going to the same direction, I want it to go forward (local space)on the x axis (in 2d) and then back off to the original position. I really don't know how to describe this problem any better, here's my code though:

 using UnityEngine;
 using System.Collections;
 
 public class MeleeWeaponScript : MonoBehaviour {
     
     public float rotationTime = 15f;
     bool attacking = false;
     Vector2 targetPosition;
 
     public StabForwardVariables stabForwardVariables;
 
     [System.Serializable]
     public class StabForwardVariables {
         public float forwardAmount;
         public float velocity;
     }
 
 
     void Update () {
         if (Input.GetButtonDown("Fire1") && (Vector2) transform.localPosition == Vector2.zero) {
             attacking = true;
             targetPosition = new Vector2(transform.localPosition.x + stabForwardVariables.forwardAmount , transform.localPosition.y);
         }
     }
 
     void FixedUpdate () {
         if (!attacking) {
             transform.localPosition = Vector2.Lerp(transform.localPosition, Vector2.zero, stabForwardVariables.velocity * Time.deltaTime);
 
             var h = Input.GetAxis("HorizontalRight");
             var v = Input.GetAxis("VerticalRight");
             if (Mathf.Abs(h) > 0.05 || Mathf.Abs(v) > 0.05) {
                 var angle = Mathf.Atan2(v, h) * Mathf.Rad2Deg;
                 transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.AngleAxis(angle, Vector3.forward), rotationTime * Time.deltaTime);
             }
         }
         else {
             transform.localPosition = Vector2.Lerp(transform.localPosition, targetPosition, stabForwardVariables.velocity * Time.deltaTime);
             if ((Vector2) transform.localPosition == targetPosition)
                 attacking = false;
         }
     }
 }

Thanks in advance.

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 Ben-Stoneman ♦♦ · Jun 13, 2014 at 03:54 PM 0
Share

So you are basically trying to make a stabbing animation?

avatar image MadJohny · Jun 13, 2014 at 05:50 PM 0
Share

Yup, the reason I don't want to use animations is because I'll have two attacks, and the second will probably be impossible to be done with animations

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by AndyMartin458 · Jun 13, 2014 at 05:54 PM

You are always setting attacking to false, so the if(!attacking) variable is going to be true immediately after you try to move the sword in a direction. You should have an extra boolean for determining when to move the sword backward.

     using UnityEngine;
     using System.Collections;
      
     public class MeleeWeaponScript : MonoBehaviour {
      
     public float rotationTime = 15f;
     bool attacking = false;
     Vector2 targetPosition;
 
     bool movingBackward = false;
      
     public StabForwardVariables stabForwardVariables;
      
     [System.Serializable]
     public class StabForwardVariables {
     public float forwardAmount;
     public float velocity;
     }
      
      
     void Update () {
     if (Input.GetButtonDown("Fire1") && (Vector2) transform.localPosition == Vector2.zero) {
     attacking = true;
     movingBackward = false;
     targetPosition = new Vector2(transform.localPosition.x + stabForwardVariables.forwardAmount , transform.localPosition.y);
     }
     }
      
     void FixedUpdate () {
     if (!attacking && movingBackward) {
     transform.localPosition = Vector2.Lerp(transform.localPosition, Vector2.zero, stabForwardVariables.velocity * Time.deltaTime);
      
     var h = Input.GetAxis("HorizontalRight");
     var v = Input.GetAxis("VerticalRight");
     if (Mathf.Abs(h) > 0.05 || Mathf.Abs(v) > 0.05) {
     var angle = Mathf.Atan2(v, h) * Mathf.Rad2Deg;
     transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.AngleAxis(angle, Vector3.forward), rotationTime * Time.deltaTime);
     }
 
     movingBackward = false;
     }
     else {
     transform.localPosition = Vector2.Lerp(transform.localPosition, targetPosition, stabForwardVariables.velocity * Time.deltaTime);
     if ((Vector2) transform.localPosition == targetPosition)
     attacking = false;
     movingBackward = true;
     }
     }
     }

Comment
Add comment · Show 1 · 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 MadJohny · Jun 13, 2014 at 06:49 PM 0
Share

That broke the script and did not fixed my problem

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

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

Related Questions

Lerp not moving 2D object correctly 1 Answer

Moving Object using Raycast / Lerp C# 2D 0 Answers

Vector3.lerp causes gameobject to move thousands of pixels away from where it's mean to go 1 Answer

Multiple Cars not working 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