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 The-Little-Guy · Aug 03, 2014 at 10:35 PM · 2drotationmovement

Rotate while moving forward

I have the following code, it makes object randomly wonder on the screen. What I am having issues with, is when an object looks at the point it is going to, I would like it to rotate as it moves forward. What is happening now, is it rotates instantly to the point it is going towards. So, how can I get it to rotate slowly and move forward at the same time?

 using UnityEngine;
 using System.Collections;

 public class Wonder : MonoBehaviour {

     protected Vector2 wayPoint;
     protected float speed;

     // Use this for initialization
     void Start () {
         speed = gameObject.GetComponent<Move>().playerSpeed;
         wonder();
     }

     void wonder(){
         wayPoint = Random.insideUnitCircle * 10;
     }
     
     // Update is called once per frame
     void Update () {
         Vector2 dir = wayPoint - new Vector2(transform.position.x, transform.position.y);
         float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.Euler(new Vector3(0, 0,Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg - 90));
         transform.position = Vector2.MoveTowards(transform.position, wayPoint, Time.deltaTime * speed);
         float magnitude = (new Vector2(transform.position.x, transform.position.y) - wayPoint).magnitude;
         if(magnitude < 3){
             wonder();
         }
     }
 }

Here is an example:

Ship Rotation

So, once the ship gets to its point another will be created and it will move there. I am thinking I will have to have a list of 5+ points, then calculate the arch the ship needs to take adding new points as the ship hits a way point then removing old ones after. I am not sure how to do this though...

shiprotation2.jpg (64.2 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Aug 03, 2014 at 10:40 PM

This changes Update() will rotate over time. I don't know what the -90 is all about. I'd remove it and fix the texture direction on the sprite in Photoshop. You'll need to declare and initialize the 'rotateSpeed' variable.

 void Update () {
     Vector2 dir = wayPoint - new Vector2(transform.position.x, transform.position.y);
     float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - 90.0f;
     Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
     transform.rotation = Quaternion.RotateTowards(transform.rotation, q, Time.deltaTime * rotateSpeed);
     transform.position = Vector2.MoveTowards(transform.position, wayPoint, Time.deltaTime * speed);
     float magnitude = (new Vector2(transform.position.x, transform.position.y) - wayPoint).magnitude;
     if(magnitude < 3){
         wonder();
     }
 }
Comment
Add comment · Show 4 · 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 The-Little-Guy · Aug 03, 2014 at 10:47 PM 0
Share

That helps, but when they rotate and move to the wayPoint, the ships are flying backwards/sideways for a short period of time. Is it possible to make them go forward as they turn?

avatar image The-Little-Guy · Aug 03, 2014 at 11:58 PM 0
Share

I added an image to my question.

avatar image robertbu · Aug 04, 2014 at 12:27 AM 0
Share

There are two approaches to get what you want. The first (and seldom used) is to calculate a spline between the points with the initial angle of the ship used as a control for the curve. Sometime in the last six months, a similar question was asked, and I believe the code to solve the issue was posted.

The more common approach is to change the movement code, so that the object moves based on its local forward (the way it is facing). For example, your movement code could be:

  transform.Translate(0,0,Time.deltaTime * speed);

Or:

  transform.position += transform.forward * Time.deltaTime * speed;

The problem with this solution is 1) the ship never lands right on the end point without some specialized code...it will pass it by since Time.deltaTime is discrete. And 2) if the distance is too short with respect to the ships initial angle, then it will circle destination rather than heading directly towards it. Both can be solved in a variety of ways. I'd start by establishing a distance between the object that you consider 'arrived' rather than an exact match in positions.

avatar image robertbu · Aug 04, 2014 at 12:54 AM 0
Share

Note one implementation of the spline solution is to use iTween and move the object along the path. It depends on what your goals are here. Edit: I bumped into SeekSteer, which you might be interested in.

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

2 People are following this question.

avatar image avatar image

Related Questions

how to make an object move towards it's rotation a set amount as a child in 2d 0 Answers

Help with rotating a sprite with mouse. 2 Answers

How do i rotate my player in moving direction in 2d? 0 Answers

Rotating my character only while moving in a 2D plane 3 Answers

How do I make a 2D object face the direction it is moving in top down space shooter 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