Hi guys,
I am new to unity and completed a couple tutorials and am now working on my own game. Will be a horror 3rd person shooter set in a pitch black mine. I am working on my player movement script and am having an issue. I have the player moving back and forth, side to side with no problem. I have the camera rotating with the mouse. Now I am trying to make the player turn and move in the direction of the mouse. Any tips? Here is what I have so far for a movement script. Thank you in advance.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
public float speed = 6f;
public float speedH = 2.0f;
public float speedV = 2.0f;
private float yaw = 0.0f;
private float pitch = 0.0f;
Vector3 movement;
Animator anim;
Rigidbody playerrb;
void Awake ()
{
anim = GetComponent <Animator> ();
playerrb = GetComponent <Rigidbody> ();
}
void FixedUpdate ()
{
float h = Input.GetAxisRaw ("Horizontal");
float v = Input.GetAxisRaw ("Vertical");
Move (h, v);
Animating (h, v);
}
void Update ()
{
yaw += speedH * Input.GetAxis ("Mouse X");
pitch -= speedV * Input.GetAxis ("Mouse Y");
transform.eulerAngles = new Vector3 (pitch, yaw, 0.0f);
}
void Move (float h, float v)
{
movement.Set (h, 0f, v);
movement = movement.normalized * speed * Time.deltaTime;
playerrb.MovePosition (transform.position + movement);
}
void Animating ( float h, float v)
{
bool walking = h != 0f || v != 0f;
anim.SetBool ("IsWalking", walking);
}
}