How can I rotate an object that is falling and pulled? - c#

I have balls falling (gravity) in my game, and in its Update method I've also added a slight pull, as the wind was blowing:
transform.Translate(new Vector3(sidewaysDrag,0));
Now, I want to rotate my GameOBject around the Z axis as well (its a 2D game), ive tried:
transform.Rotate(Vector3.forward * Time.deltaTime * 200);
But I guess my anchor is wrong, because instead of rotating around itself the whole object is rotating in a very big curve. The rotation is correct if I comment out the translation...

Put your ball object under another empty object(parent).
And translate parent, rotate ball.
or translate ball's world position, rotate ball's localRotation

It's because you are using Vector3.forward, which actually points to Z-axis in world space, you need Z-axis in local space, you can get that by doing transform.TransformDirection(Vector3.forward);

Related

How to make ball bounce

I have a game where u bounce a ball with platform and u have to destroy all boxes but my code for bounceing ball its not working like I want.
It works like a real ball bounce but I want the ball to bounce like a ball in pong where ball don't fall down until it touches wall or platfotm but my ball goes in real life curve.
What I thing with curve is like when u kick a ball in the air and goes in curve but I want it to go unlimitly in flat line to the wall andbounce in a random direction.
Someone have any code ideas?
Make a physics material like the one below and place it on any surface that bounces. It is enough for your ball to have a Rigidbody with drag 0.
Also uncheck gravity to get the following result:
Basic Force Script:
After adding Rigidbody to the ball, you can use the following script for the initial force. Obviously you need more advanced algorithms for further development.
public Vector3 force = new Vector3(80, 40);
void Start() => GetComponent<Rigidbody>().AddForce(force);
Pong/breakout style physics is simpler than you might think.
The balls have no gravity applied to them, so all you need to do is apply a constant velocity to the ball.
When it collides with an object, you would flip the velocity on the axis it collided.
For example:
ball is travelling up at 60px/sec, and horizontally at 120px/sec, at 60fps.
You add (60/60)=1 pixels vertically every frame, and (120/60)=2 horizontally.
It hits a wall, so you flip the horizontal velocity to -120px/sec.
Now it's still going up, but in the opposite direction horizontally.
If it hits a ceiling, same thing but flip the vertical velocity component.
If you'd rather it go in a random direction:
calculate the magnitude of the ball's velocity sqrt(vx^2+vy^2)
find the angle the wall is facing
pick a random angle within 180 degrees of that angle
use trigonometry to get the x/y components of that angle, and multiply by the magnitude.
Don't subtract/add a constant value to the velocity every frame, because then you'd be applying gravity, which isn't what you're looking for.

Unity CharacterController always moves in the same direction independent from rotation

If i'm rotating my player on the Y-Axis and move it like
transform.Translate(Vector3.forward * Time.deltaTime)
it will move along the Z-Axis of the character (as expected).
If I used CharacterController.Move it will always move in the same direction, independent from its rotation.
How can I make the player follow it's rotation?
The problem is that you are translating the character forward on a global level. This means forward will always be in the same direction. Instead, use transform.forward. That way, you are translating the player on the forward vector on the player.
Another way to do it is using transform.rotation * Vector3.forward. This will rotate the global forward by the rotation of the player, returning the same thing as transform.forward.

Rotating an object to face a certain point, but only on one axis

I have objects that I want to rotate towards a collision point, but only on the Y-axis. Here's what I mean:
The arrows show which direction the balls hit the pins.
First
The object rotates on it's Y-axis to face the collision point.
Second
I suspect what I need is to convert the Vector3 of the collision point into rotational degrees, that can then be plugged into the object's Y axis. I have no idea how to do this, or if it's even possible. Can anyone help me?
Why don't you simply use Unity's LookAt function like this:
transform.LookAt(new Vector3(Target.position.x, transform.position.y, Target.position.z));

Off-center rotate object in Unity

I have a laser turret in Unity3D, which I'd like to turn towards the enemies. The turret consists of a "leg" and a "head" (selected on the picture 1). The head can pan and tilt around a spherical joint.
I do the following:
Vector3 targetDir = collision.gameObject.transform.position - turretHead.transform.position;
float step = turnSpeed * Time.deltaTime;
Vector3 newDir = Vector3.RotateTowards(turretHead.transform.forward, targetDir, step, 0.0f);
turretHead.transform.rotation = Quaternion.LookRotation(newDir);
The problem is that since the pivot of the head is not aligned with the laser beam, the turret turns into the almost right direction, but it shoots above the target. (It would hit perfectly, if the laser would come out of the red axis of the pivot.)
Is there a builtin method or some trick to achieve the correct functionality other then doing the calculation myself?
Okay, here's the quick and easy way to do this. It's probably "better" to do it with proper trig, but this should give you the result you want pretty quick:
If you don't already have a transform aligned with the barrel, then create an empty GameObject and line it up (make sure it's a child of the turret so they move together). Add a reference to your script for it's transform.
Then, in your first line, calculate from the new Barrel transform instead of the turretHead transform. Leave everything else the same. This way it calculates from the turret barrel, but moves the turret head.
Now, this approach isn't perfect. If the pivot center is too offset from the barrel transform, then it would be less accurate over large moves, or when aiming at something close by, because the expected position when aiming would be different than the initial position due to the rotation pivot being elsewhere. But this can be solved with iteration, as the calculation would become more accurate the closer it is to it's desired goal.

Mapping Mouse Position to Rotation in Unity

I want to write a simple foosball game in unity. To rotate the players I am mapping my mouse movement to the rotation of the players:
float mod = (Input.mousePosition.x - RotationSpeed) * RotationSpeed;
rb.transform.eulerAngles = new Vector3((90 - mod) , 90, 90);
with mod being a delta to my mouse position. However by doing so I can't kick the ball as I teleport through it by setting the exakt angle. So my question is: how to map the rotation in a way that I actually am able to apply force to the ball, so that it can be shot?
Edit more information:
By moving my mouse to the left, my players rotate clockwise. E.g. my mouse is at x position 300 , then the rotation of the player will be set to 300 times a step (here called "Rotation Speed"). Counterclockwise in the other direction. As in a typical foosball game I want to kick the ball by flinging my mouse to one of the directions. However the ball gets stuck / does not move much when they collide. The reason for this is probably, that by setting the euler Angle directly, the players "teleport" through the ball and don't kick it back. So I need some kind of an instant smooth motion to where my mouse is.

Categories

Resources