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).
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."
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.)
"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."
– anon89471846