Okay, so what I’m trying to do is, make it so when I tilt my phone using the accelerometer the object below stays on the x axis and can only move left and right. I managed to do this, but I need it to clamp so it wont go outside of the size of the screen you’re playing on. please help me.
using UnityEngine;
using System.Collections;
public class Basket : MonoBehaviour {
public Camera cam;
private float maxWidth;
private bool canControl;
// Use this for initialization
void Start () {
if (cam == null) {
cam = Camera.main;
}
canControl = false;
Vector3 upperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
Vector3 targetWidth = cam.ScreenToWorldPoint (upperCorner);
maxWidth = targetWidth.x;
}
// Update is called once per frame
void Update () {
if (canControl) {
Vector3 acceleration = Input.acceleration;
transform.Translate(Input.acceleration.x, 0, -Input.acceleration.z);
}
}
public void ToggleControl (bool toggle) {
canControl = toggle;
}
}
Convert your object world position to screen position and then check it with screen positions, for example with width of screen. So if object goes to right end of screen you can make it stop moving.
Vector3 ObjectScreenPosition = Camera.main.WorldToScreenPoint(transform.position);
if (ObjectScreenPosition.x >= Screen.width)
{
print("Object is at right end of screen, stop moving right or it will go off screen");
}
I’m a little confused. (This is my first project in class) I switched out the code like you said but do I not put Mathf.Clamp anywhere? I know this probably is basic knowledge for some but I’ve been at it for hours
this is what it looks like after the code you gave me.
using UnityEngine;
using System.Collections;
public class HatController : MonoBehaviour {
public Camera cam;
private float maxWidth;
private bool canControl;
// Use this for initialization
void Start () {
if (cam == null) {
cam = Camera.main;
}
canControl = false;
Vector3 upperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
Vector3 ObjectScreenPosition = Camera.main.WorldToScreenPoint (transform.position);
if (ObjectScreenPosition.x >= Screen.width) {
print (“Object is at right end of screen, stop moving right or it will go off screen”);
}
}
// Update is called once per frame
void Update () {
if (canControl) {
Vector3 acceleration = Input.acceleration;
transform.Translate(Input.acceleration.x, 0, -Input.acceleration.z);
}
}
public void ToggleControl (bool toggle) {
canControl = toggle;
}
}
Please use this BB code when you are posting you code on forum, it’s easier to read: “[“code=CSharp]Your Code Here[/code”]” < Remove( " ) so it works.
First of all you need to check that positions under Update() method not in Start(). Start() method is called on the frame when a script is enabled and that happens only once, while Update method is called every frame.
Test this code to see how it works, create new scene and attach it to some sphere,cube or some other object. Then move it right or left on screen to see how the code works. Also you have example project in attachments to download.
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
void Update()
{
Vector3 ObjectScreenPosition = Camera.mainCamera.WorldToScreenPoint(transform.position);
if (ObjectScreenPosition.x >= Screen.width)
{
print("Object is at right end of screen, stop moving right or it will go off screen");
}
if(ObjectScreenPosition.x <= 0)
{
print("Object is at left end of screen, stop moving left or it will go off screen");
}
}
}