- Home /
Unity c# Flick object to direction regardless of camera angle
Hi, I have this following particular code to flick an object forward by swiping in the direction that i want. The issue is that my camera angle and gameobject itself are rotated from default so whenever i flick the object to the center or left, the flick isnt accurate. When i flick anywhere to the right, the object is thrown precisely. How can i adjust this code so that the object is throw to the world space direction regardless of camera rotation please. Thanks.
public class SwipeScript : MonoBehaviour
{
public float force = 2f;
public bool isTarget = false;
public float zFactor = 2f;
public Vector3 startPosition;
private Vector2 startSwipe;
private Vector2 endSwipe;
private Rigidbody currentBoxRb;
void Start()
{
currentBoxRb = StackerManager.currentBox.GetComponent<Rigidbody>();
startPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z);
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
startSwipe = Camera.main.ScreenToViewportPoint(Input.mousePosition);
}
if (Input.GetMouseButtonUp(0))
{
endSwipe = Camera.main.ScreenToViewportPoint(Input.mousePosition);
if (isTarget == true)
{
Swipe();
isTarget = false;
}
}
}
void Swipe()
{
Vector3 swipe = endSwipe - startSwipe;
swipe.z = swipe.y / zFactor;
swipe.y = -0.001f;
currentBoxRb.isKinematic = false;
currentBoxRb.AddForce(swipe * force, ForceMode.Impulse);
}
private void OnMouseDown()
{
//currentBoxRb.constraints = RigidbodyConstraints.None;
isTarget = true;
}
}
Why are you dividing swipe.y/zFactor ins$$anonymous$$d of simply setting swipe.z to swipe.y? I think that's totally going to screw your direction.
So the viewport coordinate system origin is at the bottom left. So you need to offset the x to center it on your game object, so that 0.5 in viewport coords will translate to 0, and similarly, 0 should translate to -0.5. So do: swipe.x-=0.5f (or do it in endSwipe and startSwipe, I'm not really sure)
Come to think of it, I think I'm wrong. I'll think more about it later and come back to fix it in case.
Back. So actually when you're subtracting here your end and start points you're getting a pure direction vector and what I said before doesn't really apply. If you swipe from bottom left to bottom right you get a 1,1. If you swipe from bottom right to top left you get a -1,1. These should work perfectly on your game object.
The only reason that occurs to me for the bad directions is what I said in my first comment, you're screwing swipe.z by assigning a division of swipe.y by zFactor. But that doesn't quite explain why your swipes don't work in the left region.
Are you sure your game object movement and your screen should be totally disconnected or do you on the other hand intend to make your perspective affect your screen input in some way?
Answer by GlenMicallef · Dec 08, 2018 at 10:03 PM
Hi, Thanks for your time and effort. Im still trying to figure it out. Coming to think of it, all i want really is to apply the swipe according to the current perspective of the game. I've tried adding some vector offsets but to no avail.
Ok Fixed it. Used raycast direction insteead. Thanks for the help.
Your answer