unity2D Moving to a fixed object causes shaking - unity3d

Please pay attention to the gif:
There are two objects.
left circle object = circle colider2D + rigidbody2D(freeze positionY, freeze Rotation Z, script for move )
Script for move:
void Update()
{
if(Input.GetAxisRaw("Horizontal") > 0){
transform.Translate(Vector2.right * 2f * Time.deltaTime);
}
}
right square object = square colider2D + rigidbody2D(freeze positionY, freeze positionX, freeze Rotation Z)
Moving the left object to the right, when two objects meet, The left object shakes to the left and right. I don't know why.
I hope the two objects do not overlap.
How can I prevent the problem?

You are using Transform to move non kinematic rigibodies, you should not. Either use AddForce() to move your objects, or use kinematic rigidbodies and Translate but that will require a LOT more work as you will need to manage collisions by yourself if you want something clean and smooth.

Related

Rotating player is messing up my movement

I wrote a simple player movement script which moves my player like this:
private void MovePlayer()
{
// Initialize Directions For Player Movement
movement = transform.right * horizontal * playerSpeed + transform.forward * vertical * playerSpeed;
// Move Player
rb.AddForce(movement, ForceMode.Acceleration);
}
and I am trying to rotate my player towards the axes, for example if the player is pressing a the horizontal will be -1 and I want to rotate my player left horizontal * 90f, now when I try this, my horizontal axis is acting like my vertical, if I press A it will bring my player backwards, if I press D it will do the same thing, this is how I rotate the player:
// buggy code:
private void RotatePLayerTowardsAxis()
{
// Rotate PLayer Horizontaly
transform.rotation = Quaternion.Euler(0f, horizontal * 90, 0f);
}
is there a way I can do this?
Edit:
The vertical is still pushing me up and down.
First, it is important to check if you are looking at the scene in Local or Global view. Check your axes in Local mode:
If your character model faces towards the blue axis, then everything should be okay.
If you want to rotate your character only by 90 degrees all the time, then it can be done somehow like this:
transform.rotation = Quaternion.Euler(0, 90, 0);
You can use the following as well, but this might not fit your needs:
transform.Rotate(transform.up, 90);
This will make your character rotate by 90 degrees AROUND the up axis. If you want it to rotate always around the world up axis:
transform.Rotate(Vector3.up, 90);
However, transform.rotation = Quaternion.Euler(0, 90, 0); might be better for you, as it always "resets" the rotation when you overwrite it. This will make the character fixed to a direction. So if you press left, the charater will always look into the left by 90 degree. And if you press right after the left turn, your character will do a 180° turn instead of looking forward. I hope you get what I'm trying to say. This means, that your character will be bound to the world axis instead of its own. To modify this (if you want to), write something like this:
transform.rotation += Quaternion.Euler(0, 90, 0);
Hope I could help!

unity2D Moving to a fixed object causes weightlessness

Please look at the photo. There are two objects.
left circle object = circle colider2D + rigidbody2D(freeze Rotation Z, script for move )
Rigidbody2D rigid;
float moveX;
void Start()
{
rigid = gameObject.GetComponent<Rigidbody2D>();
}
void PlayerMove(){
moveX = Input.GetAxisRaw("Horizontal");
rigid.velocity = new Vector2(moveX * 5f, rigid.velocity.y);
}
void Update(){
PlayerMove();
}
right square object = square colider2D + rigidbody2D(freeze positionY, freeze positionX, freeze Rotation Z)
I can move the circle from side to side.
While I press the keyboard and push the circle to the right wall, gravity doesn't work.
I don't know why. I hope gravity will work even if the circle hit the wall.
How can I prevent the problem?
Looks like physics material that's applied to that rigidbody has too much friction and slows down too much when hugging the wall. Try reducing friction value on the material (you can create the physics2D material from the assets panel).

Move Object by dragging (mobile) makes the balls fall from the cup

