Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 dragonking300 · Feb 03, 2017 at 05:29 AM · c#scripting problemscripting beginner

(URGENT) I have a swinging axe I can get to rotate once in 1 direction but i need it to loop

I put urgent in the title because I have a science fair project being presented on Saturday and I have to get this mechanic implemented by the end of Friday or else I have to leave it out.

So back to the swinging axe. I am really new with working with quaternions and I have a modified version of a script I found on unity awnsers that I am trying to use to get my axe to swing in a 180 degree arc in a loop. As I said in the title I have it rotating in 1 direction but I need it to rotate back and loop.

 using UnityEngine;
 using System.Collections;
 
 public class physics : MonoBehaviour
 {
     Vector3 Swing1Condition = new Vector3(0, 0, 499);
     public Vector3 StartingPosition;
     public Vector3 targetAngle = new Vector3(0f, 0f, 90f);
     public float speed;
   
 
     void Start()
     {
         StartingPosition = transform.eulerAngles;
         Swing1();
     }
 
 
     // Update is called once per frame
     void Swing1()
     {
         if (StartingPosition.z <= Swing1Condition.z)
         {
 
             StartingPosition = new Vector3(
                Mathf.LerpAngle(StartingPosition.x, targetAngle.x, Time.deltaTime * speed),
                Mathf.LerpAngle(StartingPosition.y, targetAngle.y, Time.deltaTime * speed),
                Mathf.LerpAngle(StartingPosition.z, targetAngle.z, Time.deltaTime * speed));
             transform.eulerAngles = StartingPosition;
         }
     }
 }

Comment
Add comment · Show 3
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 malkere · Feb 03, 2017 at 06:10 AM 0
Share

By loop you mean what? Back and forth? Or A -> B, A -> B

you should only need to change on axis if that's all you need. you don't really need lerps and things either unless you're trying to smooth the movement on purpose?

just do

transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, transform.eulerAngles.z + (Time.deltaTime * speed));

avatar image dragonking300 malkere · Feb 03, 2017 at 06:47 AM 0
Share

That won't look back and fourth though. I am trying to get my aex to swing in a 180 degree arc

avatar image ForeignGod dragonking300 · Feb 03, 2017 at 07:19 AM 0
Share

Couldn't you just animate it and make it ping-pong? Or does it have to be coded?

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bill9009 · Feb 03, 2017 at 07:51 AM

When you first used my script I forgot to set an amount to resetRate. Any way good luck.

 public class physics : MonoBehaviour {    
     Vector3 Swing1Condition = new Vector3(0, 0, 499);
     public Vector3 StartingPosition;
     private int reset = 1;
     public float resetRate;
     //This is the time before the float reset is changed to -1
     public Vector3 targetAngle = new Vector3(0f, 0f, 90f);
     public float speed;
     private void Start()
     {
         InvokeRepeating("restart", 0f, resetRate);
     }
     //InvokeRepeating constantly actives a method at a given rate of time(resetRate)
     private void Update()
     {
         StartingPosition = transform.eulerAngles;
         Swing1();
 
     }
     void Swing1()
     {
         if (StartingPosition.z <= Swing1Condition.z)
         {
 
             StartingPosition = new Vector3(
                Mathf.LerpAngle(StartingPosition.x, targetAngle.x, Time.deltaTime * speed),
                Mathf.LerpAngle(StartingPosition.y, targetAngle.y, Time.deltaTime * speed),
                Mathf.LerpAngle(StartingPosition.z, targetAngle.z * reset, Time.deltaTime * speed));
             //When I multiply targetAngle.z by -1 it becomes -90
             //This makes the axe go backward
             transform.eulerAngles = StartingPosition;
         }
     }
     void restart()
     {
         reset = reset * -1;
     }
     //I activate this using InvokeRepeating which constantly changes the axe swing
 }

Comment
Add comment · Show 2 · 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 dragonking300 · Feb 04, 2017 at 02:20 AM 0
Share

I tried your script and the axe just stays in place

avatar image dragonking300 · Feb 04, 2017 at 03:06 AM 0
Share

