- Home /
Moving while facing the camera
Hello there, I'm just learning the basics and I've found myself in a hard situations that I can't get out, I was trying to make a character move to the both vertical and horizontal, but yet moving the camera. What I want to make my character is, every time I press Up or Backwards, it follows the "camera" directions, which actually is the player direction because I've made the camera child of the player and made it rotate follow the cursor. Here is my code:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float speed;
public float mouseSpeed;
void FixedUpdate()
{
float moveHorizontal =Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
float mouseX = Input.GetAxis ("Mouse X");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * speed * Time.deltaTime;
if (mouseX > 0) {
transform.Rotate (Vector3.up * mouseSpeed);
}
if (mouseX < 0) {
transform.Rotate(Vector3.up * -mouseSpeed);
}
}
}
Comment