I have this cup that contains balls inside. All 2D
I am using Rigidbody2d and Collider2d.
When running in unity and moving the cup (with arrow keys) the balls stay inside the cup. I also added drag movement for Android touch to move the cup.
The problem is that when moving the cup too fast (by draging) the balls fall from the cup collider (using Polygon colider 2d).
Code for movement is:
public float speed = 0.1F;
void Update() {
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
transform.Translate(-touchDeltaPosition.x * speed, -touchDeltaPosition.y * speed, 0);
}
}
I tried to play with the speed parameter but it wont really help. if the cup movement is too slow it is not very useful for me.
I believe it is related the velocity/force of the ball or cup which makes the cup collider miss...
Any help on this would be appreciated greatly!
it is because you are moving it by changing the position of the cup, that means that when you move it fast, it disappears, and reappears where you dragged it, leaving the balls behind, and then they fall. I had the same thing where my objects would just go through the walls and out of the camera area. I fixed that by using AddForce. This makes the cup move, position by position over to where you dragged. this bumps the balls along, and they stay in the cup. This is what I guess is going on. If you use velocity that would work to.
you could rather than transform.Translate, you could find the vector to where you want to move to.
var direction = touchDeltaPosition - transform.position;
gameObject.rigidbody2D.velocity.x = direction.x * speed;
gameObject.rigidbody2D.velocity.y = direction.y * speed;
hope this solves it
The problem is that, when you're moving the cup too fast, at Frame 1 the ball is inside the cup, but at Frame 2 it's outside. You can reduce the value of "Fixed Timestep" (http://docs.unity3d.com/Manual/class-TimeManager.html) to increase the frequency of the physics calculations, or make the colliders larger.
If a ball in the cup will always stay in the cup, maybe you can turn off the physics of the ball once it's in the cup, or something along those lines.
Usually transform.Translate() is not very efficient when dealing with colliders, instead, you can try three other solutions:
1) rigidbody2D.AddForce(Vector2.Up * Input.GetAxis("Vertical"));
2) rigidbody2D.velocity = new Vector2(//write what you need);
3) which I consider the best solution for dealing with colliders and dragging is : MoveRigidbody2D
Well, this is what worked for me:
var newVec = new Vector2 (transform.position.x, transform.position.y);
var touchDeltaPosition = Input.GetTouch(0).deltaPosition*touchSpeed;
var direction = touchDeltaPosition- newVec;
rigidbody2D.AddForce(direction);

influence of the wheels on the vehicle movement

I'm new in Unity. I want to do that wheels (cylinders) in this car will turn around if I press "up" button on my keyboard.
This is the code I've written:
var forwardSpeed: float = 3;
function Start () {
}
function Update () {
var forwardMoveAmount = Input.GetAxis("Vertical")*forwardSpeed;
transform.Rotate(0, forwardMoveAmount, 0);
}
OK. Wheels are turning around, but my car is still at the same place. What should I do to move this car?
PS: can you explain me, why this cylinder rotates correctly, when I use Y axis? It should be z.
For physics based wheels, you probably want wheel colliders. Their use is pretty well documented here:
http://docs.unity3d.com/Documentation/Components/class-WheelCollider.html
You're applying the distance scalar to a rotate function, and not translating (Moving) the object.
transform.Rotate will rotate the object.
transform.Translate will move the object.
Pick an object on your desk. If you rotate it 90 degrees twice, it will be rotated 180 degrees but still be in the same place. Now imagine every time you rotate that object, you move it in the direction it's facing by a couple inches. After 4 cycles, the object will have completed a full circuit of 360 degrees.
To represent this in code:
var forwardSpeed: float = 3; // Tweak me
var turnAngle: float = 1; // Tweak me
function Start () {
}
function Update () {
// Rotate first
transform.Rotate(0, Vector3.right * turnAngle, 0);
// Move forward along the rotated axis
transform.Translate(0, Vector3.forward * forwardSpeed, 0);
}
You'll need to also explicitly move the car at the same time, using presumably transform.position, transform.Translate(), or something similar on the parent car object.

Elliptical movement of a rigidbody (as in whale trail game) by applying force in unity using c#?

I want to move a rigid body in an elliptical movement like the player movement "whale trail" game.What I did is:
Created a Cube called "Player" with scale(1.5,0.5,0.1)
Created another small cube called Point with scale(0.1,0.1,0.1) and positioned same as Player but 0.5 more in x (So that now the player looks like a 2D rectangle and a point on it little right to the centre of the rectangle).
Then I created a fixed joint between both bodies
Now I applied for on the player at the position of the Point as follows ,
float mfAngle = 0.0f;
void update()
{
mfAngle=transform.eulerAngles.z;
mfAngle=mfAngle%360;
if(mfAngle>=0 && mfAngle<90)
{
mfXforce=-0.1f;
mfYforce=0.1f;
}
if(mfAngle>=90 && mfAngle<180)
{
mfXforce=-0.1f;
mfYforce=0.1f;
}
if(mfAngle>=180 && mfAngle<270)
{
mfXforce=-0.1f;
mfYforce=-0.1f;
}
if(mfAngle>=270 && mfAngle<360)
{
mfXforce=0.1f;
mfYforce=-0.1f;
}
Debug.Log("Angle ="+mfAngle+"X = "+mfXforce+"Y = "+mfYforce);
Vector3 pointPos=_goPointObject.transform.position;
transform.rigidbody.AddForceAtPosition(new Vector3(mfXforce,mfYforce,0),pointPos);
}
But it doesn't works fine.I just moves upwards and the turns and moves in different direction.If anyone know how to move the rigid body in elliptical motion by applying force give me a solution.(I want to use it as like whale trail game u can see the video of the "loop movement" in this http://www.youtube.com/watch?v=wwr6c2Ws1yI video).Thanks in advance.
I had found the solution by myself.To achieve that elliptical movement u have to create two bodies and connect them with joint by placing it horizontally next to each other.Then u have to apply force in x direction constantly and in Y direction only when the Screen is be touched on the first body.
And its important to reduce the X speed when moving up then u will get that elliptical rotation.
It works fine for me.
Thanks,
Ashokkumar M