Changing the direction of what is considered "forward"
I am making a first person shooter similar to the original doom games and I have it set it so that I can move forward, backward, strafe, and rotate my view on the y axis only. However, when I start the scene, the character only moves forward in the initial direction it's facing. Is there is a way I can go about changing the code on my player so that it moves in the direction it rotates? My main camera is a child of the player if that makes a difference.
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.GetAxis("Turn");
Turning(t);
transform.Translate(transform.forward * Time.deltaTime);
Move(h, v);
}
void Move(float h, float v)
{
movement.Set(h, 0f, v);
//if(!isRunning)
movement = movement.normalized * 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);
playerRigidbody.rotation = (transform.rotation);
}
}
Your answer
Follow this Question
Related Questions
Orienting a instanced Objects long-axis to the same point 1 Answer
How to calculate and use new direction angle as a new forward direction. 1 Answer
Maintain fixed vision while the player turn 0 Answers
Selective Mobile screen rotation 1 Answer
Is it possible to restrict screen auto-orientation on a single scene? 1 Answer