- Home /
Question by
Propellent · May 28, 2016 at 05:19 AM ·
camera rotaterotation axis
Camera moves with player in doom-like
I'm attempting to make a game with the same camera controls and movement as Doom, basically the ability to move forward and backward, strafe, and rotate the view left or right. But I can't seem to figure out how to go about aligning the player with it's child, the main camera. I can turn the camera left and right, but I don't move in the direction the camera is facing. Would it be possible to cast a ray from the camera and use it's direction to orient the player? Here's my movement code so far
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour
{
public float runSpeed = 240f;
public float walkSpeed = 120f;
public float turnSpeed = 60f;
Vector3 movement;
//bool isRunning;
Rigidbody playerRigidbody;
void Awake()
{
playerRigidbody = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
float t = Input.GetAxisRaw("Turn");
Move(h, v);
Turning(t);
}
void Move(float h, float v)
{
movement.Set(h, 0f, v);
//if(!isRunning)
movement = movement * walkSpeed * Time.deltaTime;
// else
//movement = movement.normalized * runSpeed * Time.deltaTime;
playerRigidbody.MovePosition(transform.position + movement);
}
void Turning(float t)
{
float newRotation = t * turnSpeed;
newRotation *= Time.deltaTime;
transform.Rotate(0, newRotation, 0);
}
}
Comment
Probably best to align the camera to the players rotation ins$$anonymous$$d of the other way since you are moving the player and not the camera.