- Set up clamps for the ability to move. Check out Mathf.clamp. You should be able to go from there. You’re going to only want to clamp the camera’s Y coordinate, as you still want to rotate 360.
3&4: Joystick tutorial:
Note: This is a c# script, and i’m not sure if it works with java.
5: To keep things in position use this awesome script by http://pixelplacement.com/ (sorry I can’t link directly, their website is acting funky right now, full credit to them)
To use:
transform.ScreenPlacement(ScreenPosition.LowerLeft, new Vector2(60,55));
This will move the object the script is located on to the camera’s lower left anchor, with an X from the edge of 60, and a Y from the edge of 55.
Add this to your project in a separate script:
using UnityEngine;
/// <summary>
/// Screen positions for use with the ScreenPlacement transform and gameObject extension for the 9 positions around the screen edge.
/// </summary>
public enum ScreenPosition {UpperLeft, UpperMiddle, UpperRight, Left, Middle, Right, LowerLeft, LowerMiddle, LowerRight};
public static class ScreenPlacementExtension{
//Add instructions
//GameObject overrides:
/// <summary>
/// Places a GameObject at one of the 9 positions around the screen edge.
/// </summary>
/// <param name="target">
/// A <see cref="GameObject"/>
/// </param>
/// <param name="position">
/// A <see cref="ScreenPosition"/>
/// </param>
public static void ScreenPlacement(this GameObject target, ScreenPosition position){
DoScreenPlacement(target.transform, position, Vector2.zero, Camera.main);
}
/// <summary>
/// Places a GameObject at one of the 9 positions around the screen edge.
/// </summary>
/// <param name="target">
/// A <see cref="GameObject"/>
/// </param>
/// <param name="position">
/// A <see cref="ScreenPosition"/>
/// </param>
/// <param name="renderingCamera">
/// A <see cref="Camera"/>
/// </param>
public static void ScreenPlacement(this GameObject target, ScreenPosition position, Camera renderingCamera){
DoScreenPlacement(target.transform, position, Vector2.zero, renderingCamera);
}
/// <summary>
/// Places a GameObject at one of the 9 positions around the screen edge.
/// </summary>
/// <param name="target">
/// A <see cref="GameObject"/>
/// </param>
/// <param name="position">
/// A <see cref="ScreenPosition"/>
/// </param>
/// <param name="pixelsFromEdge">
/// A <see cref="Vector2"/>
/// </param>
public static void ScreenPlacement(this GameObject target, ScreenPosition position, Vector2 pixelsFromEdge){
DoScreenPlacement(target.transform, position, pixelsFromEdge, Camera.main);
}
/// <summary>
/// Places a GameObject at one of the 9 positions around the screen edge.
/// </summary>
/// <param name="target">
/// A <see cref="GameObject"/>
/// </param>
/// <param name="position">
/// A <see cref="ScreenPosition"/>
/// </param>
/// <param name="pixelsFromEdge">
/// A <see cref="Vector2"/>
/// </param>
/// <param name="renderingCamera">
/// A <see cref="Camera"/>
/// </param>
public static void ScreenPlacement(this GameObject target, ScreenPosition position, Vector2 pixelsFromEdge, Camera renderingCamera){
DoScreenPlacement(target.transform, position, pixelsFromEdge, renderingCamera);
}
//Transform overrides:
/// <summary>
/// Places a Transform at one of the 9 positions around the screen edge.
/// </summary>
/// <param name="target">
/// A <see cref="Transform"/>
/// </param>
/// <param name="position">
/// A <see cref="ScreenPosition"/>
/// </param>
public static void ScreenPlacement(this Transform target, ScreenPosition position){
DoScreenPlacement(target.transform, position, Vector2.zero, Camera.main);
}
/// <summary>
/// Places a Transform at one of the 9 positions around the screen edge.
/// </summary>
/// <param name="target">
/// A <see cref="Transform"/>
/// </param>
/// <param name="position">
/// A <see cref="ScreenPosition"/>
/// </param>
/// <param name="renderingCamera">
/// A <see cref="Camera"/>
/// </param>
public static void ScreenPlacement(this Transform target, ScreenPosition position, Camera renderingCamera){
DoScreenPlacement(target.transform, position, Vector2.zero, renderingCamera);
}
/// <summary>
/// Places a Transform at one of the 9 positions around the screen edge.
/// </summary>
/// <param name="target">
/// A <see cref="Transform"/>
/// </param>
/// <param name="position">
/// A <see cref="ScreenPosition"/>
/// </param>
/// <param name="pixelsFromEdge">
/// A <see cref="Vector2"/>
/// </param>
public static void ScreenPlacement(this Transform target, ScreenPosition position, Vector2 pixelsFromEdge){
DoScreenPlacement(target.transform, position, pixelsFromEdge, Camera.main);
}
/// <summary>
/// Places a Transform at one of the 9 positions around the screen edge.
/// </summary>
/// <param name="target">
/// A <see cref="Transform"/>
/// </param>
/// <param name="position">
/// A <see cref="ScreenPosition"/>
/// </param>
/// <param name="pixelsFromEdge">
/// A <see cref="Vector2"/>
/// </param>
/// <param name="renderingCamera">
/// A <see cref="Camera"/>
/// </param>
public static void ScreenPlacement(this Transform target, ScreenPosition position, Vector2 pixelsFromEdge, Camera renderingCamera){
DoScreenPlacement(target.transform, position, pixelsFromEdge, renderingCamera);
}
//Placement execution:
private static void DoScreenPlacement(this Transform target, ScreenPosition position, Vector2 pixelsFromEdge, Camera renderingCamera){
Vector3 screenPosition = Vector3.zero;
float zPosition = -renderingCamera.transform.position.z + target.position.z;
switch (position) {
//uppers:
case ScreenPosition.UpperLeft:
screenPosition = renderingCamera.ScreenToWorldPoint(new Vector3(pixelsFromEdge.x, Screen.height-pixelsFromEdge.y, zPosition));
break;
case ScreenPosition.UpperMiddle:
screenPosition = renderingCamera.ScreenToWorldPoint(new Vector3( (Screen.width/2)+pixelsFromEdge.x, Screen.height-pixelsFromEdge.y, zPosition));
break;
case ScreenPosition.UpperRight:
screenPosition = renderingCamera.ScreenToWorldPoint(new Vector3(Screen.width-pixelsFromEdge.x, Screen.height-pixelsFromEdge.y, zPosition));
break;
//mids:
case ScreenPosition.Left:
screenPosition = renderingCamera.ScreenToWorldPoint(new Vector3(pixelsFromEdge.x, (Screen.height/2) - pixelsFromEdge.y, zPosition));
break;
case ScreenPosition.Middle:
screenPosition = renderingCamera.ScreenToWorldPoint(new Vector3((Screen.width/2) + pixelsFromEdge.x, (Screen.height/2) - pixelsFromEdge.y, zPosition));
break;
case ScreenPosition.Right:
screenPosition = renderingCamera.ScreenToWorldPoint(new Vector3(Screen.width-pixelsFromEdge.x, (Screen.height/2)-pixelsFromEdge.y, zPosition));
break;
//lowers:
case ScreenPosition.LowerLeft:
screenPosition = renderingCamera.ScreenToWorldPoint(new Vector3(pixelsFromEdge.x, pixelsFromEdge.y, zPosition));
break;
case ScreenPosition.LowerMiddle:
screenPosition = renderingCamera.ScreenToWorldPoint(new Vector3((Screen.width/2)+pixelsFromEdge.x, pixelsFromEdge.y, zPosition));
break;
case ScreenPosition.LowerRight:
screenPosition = renderingCamera.ScreenToWorldPoint(new Vector3(Screen.width-pixelsFromEdge.x, pixelsFromEdge.y, zPosition));
break;
}
target.transform.position = screenPosition;
}
}
I also found another reference to the ScreePosition script here:
http://www.1678zz.com/projects/rear-pro/repository/revisions/c09d86106b4f4cc773b5221235a876f066fd78d7/show/Assets/Extensions/StansAssetsPreviewUI/Scripts/ScreenPlacementExtension