I am trying ot change FOV value ofr a VR camera in Unity. In editor works just fine but when i put the app on phone, thecamera has the defaul FOV value. Can somebody please tell me how can i effectively change the FOV value on phone?
Thanks
You can do it through code. Here's a script that will let you tap to adjust the FOV in realtime.
using UnityEngine;
using System.Collections;
using UnityEngine.VR; // you always need this to use special VR functions
public class VRUtility : MonoBehaviour {
public GameObject camGroup;
public Camera leftCam;
public Camera rightCam;
public float fov;
private float fovMin = 40;
private float fovMax = 100;
private float camGroupX = 0;
private float camGroupXMin = 0;
private float camGroupXMax = 100;
private float camGroupXStep = 20;
// Use this for initialization
public void Start () {
// set render quality to 50%, sacrificing speed for visual quality
// this is pretty important on laptops, where the framerate is often quite low
// 50% quality actually isn't that bad
VRSettings.renderScale = 3.0f;
OVRTouchpad.Create();
OVRTouchpad.TouchHandler += HandleTouchHandler;
leftCam.fieldOfView = fov;
rightCam.fieldOfView = fov;
}
// Update is called once per frame
void Update () {
if ( Input.GetKeyDown(KeyCode.R) ) {
InputTracking.Recenter(); // recenter "North" for VR, so that you don't have to twist around randomlys
}
// dynamically adjust VR visual quality in-game
if ( Input.GetKeyDown(KeyCode.RightBracket) ) { // increase visual quality
VRSettings.renderScale = Mathf.Clamp01( VRSettings.renderScale + 0.1f);
}
if ( Input.GetKeyDown(KeyCode.LeftBracket) ) { // decrease visual quality
VRSettings.renderScale = Mathf.Clamp01( VRSettings.renderScale - 0.1f);
}
}
private void HandleTouchHandler(object sender, System.EventArgs e){
var touchArgs = (OVRTouchpad.TouchArgs)e;
if (touchArgs.TouchType == OVRTouchpad.TouchEvent.SingleTap) {
fov += 10;
if (fov > fovMax) {
fov = fovMin;
}
leftCam.fieldOfView = fov;
rightCam.fieldOfView = fov;
}
if (touchArgs.TouchType == OVRTouchpad.TouchEvent.Left || touchArgs.TouchType == OVRTouchpad.TouchEvent.Right){
camGroupX += camGroupXStep;
if (camGroupX > camGroupXMax){
camGroupX = camGroupXMin;
}
camGroup.transform.position = new Vector3(camGroupX, 0, 0);
}
}
}
I'm also moving a camera between locations, but just ignore that.
Related
I want to get an image like in the picture with code. I stacked boxes because i must add joint with code. I want to make the boxes look like this as the character slides left and right smoothly. And how can i smooth swerving control in unity for touch or mouse button?
I tried this codes for movement:
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
firstPos = touch.position;
if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved)
Swerving(touch.position);
}
private void Swerving(Vector2 touch)
{
endPos = touch;
float diff = endPos.x - firstPos.x;
transform.Translate(diff * Time.deltaTime * swerveSpeed, 0, 0);
}
But not smooth swerving.
I tried hinge joint for image. I tried random values to motor,spring etc. But it didnt work i have never used joints.
In general when dealing with physics you shouldn't use transform this break collision detection and other physics related things like your joints etc.
Try and rather go through the Rigidbody using e.g. Rigidbody.MovePosition (or accordingly Rigidbody2D.MovePosition if your are in 2D) within FixedUpdate like e.g.
// or Rigidbody2D depending on your needs
[SerializeField] private Rigidbody _rigidbody;
private Vector2 startPos;
private float movement;
private void Awake()
{
if(!_rigidbody) rigidbody = GetComponent<Rigidbody>();
}
// get user input in Update in general
private void Update()
{
if (Input.touchCount > 0)
{
var touch = Input.GetTouch(0);
switch(touch.phase)
{
case TouchPhase.Began:
startPos = touch.position;
movement = 0;
break;
case TouchPhase.Stationary:
case TouchPhase.Moved:
movement = touch.position.x - startPos.x;
break;
default:
movement = 0;
}
}
}
// Apply Physics in FixedUpdate!
private void FixedUpdate()
{
_rigidbody.MovePosition(_rigidbody.position + Vector3.right * movement * Time.deltaTime * swerveSpeed);
}
However, in general the touch is a 2D pixel space position which might be quite different on different devices.
I would rather translate it into a 3D world space position and use something like e.g.
[SerializeField] private Rigidbody _rigidbody;
[SerializeField] private Camera _mainCamera;
private float _offset;
private Plane? _hitPlane;
private Vector3 _targetPosition;
private void Awake()
{
if (!_rigidbody) _rigidbody = GetComponent<Rigidbody>();
if (!_mainCamera) _mainCamera = Camera.main;
}
// get user input in Update in general
private void Update()
{
_targetPosition = _rigidbody.position;
if (Input.touchCount <= 0)
{
return;
}
var touch = Input.GetTouch(0);
switch (touch.phase)
{
case TouchPhase.Began:
{
// Check if you are touching the character
var ray = _mainCamera.ScreenPointToRay(touch.position);
if (Physics.Raycast(ray, out var hit) && hit.rigidbody == _rigidbody)
{
// create a virtual plane parallel to the camera view
// passing the hit point
_hitPlane = new Plane(-ray.direction, hit.point);
// also store the offset from pivot to the hit point
// we only care about the X axis
_offset = (_rigidbody.position - hit.point).x;
}
break;
}
case TouchPhase.Stationary:
case TouchPhase.Moved:
{
if (_hitPlane.HasValue)
{
// now instead keep ray casting against that virtual plane
var ray = _mainCamera.ScreenPointToRay(touch.position);
if (_hitPlane.Value.Raycast(ray, out var distance))
{
var hitPoint = ray.GetPoint(distance);
_targetPosition.x = hitPoint.x + _offset;
}
}
break;
}
default:
_hitPlane = null;
break;
}
}
// Apply Physics in FixedUpdate!
private void FixedUpdate()
{
_rigidbody.MovePosition(_targetPosition);
}
this makes sure your character is definitely placed on the X axis where you are dragging it without any delays but still complying with physics
using UnityEngine;
using System.Collections;
public class PlayerScript : MonoBehaviour {
public static float distanceTraveled;
private Touch curTouch;
public float speed;
public float maxSpeed;
public float maxSpeedConstant;
//Virtual buttons left and right half of the screen
private Rect leftHalf = new Rect(0F,0F,Screen.width/2,Screen.height);
private Rect rightHalf = new Rect(Screen.width/2,0F,Screen.width/2,Screen.height);
public void Update() {
distanceTraveled = transform.localPosition.x;
}
public void FixedUpdate() {
Movement ();
}
public void Movement() {
//Accelerometer Control up/down
Vector3 dirAcc = Vector3.zero;
dirAcc.y = Input.acceleration.y*.5F;
transform.Translate(0F,0F,dirAcc.y);
if(Input.touchCount != 0){
Vector2 curTouch = Input.GetTouch(0).position;
if (leftHalf.Contains (curTouch)){
transform.Translate(-Vector3.right*speed*Time.deltaTime);
}else{
if (rightHalf.Contains(curTouch)){
transform.Translate(Vector3.right*speed*Time.deltaTime);
}
}
}else{
if(Input.touchCount == 0){
transform.Translate(Vector3.right*speed*Time.deltaTime);
}
}
}
}
What I'm trying to achieve is that the play is able to control the up/down movement of the character via accelerometer and right/left movement by touching the right/left side of the screen.
With the above code the touch area does not matter the character will always move backwards and accelerometer input is entirely ignored. What bothers me is that the above code (accelerometer part only) worked with transform.Translate instead of rigidbody.AddForce. But from what I've read on the internet I'm going to need rigidbodies if I want collisions.
So any help or advice regarding code structure/syntax or solution towards my problem is appreciated.
On first look for the up/ down movement you are trying to change the z value which is possibly the forward movement. Another issue could be for your calculation of the touch position. It is better to covert the touch position to the camera's viewport and check for the left or right positions. A simple implementation would be like this. Add this code to any object in the scene.
using UnityEngine;
using System.Collections;
public class PlayerScript: MonoBehaviour {
public void FixedUpdate ( ) {
Movement ( );
}
public void Movement ( ) {
//Accelerometer Control up/down
Vector3 dirAcc = Vector3.zero;
Vector3 mousePosition = Vector3.zero;
#if UNITY_ANDROID
dirAcc.y = Input.acceleration.y * .5F;
if(Input.touchCount > 0) {
mousePosition = Camera.main.ScreenToViewportPoint ( Input.touches[0].position );
}
#elif UNITY_EDITOR
if( Input.GetKey ( KeyCode.W ) ) {
dirAcc = Vector3.up * Time.deltaTime;
}
else if(Input.GetKey(KeyCode.S)) {
dirAcc = Vector3.down * Time.deltaTime;
}
if(Input.GetMouseButton(0)) {
mousePosition = Camera.main.ScreenToViewportPoint ( Input.mousePosition );
}
#endif
if( mousePosition.x > 0 ) {
if( mousePosition.x > 0.5f ) { // right side of the screen
dirAcc += Vector3.right * Time.deltaTime;
}
else {
dirAcc += Vector3.left * Time.deltaTime;
}
}
transform.Translate ( dirAcc );
}
}
I have not tested this on a device.
I'm making a game with Andengine and i'm stuck for 2 days on shooting from a rotating sprite. I'm not a hero at geometry and already ask a teacher but he could also not provide me the correct answer. So who is a mathematics hero and help me out :).
The problem is that i cannot figure out where the bullet has to spawn in front of the turret. Rotating and finding the destination where the bullet has to go is no problem. It only about the spawn point.
I removed a lot of not-interesting-code for this question.
Okay so here is the rotating code from the turret-rotation:
public class AlienShip extends Ship {
public static final float BASE_ROTATION_SPEED = 0.25f;
public static final int DEFAULT_IMAGE_ROTATION = 90; //90 degrees
protected PlayerShip ship;
public AlienShip(float pX, float pY, TextureRegion pTextureRegion,
VertexBufferObjectManager pVertexBufferObject,FixedStepPhysicsWorld pw, int baseDurability) {
super(pX, pY, pTextureRegion, pVertexBufferObject, pw, baseDurability);
}
public void rotateToPlayer()
{
if (ship != null) {
float dX = this.getX() - ship.getX();
float dY = this.getY() - ship.getY();
float angle = (float) Math.atan2(-dY, dX);
float rotation = MathUtils.radToDeg(angle) + DEFAULT_IMAGE_ROTATION;
RotationModifier rotMod = new RotationModifier(BASE_ROTATION_SPEED, this.getRotation(), rotation);
this.registerEntityModifier(rotMod);
}
}
public void rotateToInitPos() {
RotationModifier rotMod = new RotationModifier(BASE_ROTATION_SPEED, this.getRotation(), 0);
this.registerEntityModifier(rotMod);
}
}
The code above is working fine.
Here is the code from the laser that the ship is shooting.
Read the comments to find out witch part is not working.
public class GameScene extends Scene {
protected PlayerShip playerShip;
private SpawnCallback createShootCallback(boolean player) {
return new SpawnCallback() {
#Override
public void spawn(SpawnTimer spawnTimer) {
PhysicsSprite laser = null;
AlienShip alienShip = (AlienShip) spawnTimer.getPhysicsSprite();
// laser = alienMissilePool.getMissileFromPool(x,y)
//spawn the laser in front of the rotating ship [Not working :( ]
laser = alienMissilePool.getMissileFromPool( ( alienShip.getX() * FloatMath.cos(MathUtils.degToRad(rotation)) - ((1280 - alienShip.getY() - alienShip.getY()/2) * FloatMath.sin(MathUtils.degToRad(rotation)) ) ) ,
( alienShip.getX() * FloatMath.sin(MathUtils.degToRad(rotation)) + ((1280 - alienShip.getY() - alienShip.getY()/2) * FloatMath.cos(MathUtils.degToRad(rotation)) ) ) );
//Set the rotation from the laser same to the ship rotation [Is working perfectly].
float rotation = alienShip.getRotation();
laser.setRotation(rotation);
//Set laser speed and direction [Is working perfectly]
float pX = 0.01f * (playerShip.getX() - laser.getX());
float pY = 0.01f * (playerShip.getY() - laser.getY());
laser.getSpriteBody().setLinearVelocity(pX, pY);
spawnPhysicsSprite(laser);
}
};
}
}
Here is a link to a drawing that shows the x-axis and y-axis values.
http://s24.postimg.org/citz29339/gamescene.png
Thank you!
Instead of getting into maths, why don't you put an object (Entity) positioned on the place of the turret and use it's position as the spawn point for the laser ?
so your turret would have an Entity attached to it on the place of the gun.
tell me if you need an example code
UPDATE Looks like this is a problem because of the static notification bar on tablet because of the lack of hardware buttons. I just didn't think about that. Anyway, in the case of the TF101 it returns a resolution of 1280x752 so about 1.702 (80 : 47) ratio. If I use a suitable unit size, like 33.5 or 11.75 vertically I get the proper scaling and this seems to fix the problem of skewed pixels.
END UPDATE
I've been setting up a game using 16x16 units for my tiled maps. I am using the resolution 1280x800 on both my desktop and android projects, I'm testing this to get a sense of how it will look on my TF101 asus tablet. I currently use a camera with units of 20x12.5 (wxh) and notice no pixel scaling on my desktop project, but when I run the game on my android I get weird scaling, and a green horizontal line. I can also move about quarter cell further along the x-axis on the tablet, shown in the screen shots. The pixels on the android project don't seem uniform at all.
I set the verticalTiles amount to 12.5f, then calculate the horizontalTiles amount as
verticalTiles = 12.5f;
...
horizontalTiles = (float) width / (float) height * verticalTiles;
camera = new OrthographicCamera(horizontalTiles, verticalTiles);
I'm aiming for devices with different aspect ratios to simply see more or less of the map, but can't seem to get working correctly. Any help would be appreciated.
Android Capture - http://imageshack.us/f/7/dsvg.png/ - notice the highlights on the roof edges, they are not uniform at all.
Desktop Capture - http://imageshack.us/f/853/5itv.png/
Current MainGame class
package com.bitknight.bqex;
/* Bunch of imports */
public class MainGame implements ApplicationListener {
private OrthographicCamera camera;
private SpriteBatch spriteBatch;
private TiledMap map;
private OrthogonalTiledMapRenderer mapRenderer;
private Texture texture;
private Texture clothArmor;
private Sprite sprite;
private BitmapFont font;
private float horizontalTiles = 0;
private float verticalTiles = 12.5f;
private int hoverTileX = 0;
private int hoverTileY = 0;
private TiledMapTileLayer layer;
private Cell cell;
private TiledMapTile canMoveToTile;
private TiledMapTile cannotMoveToTile;
private AnimatedTiledMapTile animatedStopTile;
private AnimatedTiledMapTile animatedGoTile;
private Texture spriteSheet;
private TextureRegion region;
private Player player;
float h, w;
float ppuX, ppuY;
#Override
public void create() {
// Setup the animated tiles
Array<StaticTiledMapTile> tileArray;
// Start position on the sheet
int startX = 192;
int startY = 1568;
spriteSheet = new Texture(Gdx.files.internal("data/maps/tilesheet.png"));
spriteSheet.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
// We are trying to load two strips of 4 frames, 8 total
for( int i = 0; i < 2; ++i ) {
tileArray = new Array<StaticTiledMapTile>(4);
for( int j = 0; j < 4; ++j ) {
region = new TextureRegion(spriteSheet, startX, startY, 16, 16);
tileArray.add(new StaticTiledMapTile(region));
startX += 16;
}
if( i == 0 ) {
animatedStopTile = new AnimatedTiledMapTile(1/10f, tileArray);
} else {
animatedGoTile = new AnimatedTiledMapTile(1/10f, tileArray);
}
}
// Load the map
map = new TmxMapLoader().load("data/maps/base.tmx");
// Setup the two tiles that show movable and not movable sprites
canMoveToTile = map.getTileSets().getTileSet(0).getTile(1959);
canMoveToTile.setBlendMode(BlendMode.ALPHA);
cannotMoveToTile = map.getTileSets().getTileSet(0).getTile(1958);
cannotMoveToTile.setBlendMode(BlendMode.ALPHA);
// Manually create the layer used to show the cursor sprites
layer = new TiledMapTileLayer(100, 100, 16, 16);
layer.setName("display");
cell = new Cell();
cell.setTile(canMoveToTile);
layer.setOpacity(1f);
mapRenderer = new OrthogonalTiledMapRenderer(map, 1/16f);
spriteBatch = new SpriteBatch();
font = new BitmapFont(Gdx.files.internal("data/consolas.fnt"), false);
font.setScale(0.6f);
texture = new Texture(Gdx.files.internal("data/maps/tilesheet.png"));
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
clothArmor = new Texture(Gdx.files.internal("data/img/native/clotharmor.png"));
region = new TextureRegion(clothArmor, 32, 256, 32, 32);
sprite = new Sprite(region);
sprite.setOrigin(0.5f, 0.5f);
sprite.setPosition(0f - 0.5f, 0f);
sprite.setSize(2, 2);
// Setup player and associated animations
Array<TextureRegion> regions = new Array<TextureRegion>();
player = new Player();
}
#Override
public void dispose() {
spriteBatch.dispose();
texture.dispose();
clothArmor.dispose();
spriteSheet.dispose();
}
#Override
public void render() {
player.update(Gdx.graphics.getDeltaTime());
camera.update();
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if( Gdx.input.isKeyPressed(Input.Keys.ESCAPE) ) {
Gdx.app.exit();
}
// Clear the last cell
layer.setCell(hoverTileX, hoverTileY, null);
// Convert screen coordinates to world coordinates
Vector3 worldCoordinates = new Vector3(Gdx.input.getX(0), Gdx.input.getY(0), 0);
camera.unproject(worldCoordinates);
hoverTileX = (int)(worldCoordinates.x);
hoverTileY = (int)(worldCoordinates.y);
TiledMapTileLayer layer = (TiledMapTileLayer)map.getLayers().get("collision");
if( Gdx.input.isTouched(0) ) {
//sprite.setPosition(hoverTileX - 0.5f, hoverTileY);
player.pos.x = hoverTileX - 0.5f;
player.pos.y = hoverTileY - 0.25f;
cell.setTile(animatedGoTile);
} else {
if (layer.getCell(hoverTileX, hoverTileY) != null) {
cell.setTile(cannotMoveToTile);
} else {
cell.setTile(canMoveToTile);
}
}
layer.setCell(hoverTileX, hoverTileY, cell);
mapRenderer.setView(camera);
mapRenderer.render();
mapRenderer.getSpriteBatch().begin();
mapRenderer.renderTileLayer(layer);
mapRenderer.getSpriteBatch().end();
spriteBatch.setProjectionMatrix(camera.combined);
spriteBatch.begin();
player.render(spriteBatch);
spriteBatch.end();
}
#Override
public void resize(int width, int height) {
horizontalTiles = (float) width / (float) height * verticalTiles;
camera = new OrthographicCamera(horizontalTiles, verticalTiles);
w = width;
h = height;
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
Looks like this is a problem because of the static notification bar on tablet because of the lack of hardware buttons. I just didn't think about that. Anyway, in the case of the TF101 it returns a resolution of 1280x752 so about 1.702 (80 : 47) ratio. If I use a suitable unit size, like 33.5 or 11.75 vertically I get the proper scaling and this seems to fix the problem of skewed pixels.
Also, while this is good for the TF101 tablet in my case it's not really a great solution. Here is a Gemserk series that talks about a nice solution.
http://blog.gemserk.com/2013/01/22/our-solution-to-handle-multiple-screen-sizes-in-android-part-one/
I am implementing my own camera Activity.
To rotate the captured image I need to know the orientation at the time of shooting.
Is there a higher level API for the sensor values pitch and roll that tells me my device is oriented in:
top-down - holding the phone normal, portrait
down-top
right-left - landscape the top of the phone is on the right side
left-right - landscape the top of the phone is on the left side
Or is there any other way to geht it directly from the system?
sadly ,cameras work in a weird way on android when they are not in landscape mode (which is the "natural" orientation for camera on android) .
best thing to do is set the activity to be in landscape mode , and add the onConfigurationChanged event (and add the android:configChanges="orientation" into the manifest) in order to get the current orientation .
when capturing the image, check the orientation and act according to whatever logic you wish to have.
Ok solved my problem to a certain point so that it works for me and I left out the down-top recognition.
public class DeviceOrientation {
public static final int ORIENTATION_PORTRAIT = 0;
public static final int ORIENTATION_LANDSCAPE_REVERSE = 1;
public static final int ORIENTATION_LANDSCAPE = 2;
public static final int ORIENTATION_PORTRAIT_REVERSE = 3;
int smoothness = 1;
public float averagePitch = 0;
public float averageRoll = 0;
public int orientation = ORIENTATION_PORTRAIT;
private float[] pitches;
private float[] rolls;
public DeviceOrientation(int smoothness) {
this.smoothness = smoothness;
pitches = new float[smoothness];
rolls = new float[smoothness];
}
public void addSensorEvent(SensorEvent event) {
azimuth = event.values[0];
averagePitch = addValue(event.values[1], pitches);
averageRoll = addValue(event.values[2], rolls);
orientation = calculateOrientation();
}
private float addValue(float value, float[] values) {
float average = 0;
for(int i=1; i<smoothness; i++) {
values[i-1] = values[i];
average += values[i];
}
values[smoothness-1] = value;
average = (average + value)/smoothness;
return average;
}
/** handles all 4 possible positions perfectly */
private int calculateOrientation() {
// finding local orientation dip
if (((orientation == ORIENTATION_PORTRAIT || orientation == ORIENTATION_PORTRAIT_REVERSE)
&& (averageRoll > -30 && averageRoll < 30))) {
if (averagePitch > 0)
return ORIENTATION_PORTRAIT_REVERSE;
else
return ORIENTATION_PORTRAIT;
} else {
// divides between all orientations
if (Math.abs(averagePitch) >= 30) {
if (averagePitch > 0)
return ORIENTATION_PORTRAIT_REVERSE;
else
return ORIENTATION_PORTRAIT;
} else {
if (averageRoll > 0) {
return ORIENTATION_LANDSCAPE_REVERSE;
} else {
return ORIENTATION_LANDSCAPE;
}
}
}
}
Explanation:
If I am in portrait-mode and tillt the mobil forward until it is in a horizontal position it would switch to landscape due to the rest of the code.
Therefore I check if it is in portrait and make the conditions hard to leafe this mode.
This is what I ment with the local dip.
The rest just divides into all 3 directions.
One thing is bad. If the device is in landscape_x and get tillt some degrees backwards, the pich jumps from ~2 to ~175. At that point my code is fliping inbetween landscape and portrait.
The smoothness will smooth the value for the sensor data by combining the last n values and calculating the average. It isn't realy necesary.
I hope this will help others. If you can improve the code further, please let me know.