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.
Related
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.
I have a player which is rotating around one point and I need to make an enemy aim in front of the player to actually shoot and hit the player.
In the figure below, the red point is my object that is moving around yellow point (enemy), and im trying to find out how I can make the enemy shoot ahead to actually hit the red. If I shoot at the last known location, the bullet passes behind red. I want to calculate a position in front of red to aim at so that I can actually hit red.
Thanks
The easiest way would be to calculate how much angle does the red object sweep per second. float A = 2 * Mathf.pi * Mathf.Rad2Deg * radius / red_linear_speed
Multiply that with how long it takes for yellow's bullet to reach the circle. That's the amount of angle that you have to aim ahead of red. FrontA = A * radius / bullet_speed
Rotate the vector from yellow to red by that angle and shoot. (assuming y is up) Vector3 shootDirection = Quaternion.AngleAxis(FrontA, Vector3.up) * (red.transform.position - yellow.transform.position).normalized
You may have to try -FrontA or Vector3.down at this last step to ensure you get the direction right.
You'll hit red, guaranteed!
I am working on a game and have the following information
Player Position - Vector3
Player Rotation - float (Radiant)
Enemy Location - Vector3
Player attack box - length, width, height
What I need to do it test if the Enemy Location is inside of the player attack box. I know somehow I would either have to rotate the attack box around the player or rotate the enemy location around the player then test it. The rotation is only left to right no up and down. That is why it is a single Radiant value.
I have tried to code the rotation of both the player attack box and the enemy but I feel like I do not have enough knowledge about vector math to properly come up with a solution.
As far as I understand, it is enough to determine whether enemy is inside rotated (around z-axis) parallelepiped centered at player
We can transform enemy coordinates into player coordinate system. To perform this task, we should make translation (to provide player position in origin), then make rotation by reverse angle
newEnemyX = (enemy.x - player.x) * Cos(P_Rotation) + (enemy.y - player.y) * Sin(P_Rotation)
newEnemyY = -(enemy.x - player.x) * Sin(P_Rotation) + (enemy.y - player.y) * Cos(P_Rotation)
newEnemyZ = enemy.z - player.z
Now compare (I assume length-width-height correspond to x,y,z)
-length/2 <= newEnemyX <= length/2
-width/2 <= newEnemyY <= width/2
-height/2 <= newEnemyZ <= height/2
I am trying to make a spin wheel that is divided into 6 sections. Each section is marked with a gameObject that is centered in that section. After the player spins the wheel, it will rotate until its starts stopping and then the wheel moves and stops in the center based on the section that was selected (Randomly). I used the following code to rotate the wheel towards the 0 on X axis. this code works fine and the wheel rotates fine, but only if the selected section was on the positive X axis.
float rotateFloat = (((randomReward + 1) * 60) - 30) - transform.rotation.z;
Quaternion targetRotation = Quaternion.Euler(new Vector3(0, 0, rotateFloat));
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, f_difX * Time.deltaTime);
I did some digging and found that Quaternion.RotateTowards()finds the closest way towards the target and rotates using that direction (This caused a problem with the direction of rotation).
Example (Follow image): The player swipes and randomReward (Number 5 on spin wheel) and the wheel starts rotating. When the wheel slows down, it starts moving towards the center. Then it will stop spinning along the direction of the swipe and will rotate towards the other side (Because the distance to the center is closer from the left side).
How can I set it to follow the same direction of the swipe regardless of which is closer to the center. And maybe if there is a better way to do this, please enlighten me. Thanks.
I think the easiest way to rotate a GameObject is by using:
float Speed = 1f;
void Update()
{
// Rotate the object around its local X axis at 1 degree per second
transform.Rotate(Vector3.right * Time.deltaTime * Speed);
}
You can read more about his here
It can happen sometimes the center of the GameObject it´s not placed in the center of the mesh or sprite. If you can´t modify this for any reason, what you can do is place this model/sprite as a child of an Empty GameObject and then attach the rotation script to the Empty GameObject.
While i understand that you don't want people to rotate the disk to the reward they want. Why do you use a random reward and go through the trouble of lining the rotation to the reward?
You should be able to say catch a 'swipe time', then do some math to it (say so it turns at least once if the screen touch time is very short) and then add a random range to it, that is within the circumference of the disk. Then when the disk is done spinning do a ray cast at the top location to determine the reward? (could also link the spinning time to the swipe-time, so that the reward is offered in somewhat the same time interval).
//this comment would have been made as a comment if i would have had the rights to do so, as i think this response may help the question asker, it is provided as an answer instead. i do hope this doesn't piss any one off to much :(
So I need some help with my C# XNA top-down 2D "wave spawn" game. Basically, I have a player that has the functionality of shooting a laser at enemies that spawn in waves. The problem that I'm looking at fixing right now is my enemy collision code with a stationary non-rotated wall (rectangle)
I need to detect which side the enemy hits the rectangle on so he can properly get around the rectangle and to the player. (Example: if the enemy is blocked from the right side of the wall, move up the wall until he can get around the wall)
Here is the code I have to detect the angle between the enemy and the stationary wall when collided:
// I am blocked, find angle to find which side I'm blocked from
centerOfWall = wall.CenterOfWall;
Vector2 difference = centerOfWall - centerOfEnemy;
// Getting the angle of intersection between the two rectangles!
angle = Math.Atan2(difference.X, difference.Y);
// Converting angle to degrees!
degrees = angle * (180 / MathHelper.Pi);
I guess what I'm asking is, with this angle, can I find the side at which the enemy hits the wall? Like if the angle is less than 90 degrees but greater than 0 degrees I will be hitting from the Left or something like that.
If this doesn't work, can someone help me in finding another way that would work?
Thanks,
Johan