- Home /
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:
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...
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();
}
}
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?
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.
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
Follow this Question
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