I have several formulas I use for firing a bullet at a target, but it all breaks down when the player moves from his original position and the bullet reaches the old position of the player as intended.
When PlayerPosition == BulletPosition, how do I make the bullet keep going in the right direction if it misses? My problem is once the bullet reaches where it was supposed to go it stops and I need a new formula to keep it moving.
If it hits the player, that's easy, remove the item, but I can't seem to find a good solution. Below is some code, it's super simple for now.
var movement = PlayerPosition - Position;
if (movement != Vector2.Zero)
movement.Normalize();
//var angle = Math.Atan2(movement.Y, movement.X);
Position += movement*_projectileMoveSpeed;
Did you intend the bullet's speed to be affected by the distance from the player?
I'd just save the velocity, then use that in the future frames. In pseudocode:
to shoot a bullet:
direction is sign(PlayerPosition - Position)
in each frame:
for each bullet:
modify position by direction * projectileMoveSpeed
handle collision (player or screen edge)
Related
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.
I am a newbie to unity. In my project, the ball should instantiate in the player
position and it should go randomly 40 to 120 degrees from the player position.
I am using transition.forward. My player is constantly in running. when the player stands the ball is going correctly in front of the player.
but when the player runs the ball is going backward
How can I move towards randomly in front of the player? Like a ball
I am using the below code.
transform.Translate(Vector3.forward*5f );
There is a chance it might be fixed if you spawn the ball in front of the character. Since it works when the player is stationary, it might be conflicting with the collider of player when it is moving.
Also translating will just move the ball, not give it a velocity or anything. You should take a look at https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
So, first translate the ball forward and see if it spawned in the right location.
Then add a force vector to the ball's rigid body in the direction you like.
Something like:
ball.transform.Translate(Vector3.forward*5f);
float thrust = 10;
var rb = ball.GetComponent<Rigidbody>();
rb.AddForce(transform.forward * thrust);
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 :(
I have an enemy on Unity, the enemy follows the player and when player is at a specific distance i want the enemy run X units without stopping. The idea is that the enemy run, then he gets tired and the Player can attack him from behind.
How can i make enemy run X distance forward without stopping?
How can i make enemy run X distance forward without stopping?
Depends on a lot of things, how is the movement handled in your game? is it 2D? 3D? etc. ...
If you use a character controller on your Enemy GameObject for example (in 2D)
Vector2 moveDirection = Vector2.zero;
void Update(){
CharacterController player = GetComponent<CharacterController>();
if (player.isGrounded)
{
// constantly move horizontally
moveDirection.x = valuespeed;
player.Move(moveDirection * Time.deltaTime);
}
}
This is just some basic theory. FOr 3D, you add a dimension and choose the direction by using the current forward vector or so.
As for checking the X value, what you could do is that in your Enemy class, when the "running mode" is triggered, you save the origin position and in the update, you check the distance between CurrentPosition and Origin (Vector3.Distance(......)) and as long as the distance is smaller than X, you let him run. If not, you are not in "running mode".
To give you the setting: I'm making a Vive game in zero-g, where the player moves by grabbing handles and propelling themselves.
What I'd like is for the player to be able to rotate themselves, by grabbing a handle with both hands. Imagine how you'd move in zero-g if you held on to a bar with both hands.
To illustrate:
On the left hand side the player has grabbed a handlebar with both hands. Left arm extended, right arm bent.
In the right hand side picture the player has now extended their right arm, which has rotated the player around the bar.
I guess it's easier to see it as if the player would be moving the entire world, when they do this.
My question is: How can I do this in Unity in 3 dimensions, either through math of Unity-trickery? It needs to roll, yaw and position the player relative to the hands.
Thank you!
Record the average of the three vectors. Then in the next frame, get the difference from the previous average. The difference can be used as euler angles to apply constant force to a rigidbody (or rotate an object by that amount, or other possibilities, depending on your goals).
https://docs.unity3d.com/ScriptReference/Transform.Rotate.html
https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
Vector3 previousCenterPoint;
void Update() {
Vector3 newCenterPoint = (leftHand.transform.position + rightHand.transform.position + player.transform.position) / 3f;
if (previousCenterPoint != null)
{
Vector3 someEulerAngles = newCenterPoint - previousCenterPoint;
someRigidBody.AddForce(someEulerAngles, ForceMode.VelocityChange);
}
previousCenterPoint = newCenterPoint;
}