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 Hiilo · Sep 24, 2014 at 08:21 PM · anglewave

sine movement at any angle

I'm working on a 2D game. I have enemy spawn points at all directions around the viewport. All the enemies will pass through the centre of the viewport. What I'm trying to do is make some enemies move in a sine wave. But the enemies could come from any spawn point. Here's an image that hopefully will explain what I'm trying to do. Different colours mark some of the wished paths. alt text I have done some research but haven't found a solution yet. The closest I came was the solution described here. http://answers.unity3d.com/questions/305484/sine-movement.html That works fine in one direction.

Here's my current code

 using UnityEngine;
 using System.Collections;
 
 public class SineMove : MonoBehaviour {
 
     private GameObject center;
 
     private Vector3 difference;
 
     public float CurveSpeed = 5;
     public float MoveSpeed = 0;
     public float fTime = 2;
 
     Vector3 vLastPos = Vector3.zero;
     
     // Use this for initialization
     void Start () 
     {
         center = GameObject.FindWithTag("CenterTarget");
         difference = transform.position - center.transform.position;
 
     }
     
     // Update is called once per frame
     void Update () 
     {
 
         fTime += Time.deltaTime * CurveSpeed;
         
         Vector3 vSin = new Vector3(Mathf.Sin(fTime), -Mathf.Sin(fTime), 0);
         Vector3 vLin = new Vector3(MoveSpeed, MoveSpeed, 0);
         transform.position -= (vSin + vLin) * Time.deltaTime;
 
         //Tried using difference but it didn't work
         //transform.position -= (difference+vSin) *0.2f*Time.deltaTime;
 
                 //vLastPos Is only for Debug.Draw
         vLastPos = transform.position;
         Debug.DrawLine(vLastPos, transform.position, Color.green, 100);
     }
 
 }
 

Any help much appreciated.

rotatedsine-01.png (16.7 kB)
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 Scribe · Sep 24, 2014 at 09:45 PM 0
Share

$$anonymous$$y answer here might help! http://answers.unity3d.com/questions/266592/implement-the-equation-as-a-code.html

on my phone so sorry for bad formatting or whatever happens when I press comment :D

avatar image Hiilo · Sep 25, 2014 at 05:41 PM 0
Share

Thanks for the link. I went with the animation option for now, but will try out your solution in the other question later.

2 Replies

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

Answer by N1warhead · Sep 24, 2014 at 09:10 PM

Or just create a basic animation that plays when the enemy is charging in that direction. At least, that's what I'd do.

If you'd like to do that, then go to Window or whatever at the top of Unity and click Animation. Very easy to learn, I learned it without even finding help, at least with making the Animation at least.

Hope that helps.

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 Habitablaba · Sep 24, 2014 at 09:15 PM 0
Share

Probably a lot simpler / less code to change to make it work this way!

avatar image N1warhead · Sep 24, 2014 at 10:14 PM 1
Share

Oh it is. If you convert the Animation to legacy.. All it will take is like 1 or 2 lines of code to play the animation. I like to try to keep something as simple as possible.

avatar image Hiilo · Sep 25, 2014 at 05:39 PM 1
Share

Thanks for the answer. It works for now. What I did was I animated the y position of my enemy with 4 keyframes. So it's for example y=0; y=4; y=0; y=-4; And then I made my enemy gameobject a child of a empty gameobject and use script to move that object along the path i want. I also use script to make the parent gameobject to look towards the target position. It's a bit work to adjust the step length and amplitude but it works.

avatar image
1

Answer by Habitablaba · Sep 24, 2014 at 08:51 PM

What you have here is an issue with local space vs world space.

First of all, what I would suggest is to change from setting the position directly, to using transform.Translate and transform.Rotate.

  • calculate the (vertical) sine wave movement in local space

  • calculate the forward movement in local space

  • create a vector3 based on the above

  • use transform.Translate to apply the movement to the enemy

  • use transform.Rotate to apply rotation of the enemy

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

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

28 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

Related Questions

Apply force at an angle or transform.rotation 1 Answer

Need a little help with quick angle equation 2 Answers

Calculate vector3 from angle 2 Answers

Rotating A Character 180 Degress 1 Answer

Calculating a Second Set of Normals 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