I have a game where the main character is on the far left side of the screen (landscape) in the middle. He rotates using the accelerometer. Hes going to be shooting bullets constantly and I need a way to make the bullets fire depending on his rotation.
So basically I dont know the math to make the bullet fire straight depending on the sprites rotation.
Any help appreciated.
Edit:
I know my question is kind of vague, I dont really know how to ask it.
My character is a top view of a turret, he will rotate when you rotate your phone, and obviously as he rotates, the bullets still need to shoot straight. I just dont know the math to make this happen.
I assume you know how get values from accelerometer and the character "rotates" up and down (angry birds style).
If you fire your bullets by some amount of coordinates (moveBy(x,y) ) - just use more y to fire up. To fire down use more x.
For example: To fire in straight line - moveBy(10,10)
To fire up - moveBy(10,20);
To fire dowp - moveBy(20,10);
General case - moveBy(x*accelerometerVal, y*(1/accelerometerVal) );
Related
I am looking for a way to make my game load more SKScene when my node (ball) goes above certain Y position.
I'm looking for a way to make the loading a little bit like ColorSwitch game where you tap and the ball loads more screen once you hit a certain Y position.
Use SKCameraNode and have it centered on your scene (or wherever it is you want to look at). Check documentation or search here for more help on setting up your camera.
In update() simply check the position of ball's y value ball!.position.y, if it's past a threshold such as view!.bounds.y, move your camera up by however much camera!.runAction(SKAction.moveBy...)
If you want to scroll back down, it would be a bit more complicated, but the same principles would apply.
(note: I have my stuff as Optionals so your code may not have !)
https://developer.apple.com/reference/spritekit/skcameranode
You could also use invisible boxes and collision detection, but that seems more complicated of an approach to me >.>
If you post some code we can help you get it up and running. Unfortunately, there will be some degree of math involved x[
I’m starting a learning project. The idea is that you have an archer character that is static, that has a bow attached to it that shoots arrows to targets of varying difficulty.
Turns out that right at the start I’m stuck. How do I make it so that the bow rotates when the player clicks and holds the mouse anywhere on the screen? So I click+hold and move left/right and the bow rotates left/right to aim the shot. I’d like to also eventually make it portable to phones (so you’d tap+hold etc).
Stack Overflow isnt a code writing service but i will explain what you must do:
Method 1 (Exact Aiming):
For every frame the mouse is down:
Make a Ray from a screen point... hint (use
camera.ScreenPointToRay).
Get a far point along the Ray using ray.GetPoint(distance);.
Bow.Transform.LookAt(newPoint, Vector3.Up);.
Method 2 (Continuous Movement):
Make a variable oldMousePos to store a Vector2 location.
Record your initial screen click position into that variable on a
mouse down event.
Have a function that runs once every frame the mouse stays down.
For the direction of the rotation of the bow, you can use
(newMousePos - oldMousePos).normalized;.
For the speed of rotation for your bow, you can use (newMousePos -
oldMousePos).sqrMagnitude;.
I am working on a tabletop game that user controls using accelerometer in the phone. It is pretty similar to Labyrinth on iOS.
The scene contains a tabletop created using planes and cubes, and a sphere as a ball. As it works in Labyrinth game, on tilting phone ball should drop to the tilted side, while the camera stays centered to table. I am trying to do similar thing where user tilts the phone, and objects on table move to tilted side.
Currently, I add the x and z component to Physics.gravity on tilt. Sadly, this change in gravity does not affect the ball which stays put on the table. Use gravity is selected for the ball, and it drop down from height to the tabletop initially and then comes to halt. After the initial drop, ball does not react to any gravity change.
I have also tried rotating the whole table, but using transform.rotate does not work either. The table rotates perfectly, alongwith the camera, but the ball stays put hanging in the air. So, currently I am out of my depth about the issue.
Is there any other way to allow tilt action registered, so that ball moves to the tilted direction? I cannot use addforce function, as there are multiple objects which need to react to tilt, and it will be difficult to keep track of that many addforce calls. How else can I get it working?
See this post for some related information.
As for why your sphere is sticking to the table, can I assume your sphere has a rigidbody? If so, you need need to wake it up:
if (rigidbody.IsSleeping())rigidbody.WakeUp();
Ideally you would make that call only after detecting a change in orientation/gravity, not every frame.
In my game the player running in forward direction on a path triggers the collider to instantiate the enemies, when the same player comes in opposite direction and triggers the collider i dont want the player to instantiate enemies. How to achieve this please help..
When I read your question, I see two possible situations here:
The player is only allowed to trigger when moving forward through it.
The player is only allowed to trigger when coming from a certain direction.
The first one would be quite easy. You can get the movement direction from the character controller to see if he's moving forward. You can for example try to use Transform.LookAt and compare it with the rotation of the player.
The second one is approached differently. For example, your trigger may only respond when the player moves forward along the x-axis. Then you record the X position as the very last thing in your movement loop. Then compare your X position in the next frame with the position in the last frame. If the difference between them is positive, you are moving in a positive direction. If negative, your moving in a negative direction.
I was wondering how to have it in Cocos2D to make a sprite move up, while the user's finger is being held on the screen. I have a mechanism in place that does so, but only moves the sprite once.
Is it possible to have the sprite move up while the finger is holding on the screen?
Also, even though it's not necessary, if you could make it so when the user isn't holding on the screen, the sprite goes down.
Thanks, I think that a physics engine such as Box2D of Chipmunk is necessary, but I'm not sure how to do this.
-Soule
I have a not so perfect answer:
set up a repeatable timer with a short interval, at most 0.1 second, then fire the timer in ccTouchBegan, invalid the timer in ccTouchEnded. Everytime the timer ticks, move the sprite a short distance.
the shorter the interval is, the smoother the move will be.