I have a sphere as player and I want it to move relative to camera and not world.
When I rotate camera along Y-axis by 90 or 270 degree and give input, it gives opposite output than expected.
Here is my script attached to sphere:
using System.Collections;
using UnityEngine;
public class mover : MonoBehaviour {
public Rigidbody sphere;
public GameObject cameraa;
void Start ()
{
sphere = GetComponent<Rigidbody> ();
}
void Update ()
{
// getting input in controlVector
Vector3 controlVector = new Vector3 (Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
// transforming direction from world space to local space[to camera]
Vector3 localVectToCam = cameraa.transform.InverseTransformDirection (controlVector);
//applying inputs
sphere.AddForce (localVectToCam * 10);
}
}
when camera's direction is same as world and when camera is rotated along Y-axis by 180 degree, it works fine.
But on rotating camera along Y-axis by 90 and 270 degree is gives opposite output.
example:
camera rotation along Y-axis = 90 degree [ i.e (0,90,0) ]
input given = (0,0,1)
output got = (-1,0,0)
expected output = (1,0,0)
Can you help me to understand and correct it?