Moving moving Object with left/right arrow keys in a circular direction
Hello, so i want to move an Object which is already constant moving up the Yaxis, but when i press left/right key then it should move to the pressed key direction in a circular way. Basically it should move like a circle from that position it was last. And here is my problem: when I press the left/arrow key it moves in a circular way but not from where my objects was last. It always jumps to the same position(it rotates around position 0,0).
private float timeCounter = 0;
private Vector3 tempPosition;
void Start () {
tempPosition = transform.position;
}
void Update(){
if (Input.GetAxis("Horizontal") == 0){
tempPosition.y += Time.deltaTime;
transform.localPosition = tempPosition;
}
else{
timeCounter += Time.deltaTime * Input.GetAxis("Horizontal");
float x = Mathf.Cos(timeCounter);
float y = Mathf.Sin(timeCounter);
tempPosition = new Vector3(x, y, 0);
transform.localPosition = tempPosition;
}
}
Everything is done in 2d and the script is attached to the object.
Thanks in advance!
Answer by Nomenokes · Feb 11, 2017 at 06:47 PM
My first inclination, if I understand you correctly, is to store a vector with the origin you want to rotate around, then add that point to your temp position.
Vector2 origin;
float radius;
//define these in some other place when the character starts moving in a circle
else{
timeCounter += Time.deltaTime * Input.GetAxis("Horizontal");
float x = Mathf.Cos(timeCounter)*radius+origin.x;
float y = Mathf.Sin(timeCounter)*radius+origin.y;
tempPosition = new Vector2(x, y);
transform.localPosition = (Vector3)tempPosition;
}
This would keep the player rotating around an origin specified with a radius "radius".
Answer by Glurth · Feb 11, 2017 at 08:02 PM
Another option is to use an empty gameobject to define the center-point of the rotation with it's position, and make the object-to-be-rotated-in-a-circle a child of this.
Now, you can change the transform.rotation component of this parent object, to rotate the child object around the center-point.
The most noticeable difference between this and the method Nomenokes suggested is: using this method the same face will always point towards the center. In other-words, the object itself will rotate once per "orbit", like the moon around the earth (tidally locked). (Of course, an additional operation to the objects rotation, could change this, for either method.)
Answer by manirana94 · Mar 26, 2020 at 06:03 PM
Here you go.
using System; using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour {
private float _timer = 0;
private float _angle = 0;
public float _speed = 0.02f;
public float radius = 2;
private Vector3 rotationAngles;
private float rotationSpeed;
public GameObject Center;
private void Awake()
{
transform.position = new Vector3(0, (Center.transform.position.y + radius), 0);
}
void Start()
{
}
void Update()
{
if (Input.GetAxis("Horizontal") != 0)
{
MoveCircle();
}
}
void MoveCircle()
{
_timer += (Time.deltaTime) * Input.GetAxis("Horizontal") *_speed;
_angle = _timer;
transform.position = Vector3.MoveTowards(transform.position, new Vector3((Center.transform.position.x + Mathf.Sin(_angle) * radius), (Center.transform.position.y + Mathf.Cos(_angle) * radius), ((Center.transform.position.z))), 0.35f);
transform.Rotate(new Vector3(rotationAngles.x * rotationSpeed * Time.deltaTime, rotationAngles.y * rotationSpeed * Time.deltaTime, rotationAngles.z * rotationSpeed * Time.deltaTime));
}
}
Your answer
Follow this Question
Related Questions
SImple, but how do I check a game object's position in an If statement? 1 Answer
Move UI with / corresponding Game Object? 1 Answer
How do I move a game object only on the x axis with arrow keys? 1 Answer
How to make an object not move through an wall using transform.translate? 3 Answers
2D AI movement 0 Answers