Question by
Vonricter · Apr 26, 2017 at 10:37 PM ·
camera-movementcharacter controllercharacter movementcharacter controllingcamera controls
Problem with game objects moving relative to the camera Y axis? [ANSWERED]
I recently picked up Unity primarily for the purpose of learning C#
right now I'm trying to tackle character/camera controller scripts but I'm having a bit of an issue with the game object moving in the literal direction of the camera, even if that means attempting to move upwards, or downwards through the floor depending on camera position
here is my code, i'm using unity 5.6
is there a way i can code around this, or should i start from a completely different approach?
void ControlPlayer()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
jumpInput = Input.GetAxis("Jump");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
movement = Camera.main.transform.TransformDirection(movement);
transform.rotation = Quaternion.LookRotation(movement);
transform.Translate (movement * movementSpeed * Time.deltaTime,
Space.World);
Comment
Answer by Vonricter · Apr 27, 2017 at 06:58 AM
Hey if anyone reads this and is wondering what my solution was, i achieved the type of camera relative movement i wanted with this code
void FixedUpdate () {
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
m_CamForward = Vector3.Scale(m_Cam.forward, new Vector3(1, 0, 1)).normalized;
m_Move = v*m_CamForward + h*m_Cam.right;
transform.Translate (m_Move * movementSpeed * Time.deltaTime, Space.World);
}
}