- Home /
How to move the Rigid body with respect to the camera angle
Not trying to beat a dead horse here because I know this question has been asked so many times. But I'm still having trouble trying to figure this out. I'm working on modifying the first tutorial from unity. I got my camera to move horizontally left and right, But when I go forward even though I am facing to either the left or right, When I press "up" I still am moving along the Z axis. So what I think I need to do(correct me if I'm wrong) is to change the Z axis with respect to the camera's current angle. This in turn will allow me to move forward in the direction of the camera?
This is what I have tried to do "code wise". This is the Player class.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
public Text countText;
public Text winText;
private int count;
public Vector3 movement;
public CameraController c;
private Rigidbody rb;
public Transform target;
public Vector3 targetDir;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody> ();
count = 0;
winText.text = "";
}
// Update is called once per frame
//called before rendering a frame
void Update () {
transform.forward = new Vector3 (rb.position.x + c.directionView, 0, 0);
transform.right = new Vector3 (0, 0, rb.position.y + c.directionView);
print (transform.forward);
}
//called just before calling any physics calculations
void FixedUpdate () {
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
movement = new Vector3 (moveHorizontal,0.0f,moveVertical);
rb.AddForce (movement * speed);
//rb.AddRelativeForce (movement*speed);
}
void OnTriggerEnter(Collider other){
if(other.gameObject.CompareTag("Pick up")){
other.gameObject.SetActive(false);
count += 1;
setCountText ();
}
}
void setCountText(){
countText.text = "Count : " + count.ToString ();
if (count >= 8) {
winText.text = "You Win!";
}
}
}
This is the Camera class.
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
PlayerController pc;
public GameObject player;
public float directionView;//camera direction left and right
// Use this for initialization
void Start () {
}
// Update is called once per frame
void LateUpdate () {
transform.position = player.transform.position;
//transform.eulerAngles = new Vector3 (50, 0, 0);
if (Input.GetKey ("right")){
turnRight ();
}
else if (Input.GetKey ("left")) {
turnLeft ();
}
else if (Input.GetKey ("up")) {
transform.rotation = Quaternion.Euler (0, directionView, 0);
}
else if (Input.GetKey ("down")) {
transform.rotation = Quaternion.Euler (0, directionView, 0);
}
print (directionView);
}
void turnRight(){
directionView += 20;
transform.rotation = Quaternion.Euler (0, directionView, 0);
if (directionView == 360) {
directionView = 0;
}
}
void turnLeft(){
directionView -= 20;
transform.rotation = Quaternion.Euler (0, directionView, 0);
if (directionView == -360) {
directionView = 0;
}
}
}
Your answer
Follow this Question
Related Questions
Moving Randomly 1 Answer
"Fake" Vector3.forward? 3 Answers
How to rotate an object to face the direction it's going? 1 Answer
jump to the direction of the getaxis 1 Answer