I've got a plain 2D Capsule Sprite. I want it to rotate around a specific point instead of its very centre. In other programs, this point is called the anchor point. For context, I'm making a paddle for my 2D pinball game so obviously the paddle needs to move when you press a button, but I can't have it moving around its very centre... Well, I could but I don't want to.
So my questions are:
Is anchor point the correct term for Unity?
Can this be altered/moved and how?
So, I ended up getting an idea from a friend who does this stuff too.
I made an empty game object. Made it the parent of my paddle. Moved the paddle to the point of the game object where I want the paddle to rotate around, and now when I rotate the game object, it rotates the paddle around the pivot point that I want it to have.
Image of paddle, arrow pointing to the empty game object
Edit: Just works using UI components within a canvas
You can use the Anchor presets of the Rect Transform. Or define the Min/Max values for x/y coordinates in Anchors Property.
Related
I used to have a game object used as a sword with sprites with a specific size, despite including quite a bit of transparent space, to ensure these are aligned properly with the player game object:
This ensures the sword follows the player as he jumps by making use of the hero transition like this transform.position = hero.transform.position; , and though there may be issues with sprite changes I would address these later.
However, since I want to have several different equipment, and other sprites of this same sword might need a bigger dimension to look good (such as a sword attack while standing on the ground), I could either make even bigger sprites which would eventually affect performance due to transparent pixels loading, or I thought of making sprites with specific sizes:
(if this works I'd make sure to draw and put them close together instead of being separate)
And although when I prepare the animation I make sure to shift the position of the new sword to where it would be based on the player sprite on its own air attack animation (thus I had to modify this frame by frame,
The sword doesn't seem to follow the player, even when its game object still uses the script that makes use of the player's transform position:
I'm assuming something else has to be changed frame by frame, but what could it be? Is there a way to align or anchor a smaller sprite to follow the pivot of a bigger sprite?
All rotation or changing of sprites is done relative to the sprite's Pivot Point.
When you currently swap your sprites, your sword looks like it is rotating on it's blade rather than the handle.
Change the Pivot point to the handle, and it will do most of the work.
The rest is just making sure the handle of the sword follows the character's hands.
I am trying to move forward like this
float speed = 40;
this.transform.Translate(0,0,speed/50);
It works well when character's face is looking horizontally(y=0)
However when character's face is looking down,
Somehow, It goes down through the terrain.
Is there any good way to move horizontally even character's face is looking down??
If the GameObject the script is attached to is passing through the terrain (to find out look in the scene view with the object selected and see if its position is inside the terrain):
transform.Translate moves the position of a GameObject in the direction specified even if there is an object with a collider in the way such as terrain.
If you want to move an object with collision detection you should think about using a Rigidbody or CharacterController.
If the camera is seeing through the terrain even though the camera is not inside the terrain (to find out look in the scene view with the camera selected and see if its position is inside the terrain):
It is possible that the value of the camera's near clipping plane is too large. The camera's clipping planes tell Unity at what distances to start and stop rendering geometry. The far clipping plane tells Unity at what distance away from the camera to stop rendering geometry and the near clipping plane tells Unity at what distance from the camera to start rendering geometry. Any geometry within the two values is rendered and any geometry outside the two values is not. To stop the camera seeing through the terrain you could try lowering the value of the near clipping plane, to have geometry closer to the camera rendered or move the camera further away from the terrain.
I am currently working on a unity project and I have a stickman built by Hinge Joints but when I animate his legs and activate the animation, the legs stays at the same position and the rest of the body is moving forward.
How can I animate the legs without freezing it's position?
This worked for me:
Anchor and Axis should be correct, if it works with a motor.
For testing, deactivate "Use Spring", "Use Limits" and "Use Motor".
If it still doesn't move, you should have a look at the rigidbody. "Is Kinematic" should be deactivated as well.
From here
A lot of time passed since I asked this question and when I read this question again I figured out that my mistake was animating the legs without a parent.
The legs hadn't a parent so their positions where the animation position.
But if I had setted the legs as children of an empty game object, their positions were relative to the empty game object, which I could move as I wish.
Sorry I know the title is really confusing.
Basically the camera and hands are nested under an empty object. This empty object is then used to rotate the character and teleport the character. Thing is, the camera is able to freely move around and away the empty object. Making rotation seem like you're running in a circle instead of just rotating. Then when you teleport it's a little offset. Like if you shoot the teleport beam straight down you move because it's teleporting the empty object to the camera which moves the camera.
So if the headset and camera aren't exactly on top of the empty object everything gets wonky. I understand the problem just can't figure out a solution.
I tried resetting the position on every teleport but you can still move away and spin in circles. As well as resetting resets the orientation and that's not exactly what I need.
Any help would be greatly appreciated.
There's a few ways to approach this. A crude, and not very elegant solution would be to parent your camera parent to yet another object, created in runtime, that would assume the position of the actual player camera when the rotation starts. You can reparent objects dynamically, so you could rotate this newly spawned object, which would give you the desired effect, and then loose the top parent when the rotation stops.
Alternatively you could move your camera parent as you rotate it, based on transforming the position relative to the player camera position. A bit more elegant but there's probably a few lines of math that you would have to write
I am working on a tabletop game that user controls using accelerometer in the phone. It is pretty similar to Labyrinth on iOS.
The scene contains a tabletop created using planes and cubes, and a sphere as a ball. As it works in Labyrinth game, on tilting phone ball should drop to the tilted side, while the camera stays centered to table. I am trying to do similar thing where user tilts the phone, and objects on table move to tilted side.
Currently, I add the x and z component to Physics.gravity on tilt. Sadly, this change in gravity does not affect the ball which stays put on the table. Use gravity is selected for the ball, and it drop down from height to the tabletop initially and then comes to halt. After the initial drop, ball does not react to any gravity change.
I have also tried rotating the whole table, but using transform.rotate does not work either. The table rotates perfectly, alongwith the camera, but the ball stays put hanging in the air. So, currently I am out of my depth about the issue.
Is there any other way to allow tilt action registered, so that ball moves to the tilted direction? I cannot use addforce function, as there are multiple objects which need to react to tilt, and it will be difficult to keep track of that many addforce calls. How else can I get it working?
See this post for some related information.
As for why your sphere is sticking to the table, can I assume your sphere has a rigidbody? If so, you need need to wake it up:
if (rigidbody.IsSleeping())rigidbody.WakeUp();
Ideally you would make that call only after detecting a change in orientation/gravity, not every frame.