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!

"Default" layer? You are speaking about layer number 1? Entity detecting colliders are on it's own layer, let's say layer number 19. Okay, let's make it simpler: "Bob want to check if he see John, that's why he throw ray to John's collider. But the problem here is that Bob will hit own collider instead of John's. Bob and John are using the same way to detect each other, they using same looking colliders, on the same layer."

3 Answers

3

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”.

The default layer is 0. You don't need to make it simpler, you need to make sense. Good luck. If the AI is also an enemy, you need to refer to my very first comment. The Raycast will ignore the collider it starts inside.

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.)

Good idea! Didn't think of that.

Sense here is that I need to find fast working solution, which will raycast and ignore given colliders. Thanks alot for your invaluable support!

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));
}

}

I have explained everything you need to know, if you cannot understand it, you need to read the manual and learn the basics first.