hi I got this piece of code
direction = Vector3(Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
rotation = Quaternion.LookRotation(direction, Vector3.up);
transform.rotation = rotation;
it makes the object face the direction the analogue stick is facing. But the problem is its in world space, and I want it to be relative to the camera position. Can someone tell me how to edit the code to make it so.
Try:
direction = Vector3(Camera.main.transform.position + Vector3(Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical")));
thnx but it didn’t work, im getting this error message
“BCE0024: The type ‘UnityEngine.Vector3’ does not have a visible constructor that matches the argument list ‘(UnityEngine.Vector3)’.”
Are you using C# or JavaScript/UnityScript?
If you’re using C# then try this:
Transform mainCamera = Camera.main.transform;
direction = mainCamera.positon + new Vector3(mainCamera.right * Input.GetAxis("Horizontal"), mainCamera.up * Input.GetAxis("Vertical"), mainCamera.forward).
It’s sloppy but I’m not sure I understand what you’re trying to accomplish. Let me know if it works.
using java script please try again
Oh I’ll get this:
var mainCamera = Camera.main.transform;
var direction = mainCamera.positon + Vector3(mainCamera.right * Input.GetAxis("Horizontal"), mainCamera.up * Input.GetAxis("Vertical"), mainCamera.forward);
var rotation = Quaternion.LookRotation(direction, Vector3.up);
transform.rotation = rotation;
I use C# so hopefully you can iron out any translation errors.
PS: Make sure the camera you’re using is tagged “MainCamera” or Camera.main will return null (or whatever other camera is tagged “MainCamera”).
thnx for that I’ll give it a go
Jallen182:
Oh I’ll get this:
var mainCamera = Camera.main.transform;
var direction = mainCamera.positon + Vector3(mainCamera.right * Input.GetAxis("Horizontal"), mainCamera.up * Input.GetAxis("Vertical"), mainCamera.forward);
var rotation = Quaternion.LookRotation(direction, Vector3.up);
transform.rotation = rotation;
I use C# so hopefully you can iron out any translation errors.
PS: Make sure the camera you’re using is tagged “MainCamera” or Camera.main will return null (or whatever other camera is tagged “MainCamera”).
You can’t do any of that, unity complains that A. your trying to multiply a vector3 times a single float value, and B. your trying to set the x,y,and z values of the vector 3 each as their own vector 3
This is prefectly fine. B is not though.
I remember seeing the Mechanim Locomotion Starter Kit has some code for transforming stick movement depending on camera. Check it out:
https://www.assetstore.unity3d.com/#/content/7673