Fixing the Unity Constantly Turning or Spinning Player / Camera Problem

If your players are reporting that they keep spinning in your game and can’t stop, there might be an issue with the joystick inputs on their end that makes it impossible for you to find the bug. However… you can program to solve it if you make enabling Joysticks optional.

Here is a player video of the bug!

The best way to solve this from the programmer end is to write your input scripts to deal with it. Currently, your game (by default) accepts Horizontal and Vertical Axis info from the keyboard or a joystick. They are labeled the same in the Input Manager… That way you can write code like:

float translation = Input.GetAxis(“Vertical”) * speed;
float rotation = Input.GetAxis(“Horizontal”) * rotationSpeed;

And it won’t matter if the player is using a keyboard or a joystick. Unfortunately when a player has something going on with their joystick stuff (virtual joysticks seem to cause issues), the Axis gets all wonky… Players report spinning around that won’t stop.

BUT… If you change the name of your second set of Horizontal and Vertical Axes in the Input Manager (the ones that correspond to the joysticks) to something like HorizontalJoy and VerticalJoy then they won’t interfere for your keyboard-only using player. You could then add a toggle on your Controls or Pause menu to enable Joystick controls.

then you might write code like this:

if( !joystick )
{
//if joysticks aren’t enabled – just track keyboard axis

float translation = Input.GetAxis(“Vertical”) * speed;
float rotation = Input.GetAxis(“Horizontal”) * rotationSpeed;
} else

{

float translation = Input.GetAxis(“VerticalJoy”) * speed;
float rotation = Input.GetAxis(“HorizontalJoy”) * rotationSpeed;
}

Below is a screenshot of the input manager – notice the second set of Horizontal and Vertical Axis are called HorizontalJ and VerticalJ. Do something like that and write code to allow for joy support if you want to give your user the option and the bug should be fixed on your end and the users. This also means joys won’t really be tracked unless you write code to watch the specific axis as stated above. This solution worked for my testers.

Screen Shot 2014-12-01 at 7.08.51 PM

Collision Avoidance using Raycasts in C# Code Snippet

screen_121.3604

I just add some pretty good code for collision avoidance in Space Warfare… Most code ideas I found around the nets about collision avoidance use the same block of Unity Javascript. Well, Javascript is taking a back seat in Unity pretty soon and I don’t really use it. I thought other folks might like the same code snippet in C#. Here it is!

 

//subtract AI thing’s position from waypoint, player, whatever it is going towards…

Vector3 target = targetYouAreFollowing.position – transform.position;

//normalize it to get direction
target = target.normalized;

//now make a new raycast hit
//and draw a line from the AI out some distance in the ‘forward direction

RaycastHit hit;

if(Physics.Raycast(transform.position, transform.forward, out hit, 800f)){

//check that its not hitting itself
//then add the normalised hit direction to your direction plus some repulsion force -in my case // 400f

if(hit.transform != transform){
Debug.DrawLine(transform.position, hit.point, Color.red);

target +=hit.normal * 400f;
}

}

//now make two more raycasts out to the left and right to make the cornering more accurate and reducing collisions more

Vector3 leftR = transform.position;
Vector3 rightR = transform.position;

leftR.x -= 2;
rightR.x +=2;

if(Physics.Raycast(leftR, transform.forward, out hit, 800f)){
if(hit.transform != transform){
Debug.DrawLine(leftR, hit.point, Color.red);
target +=hit.normal * 400f;
}

}
if(Physics.Raycast(rightR, transform.forward, out hit, 800f)){
if(hit.transform != transform){
Debug.DrawLine(rightR, hit.point, Color.red);

target +=hit.normal * 400f;
}

}

// then set the look rotation toward this new target based on the collisions

Quaternion torotation = Quaternion.LookRotation(target);

//then slerp the rotation
transform.rotation = Quaternion.Slerp(transform.rotation, torotation, Time.deltaTime * 100f);

//finally add some propulsion to move the object forward based on this rotation
//mine is a little more complicated than below but you hopefully get the idea…

transform.position += transform.forward *20f *Time.deltaTime;

 

Thanks to http://wiki.dreamsteep.com/Unity_ai and http://vimeo.com/9304844 for explaining the concept to me. If any readers are having trouble, check those sources… My numbers for distance and repulsion are based on spaceship moving at high speeds. The examples show slow moving spheres so their numbers might work better.