Huzzah! I guess I can include my swinging axe in my game in-time for the science fair <3 . Could you do me a favor though and explain to me how the script works? you can tell me in the comments or put comments in your script :P

avatar image
0

Answer by goutham12 · Feb 03, 2017 at 07:51 AM

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class test : MonoBehaviour {

 Vector3 StartingPosition,targetAngle;

 float speed = 5;


 void Start(){
     StartingPosition = transform.eulerAngles;
     targetAngle = new Vector3 (0, 0, 90);
     StartCoroutine (Swing1 ());
 }

 IEnumerator Swing1()
 {
     while (true)
     {
         float moveProgress = 0;
         Vector3 target = Vector3.zero;
         while(moveProgress < 1)
         {
             moveProgress += Time.deltaTime*speed;
             transform.eulerAngles = Vector3.Lerp(StartingPosition,targetAngle,moveProgress);
             yield return null;
         }

         targetAngle = StartingPosition;

         StartingPosition = transform.eulerAngles;
         yield return null;
     }

 }


}

Comment
Add comment · 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
0

Answer by malkere · Feb 03, 2017 at 09:04 AM

     float speed = 5f;
     bool goingForward = true;
 
     void Update () {
         if (goingForward) {
             Vector3 r = transform.eulerAngles;
             transform.eulerAngles = new Vector3(r.x, r.y, r.z + (Time.deltaTime * speed));
             if (transform.eulerAngles.z >= 90) {
                 goingForward = false;
             }
         }
         else {
             Vector3 r = transform.eulerAngles;
             transform.eulerAngles = new Vector3(r.x, r.y, r.z - (Time.deltaTime * speed));
             if (transform.eulerAngles.z <= 0) {
                 goingForward = true;
             }
         }
     }
 

plain mechanics to expand off of

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 dragonking300 · Feb 04, 2017 at 02:23 AM 0
Share

tried a variation of your script and the axe spins in 360s ins$$anonymous$$d of looping back and fourth in a 180 degree arc

avatar image dragonking300 · Feb 04, 2017 at 02:40 AM 0
Share

forgot to mention your script and how i modified it

 using UnityEngine;
 using System.Collections;
 
 public class physics : $$anonymous$$onoBehaviour
 {
     public Vector3 StartingPosition;
     public Vector3 targetAngle = new Vector3(0f, 0f, 90f);
     public float speed;
     bool goingForward = true;
 
     void Start()
     {
         StartingPosition = transform.eulerAngles;
     }
 
 
     // Update is called once per frame
     void Update()
     {
        if(goingForward)
         {
             Vector3 r = transform.eulerAngles;
              transform.eulerAngles = new Vector3(r.x, r.y, r.z + (Time.deltaTime * speed));
 
             if (transform.eulerAngles.z >= -90)
             {
                 goingForward = true;
             }
             else
             {
                 Vector3 v = transform.eulerAngles;
                 transform.eulerAngles = new Vector3(v.x, v.y, v.z - (Time.deltaTime * speed));
                 if (transform.eulerAngles.z <= 90)
                 {
                     goingForward = false;
                 }
             }
         }
     }
 }
avatar image malkere · Feb 04, 2017 at 08:46 AM 0
Share

Sometime the way unity handles the floats of rotations is unusual, quite a lot of people post problems about it. To be sure I always add if (rotation >= 360) rotation -= 360; else if (rotation < 0) rotation += 360;

that way it's always within 0-360. Hard to gaurentee otherwise

the way you rewrote the code however nestles the second if inside the first. So you ALWAYS are adding to rotation. after that the if statements are being called. I seperated everything into one if and one else

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

297 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 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 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 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 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 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 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 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 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 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 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 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 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 would I make a ListChangedEvent? 1 Answer

Unity 3D: How to make the gameobject speed increase continuously on tapping quickly? 1 Answer

Spawning 3D Objects according to Data at JSON Matrix 0 Answers

How to move the object to where the object is already pointing to? 1 Answer

"Follow Target" script with a prefab (C#) Help me please! 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