- Home /
Move Object With Angle In 2D
Hello ..
I am Developing 2D game in that I have Scene in x(horizontal)-y(vertical) Axis..
I want to move my Object From Y direction with Some random angle.. But I am not getting idea about how can i translate object with angle..
So please Help me..
Thanks for your support and help..
Answer by raimon.massanet · May 16, 2013 at 07:36 AM
If you want to rotate your object a certain angle, you should do something like this:
void Start () {
// Initially object is facing up (Y-axis)
transform.localRotation = Quaternion.Euler(Vector3.up);
}
public void Rotate(float angle)
{
transform.Rotate(0, 0, angle);
}
No i dont want to rotate my Game Object.. i just want that object to be translated with some angle so it will be some thing like cross direction..
I don't understand what you mean by
translated with some angle
. Do you mean that you want the object to move somewhere using polar coordinates?
You need to define a radius, and calculate the polar coordinates:
private Vector3 currentPosition;
public Vector3 $$anonymous$$rCoordinates(float angle, float radius)
{
// Assu$$anonymous$$g movement is on XY plane
Vector3 relativePosition = new Vector3(radius * $$anonymous$$athf.Cos(angle), radius * $$anonymous$$athf.Sin(angle), 0);
return currentPosition + relativePosition;
}
Then simply translate to the new position.
Hi my 2d character rotate in hit point angle to rotate but character rotate in turn to rotate not a direct rotate
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit = new RaycastHit(); if (Physics.Raycast (ray, out hit, 100)) { Vector3 hitPoint = hit.point;
float dist = Vector3.Distance(hitPoint, transform.position);
Debug.Log(dist);
targetDir = hitPoint - transform.position;
float angle = $$anonymous$$athf.Atan2(targetDir.y, targetDir.x) * $$anonymous$$athf.Rad2Deg;
//transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler (0, 0, angle), 20 * Time.deltaTime);
//Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
//transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * 1);
}
plz help me...
Your answer
Follow this Question
Related Questions
Switching Cameras at runtime 1 Answer
Unity2D move + turn with a fixed angle 1 Answer
A node in a childnode? 1 Answer
2D : Rotation of Object With x-y Axis 2 Answers
Move an object in 2D space along the angle of the object 1 Answer