Next I will be adding a case where if all three raycasts hit you need to move one way or another. Otherwise, everything cancels out just about and ships still crash into stuff and explode. I kinda want that to happen a little bit so that you can feel like you are in a good asteroid chase scene, but not so much that it is over in seconds…

Ghost Maze – A game about ghosts in mazes

For the month of October I have been working on a way to randomly generate a world that is interconnected in a way that I can drop the functionality into my Space Wars game to manage jump gates between Star Systems. The result has been this maze game. Some of the behind the scenes stuff isn’t really used by the maze game and the wall building stuff probably won’t be used in Space Wars but its still coming along pretty well. Here is a video of the gameplay so far…

At some point you could see the maze generation. I used Conway’s Game of Life to generate the structure. Then I had to test which squares need walls. I am also using the Respawn system to drop in Ghosts and objects to help you navigate the maze. The exit portal ends up also being spawned randomly. Here is a pic of early maze generation —

Screen Shot 2014-10-15 at 6.37.17 PM

 

Originally, I was building 100×100 mazes, but with the game in action 50×50 currently runs best. Also it seems pretty impossible even in the smaller size. I built a pseudo occlusion culling system as well which disables renderers for rooms that aren’t super close. It definitely helped speed the game up.

 

Let’s Call April’s #1GAM Finished: Tap Invasion Post Mortem

April is coming to a close and with it, my 5th “1 Game a Month” game must be called complete. Can I endlessly tinker with it? Sure. Did I sort of want the Blue Eyes to split apart into baby bad guys like those Zelda enemies? Yes. Should the game have ten more bosses? Should Level 12-13 be winnable? Will anyone other than me see the level 10 boss? Who knows, but it is April 3oth and it is time for Tap Invasion to be called finished. Here is the link to Tap Invasion.

Screen Shot 2014-04-30 at 5.48.15 PM

While May will probably see me embark on a month long quest to make a massively multiplayer procedurally generated infinite terrain RPG with exploration, self aware AI, fully original 3D characters, crafting everything in the game, modding and Occulus Rift support. Or maybe a multiplayer Pong clone?

Post Mortem:

What went well:

  • I had an initial idea for something with simple controls but Space Invaders-like and I think I’ve accomplished that goal.
  • I got the basic concept up and running in a day!
  • I was happy with my code design allowing me to easily add new enemies and other features.
  • 5 bosses, 5 enemies, 2 power ups – the game feels varied and polished.
  • I really like the soundtrack – but I hope to make my own music someday.
  • Most people who have given feed back say it’s simple to learn and addictive!
  • Making pixel art went pretty well – I think its cool, but who knows what other people think. I may continue this route for my future Space Wars games.
  • I have a lot of cool reusuable code – Top Score Board!
  • I sorta got touch controls to work, but still trying to get Unity Remote to work or get the game on my Android phone.

What went not so well:

  • Relearning PHP/MySQL and figuring out how to get it all working with Unity ate up a whole week!
  • Original score board code was super complicated.
  • My game controller object is super bloated.
  • The music is a little memory intense.
  • The game seems to get unbeatable – much better balanced now but could use a little more.
  • Trying to port to Android and iOS may be impossible!?

Tap Invasion: Updated Demo with bad guys and bosses!

Screen Shot 2014-04-20 at 8.58.50 PM

Here is the updated version of the game so far. Still some polishing to do, but I think it is coming along well! The game difficulty gets epically unbalanced by level 6. Most players can get there and then are completely destroyed–so I will have to adjust how the algorithms work around that point to keep the intensity but not make it impossible. Also a few more offensive power ups might help even the field a little.

Here is the link to Tap Invasion.

Island in Unity 3D

Hi there! I am messing around with Unity 3D since a compiled game on the desktop might be about a billion times faster. I am getting worried that my programming abilities in WebGL have hit a snag with the need to write custom shaders to make all the ships draw and move without crippling the frame rate. And so I am looking at Unity.

I will start posting some of the tutorials and things I have been working with since its not super easy to hunt down everything needed to make a good space game apparently. I’ve also been working with Blender on 3D models of ships. Screenshots coming.

Screen Shot 2013-11-04 at 10.13.49 PM

Here is a screen shot of the Island I made. Its real simple, nothing special – I just used Unity’s terrain features and played around. There is a path you can take to get pretty close to the top of the crag. Also it seems you can just run up the face… not sure why that is possible. Still learning!! Click here to play around with it in the Unity Web player.