Unity3D Detecting the Edge of the Screen - Object is Flickering when moved - unity3d

Hi am developing a simple Space Shooter styled 2D game and I am stuck at the point where the Object should restrict itself moving beyond the left and right edges of the screen.
I implemented #Waz solution in one of the answers in Unity Answers and it works great if the object is not a rigidbody. However if it is applied to a rigidbody, the object starts to flicker. Below is the code that I used from #Waz
float speed = 0.1f;
Vector3 viewPos = Camera.main.WorldToViewportPoint(transform.position);
viewPos.x = Mathf.Clamp01(viewPos.x);
viewPos.y = Mathf.Clamp01(viewPos.y);
transform.position = Camera.main.ViewportToWorldPoint(viewPos);
Here is the link where #Waz mentioned his piece of code:
http://answers.unity3d.com/questions/148790/detecting-the-edge-of-the-screen.html
Here is a link that says to use an alternative solution for rigidbody but this code does not work for me:
http://answers.unity3d.com/questions/62189/detect-edge-of-screen.html
I am not sure how to modify the above code so that the object I touch and move does not flicker. Any help would be great.

You are translating from arbitrary float coordinates to the range [0,1] and back again. Likely the issue you are running into is due to floating-point inaccuracies when your world-position is far away from 0.
There are multiple ways of solving this:
In your above script, only perform the transform if they are actually touching the edge of the screen.
Handle the OnBecameVisible() and OnBecameInvisible() messages. Don't let the player move off screen if it would cause them to "go invisible".
Use the IsVisibleFrom() callback from this wiki article. Some people prefer this because they claim that "OnBecameVisible()/OnBecameInvisible() are broken."
I don't know how/why they believe they're broken.

Have you tried using Screen.width and Screen.height in detecting the edge of the screen? Maybe it can help in the prevention of the flickering.

Related

How to rotate an object to point along a vector Unity3D

Hi I have been searching google for some time now with no avail. I was wondering if there was some function or easy way to make an object rotate so it is pointing along a certain vector (in this case, pointing the same way it is moving, but I would like to know generally how to do this with any Vector3) in Unity? I know it is possible to rotate an object using a Vector3 holding EulerAngles, but that is not what I'm looking for, since the object will not point in the direction of that Vector in 3D space.
I believe you should be able to use either Transform.LookAt or Quaternion.LookRotation to accomplish your goal.
Additionally, here is a link to a good Unity answers post where they use Quaternion.LookRotation to get a character to face the position it's moving based on its vertical and horizontal movement input.
transform.forward = velocity.normalized;
Hope that helps =)

How to ensure sprites face the correct direction at all times in a 2D top down game. (logic)

