- Home /
Question by
HipsterSoap · May 30, 2015 at 04:51 PM ·
cameracamera-movementplayer movement
Make a ridgid body move in the direction the camera is facing
So what I am trying to do is when I press foreword the player(a ridgidbody sphere) moves in the direction the camera is facing. Think something like marble blast ultra.
Player Code(Movement)
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
private Rigidbody rb;
void Start ()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
}
Camera Code
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public float turnSpeed;
public GameObject player;
public float height = 1f;
public float distance = 2f;
private Vector3 offset;
void Start(){
offset = new Vector3 (0, height, distance);
}
void LateUpdate(){
offset = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offset;
transform.position = player.transform.position + offset;
transform.LookAt(player.transform.position);
}
}
Thanks!
Comment