- Home /
change character movement axis in respect to camera orientation?
Hi all, so a few days ago i did the basic "Unity roll-a-ball" tutorial and spent some time afterwards playing around with the character controller. so far, i have an empty player object 'player' that has a few children; 'Ball' object with a rigidbody and a PlayerController script to move it around that looks like this:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
private Rigidbody rb;
void Start () {
rb = GetComponent<Rigidbody> ();
}
void FixedUpdate () {
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
}
and 'Gyro' empty object with the main camera attached. the 'gyro' has a script that originally only kept the gyro object offset from the rotation of the Ball so that i could just attach the camera to it. i extended on its abilities a bit by adding in a chunk of code to allow the user to orbit the ball based on moving the mouse. the gyroScript looks like this:
using UnityEngine;
using System.Collections;
public class gyroScript : MonoBehaviour {
public GameObject target;
// the target in this case is the ball object
public float speed = 10f;
public float rotationSpeed = 100.0f;
private Vector3 offset;
void Start () {
offset = transform.position - target.transform.position;
}
void Update()
{
mouseOrbit();
}
void LateUpdate () {
transform.position = target.transform.position + offset;
// this keeps the gyro object stable in comparison to the ball object
}
void mouseOrbit()
{
float translation = Input.GetAxis("Mouse Y") * speed;
float rotation = Input.GetAxis("Mouse X") * rotationSpeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate(0, 0, translation);
transform.Rotate(0, rotation, 0);
// this rotates the gyro object about the Y axis by means of mouse movement
// because the camera is a child of this it rotates about with it
}
}
the code here does not rotate the camera but the gyro object which results in the camera being rotated about the object because it is parented.
My Question:
when i orbit the gyro, the player controller script does not adjust (e.i. to set the W key as the 'forward', D to right, ect...) based on the orientation of the gyro object because it is basing what is forward on the global horizontal and vertical as defined in the Player Controller Script.
how would i go about linking the two together so when i turn the gyro object and press to go 'forward', i go forward based on the direction the gyro object is facing rather than the global horizontal and vertical axis?
Answer by andycodes · Feb 10, 2016 at 11:10 PM
I discovered the solution!
It is required to set a target to the gyro object and delete the old vector3 and modify the add force attributes as such:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
private Rigidbody rb;
public GameObject target; //new code
// target is the object you will take the rotations from
void Start () {
rb = GetComponent<Rigidbody> ();
}
void FixedUpdate () {
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
//Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical); (old code)
rb.AddForce(moveVertical * target.transform.forward * speed); //new code
rb.AddForce(moveHorizontal * target.transform.right * speed); //new code
//rb.AddForce (movement * speed); (old code)
}
}
This works but only partly, the camera seems to teleport around every few seconds or so and if you hold both keys from a different axis like W and D or A and S it glitches out.