I am making a multiplayer top down 2D game with 3d elements. All my movement, healthbars and basic functionality is working flawlessly even while hosted and playing on a server, node.js socket.io. However In this game it is possible to move the camera like in Realm of the mad god.
in case you are in doubt here is a video: https://youtu.be/4tdcxl3aZ0c?t=31s
This of course means that the players can end up being upside down with regards to each other and I cannot find a solution that works in all regards to make sure the sprites of the other players are always facing the correct direction with regards to their movement.
I have made several solution to this problem which cover most scenarios but while play testing other things we always end up noticing that the sprites sometimes face the wrong directions. So I am wondering if anyone has an answer, the logic, the fixing this problem.
Things I have tried:
Adding a gameobject to the camera to which all sprites asses their change in distance and determine their facing direction based off that information. (this leads to the players sometimes flipping erratically when the camera is moved and they as well are moving as sometimes they may be moving slower and there although moving left the camera approaches from the right and that flips them)
Adding a gameobject to the world which allows all sprites to have a fixed point to which they can measure their change in distance and therefore also know what direction to face (this worked somewhat better as they always know what direction they have to face, however once the player is upside down everything is inverted)
Emitting to the other players wether I am upside down or not in order to try to reinvert the above solution in the case I am upside down. (I could not find a good way to do this, and it got me thinking that this must be a problem people have fixed before many times and perhaps someone know of a good solution that works.)
thank you all for your input.
I seem to have found a solution for this issue that works decently well. Keeping in mind that I do not want to have the server being involved in this and I would rather have each individual sprite know its direction rather that have something heavy trying to determine this logic I came up with the following solution. May not be the best but it works. Still very keen to hear other solutions.
On my main character I have a switch case, which changes depending on the players rotation in the world. I need this switch case anyway for fixing (http://answers.unity3d.com/questions/1348301/trying-to-change-the-cameratransparencysortaxis-to.html?childToView=1348316#answer-1348316) that issue.
As the cases change I simply place the gameobject that I want the sprites to compare their distance to at 1 of 4 positions. YPos, YNeg, XPos, XNeg. Meaning that the sprite now determines its facing direction based on a gameobject that is placed in accordance with the Players position. without having to place it on the camera.
I will update if during further play tests this gives me trouble but thus far it works in the all the cases I need it to.
Still very willing to hear other solutions to this problem.
Thank you.

Change playerCharacter's facing direction in Unity3D

I am trying (for learning purposes) to make a Portal game. I have the basics working, I can place two portals, and walking within the collider of one makes me teleport to the other, however I can't seem to get the facing direction/rotation to work.
I want to face outwards from the new portal after the teleportation.
I have tried the following, with no success:
var angle = thisPortalCamera.transform.rotation.eulerAngles.y - otherPortalCamera.transform.rotation.eulerAngles.y;
playerChar.transform.Rotate(Vector3.up, angle);
My idea here was that only the y-axis rotation really matters, and I think I should rotate the player by the difference in axis between the two portals. This is probably really simple and easy, but I am pretty new to Unity. Any suggestions?
The easiest way would be to set your portal so that its forward is the orientation you want you player to have. Then you just go with:
player.transform.rotation = portal.transform.rotation;
player.transform.position = portal.transform.position;
The aim is to have the blue arrow of your portal to point in the right direction.
One of the easiest way is to use the function LookAt() and Rotate() of the transform. This function take one parameter that is the Position the object has to look.
transform.LookAt(portal.position);
tranform.Rotate(new Vector3(0,180,0);
This will make your character face the opposite way of your portal.

How to handle sprites when moving out of the camera view?

I am learning Unity2D and trying to teach myself based on making clones of older games and my first one is Spacewar (1962 game).
The problem I cannot wrap my head around is setting up a script (I think this is the correct approach) for when my sprite goes off camera on one edge it will appear on the opposite edge, example if you are not familiar with Spacewar would be PacMan when PacMan goes off the screen and appears on the opposite side.
How should I approach this because there are other games in the list of my cloning projects that will also share this same view mechanic.
I feel like I have the logic inside my head perfectly on what needs to be done but at the same time I am so new to Unity that syntax is preventing me from moving forward.
Should this be based on :
1) Having collision on my edges and just moving it to the opposite side
or
2) Go based on camera edges.
I kinda think it might be something along the lines of #2 because what if the screen size is different on another computer.
What I was looking for was this "Mathf.Clamp(transform.position.x, 6.0, -6.0)". This solved my problem from going off the screen on one edge and reappearing in the other. This is just for the left and right edges.
if(transform.position.x < -6.0 || transform.position.x > 6.0){
var xPos : float = Mathf.Clamp(transform.position.x, 6.0, -6.0);
transform.position = Vector2(xPos, transform.position.y);
}

Problem using chipmunk

I made a game that using a character that can moving left or right by touching the screen, above the character, it is a apple, what i need is the apple will fall down, the character may push the apple up again just like playing volleyball.
That's the problem,what i have learnt from "bounding ball" is give a sprite a shape and body, then they would collide automatically,when i give shape and body to the character, the animation is not work, the character moved and after a second he "jumps" to the starting point,whatever i using setPosition or Action(MoveTo) to sprite are also useless
moving the body is not work too.I use touchmoved to set the body->p,it keeps shaking and when i release the button, it "fly" alway out of bound..........
Can anyone tell me what should i do or give a similar example(e.g. volleyball and something like that),Plx help!!!
I appreciate that if anyone can give me a hand.
Adding a chipmunk body and shape to a sprite is not the best way to go about this. Try not to think from the screen's point of view! Instead, create a controller that contains a reference to the physics body and the sprite.
Using a physics engine, such as chipmunk, can be a bit daunting. It helps to enable debug drawing. Have a look into how you can best do that! There is some code you can take from the cocos2d chipmunk example, that comes with the normal cocos2d download)
I take it you've added a "ground" shape as a static shape and have gravity set?
When you move a body with body->p = cpv(x,y); the problem is that you don't change the objects state other than it's position. To the physics engine, it's like you teleport the object. If the object was falling, it will still be falling.
So now, your falling object is not touching the ground. Its speed increases. You "teleport" it again.. but until it bumps into anything, gravity will keep increasing its falling speed.
So, if you move an object to another point, be sure to reset all forces and the velocity on that object, if this need to be done!
A better way than using body->p is to use these functions:
void cpBodySlew(cpBody *body, cpVect pos, cpFloat dt)
void cpBodyUpdateVelocity(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt)
void cpBodyUpdatePosition(cpBody *body, cpFloat dt)
The documentation of these functions isn't too ridged, but there's other documentation out there. Give it a google :-p I haven't fully mastered using these functions yet either (I'm still trying to get around Chipmunk too) but they're the way to go about it.
Alternatively you can (instead of setting body->p) set the velocity of the object directly (though cpBodySlew does this for you)
The latest chipmunk code is doing this with a pivot joint in between the object to be grabbed and the mouse (touch): ChipmunkDemo.c.
See also: Mouse interaction