Getting the correct movement vector to fire a bullet from a rotated sprite - swift

Hi,
I'm using a SKNode to represent a ship sprite. This sprite can be rotated and moved by touch events. This sprite should fire a bullet (another SKNode object) every second.
I plan to use SKAction to move the bullet.
The zRotation method of SKNode objects returns the rotation around the z-Axis. But how can I transform this into the right vector?
Thanks for your help
Stefan

Related

Unity 2D physics is not colliding when player moves fast

A 2D circle shaped player has the following code
Vector2 target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = target;
which sets the player's position to the mouse position, and when it collides with enemies they get destroyed (simple concept).
The problem is that the enemies do not recognize collision when player passes through them quickly so they are not destroyed. This problem frustrated me I don't know why collision doesn't work when an object passes through another object quickly .
Is there a solution? Or it is just UnityEngine Maximum Performance
Thanks in advance.
I recommend you to see this link.
It explains much about Collision in unity.It looks similar to your game also.
You could also calculate distance between two objects and when the
distance is equal to zero you may destroy that object too.
Note1: this destroys the object only when it is coinciding with another object.
Or by calculating the outer distance between two objects when it is
equal to zero you destroy that object.
Note2: this destroys the object when two objects are starting to collide.

Unity: aligning object from multiple points to ground

I need to allow players to drag objects around the surface of my lowpoly ground so that the object sticks to the ground from certain points (in this case all the 6 legs). I tried Raycast to set position and rotation of the object with:
transform.position = hit.point;
transform.rotation = Quaternion.FromToRotation (Vector3.up, hit.normal);
This works quite nice but it always leaves some of the legs in the air because I set position and rotation of the whole transform not each leg individually. I could Raycast all of the legs individually but after that how can I position and rotate legs so that other parts of the model stays with the legs and the model doesn't "break".
Also I tried to give each leg a rigidbody and box collider but that just breaks the model by shooting the legs somewhere.
All ideas are welcome.

Collision Detection of objects in certain area of camera in Unity3d

I am creating a 3rd person action game where the player is a helicopter and he can shoot other objects while moving. The problem is I am trying to find the enemy objects who are inside a circle in the center of the camera and I need to track them and shoot them.
Raycast wouldn't help as i need a thicker raycast and so I tried spherecast and capsulecast.
I have a GUI element which gives the player idea on where he can shoot.When using Spherecast or Capsulecast, It is working when the enemy is near but when the enemy is far behind I guess the spherecast becomes small while traveling along z and doesn't hit the object most times.
if (Physics.SphereCast (startPoint, 1f, transform.forward, out hit)) {
if (hit.collider.CompareTag ("Shootable") ){
Debug.Log(hit.collider.name);
Destroy(hit.collider.gameObject);
}
}
I have seen raycast from camera and so i was wondering if there is something to do like circlecast from the camera which would be appropriate for this. If not how can I proceed?
Any help is really appreciated.
If you want to detect whether enemies lie within a conical area in front of your camera, using a SphereCast or RayCast will not be able to meet your needs.
Instead, you might consider checking the angle between an enemy's relative position and your camera's forward vector, to see if it is below a particular value, and hence within the cone.
For a 60-degree field of view, and assuming you store your enemy Transform components in an array/List, your code might look like:
foreach (Transform enemy in enemies){
if (Vector3.Angle(transform.forward, enemy.position - transform.position) < 30){
Destroy(enemy.gameObject);
}
}
Hope this helps! Let me know if you have any questions. (Answer adapted from this Unity question.)

Physics vs graphics fighting each other (box2d)

so I have a ball (sprite subclass) that can be dragged around the screen and updates it body to tell the world its position.
The problem is, when I apply a force to the ball while the ball is being touched (thus being controlled by a finger), I have two pieces of code fighting against each other to update the sprite's visual position.
The world stepper is trying to follow through the physics and update the ball's position. The touch methods however are also trying to update the ball's body and graphic position. Ideally, when touched I would like to kill off any physics affecting the ball.
So what exactly should I be trying to do to consolidate this? This is also creating issues for collisions and filtering. Any help appreciated. Thanks
Do you want the ball to have an accurate physics simulation? If not, you could always use the SetTransform method of the b2body class like this.
CGPoint ballLocation = ;//use the touched location for this
b2Vec2 vecBallLocation = b2Vec2(ballLocation.x / 32, ballLocation.y / 32);//calculate this on your own
b2Body * ballBody = ;//replace this variable with your ball's b2Body;
ballBody->SetTransform(vecBallLocation, ballBody->GetAngle());
Do not set the ball sprite's position manually. Leave this up to the physics simulation. Your code will get really messy over time if you have to keep track of what sprites you set the position of manually and which ones use the physics simulation to update their positions. This way is also much easier.
When the physics object is being dragged, set its velocity and angular velocity to 0,0 before running the physics step simulation. You may also want to temporarily get the physics object's position, then step, then set the body's position back, then apply whatever touch dragging offset you got from moving the finger to the body.

stick a sprite to another when it collide with it

so I have a sprite that is created every second and that is moving at a random position (sprite1) and another sprite that has a fixed position (sprite2). I would like that when sprite1 collide with sprite2, sprite1 is like sticked to it (it stops moving and is sticked to it) . How can I do this please ? sorry for my english I'm french :/
p.s : sprite2 is rotating with accelerometer, so if sprite1 collide with it I would like that it rotate too :)
I think, you can try to use box2d to do this. It will help to detect collisions and to manage rotations, movement, etc.
I think, you can do it simply in Cocos2d.
1) First set the rect for sprite1 and sprite2 using CGRectMake(x,y,width,height)
2) As you told sprite1 is moving at random position and sprite2 is fixed to particular position, you can check them collide by using CGRectIntersectsRect([sprite1 bounds],[sprite2 bounds]).
3) if it intersects, set sprite1.position = sprite2.position
Note: you said sprite1 is rotating, rect can be fit only to the regular bodies. if you want exact collision or physical properties for sprite better you can go for box2d.
If you don't want to use Box2d (which can handle circle collisions), you can try something like this:
1.) Detect collision, is the distance between the two circles center point (x,y), less than the sum of the two circles radius.
2.) Make the Sprite1 stick to Sprite2, Stop the movement of Sprite1, and save the relative delta (x,y) to Sprite2, then whenever Sprite2 moves or rotates apply the same delta movement and rotation to Sprite1.