- Home /
Player Movement Relative to Camera?
Hi all... for now my charter (in my case a ball) moves relative to the world and not the camera. How is it possible to make the ball move relative to the camera rather than the world?!
Here is my script...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
public float speed;
public float jump = 20f;
public Transform SpawnPoint;
private Rigidbody rb;
float fallZone = -20f;
bool isGrounded = true;
bool isSpawn = true;
float forward;
GameObject MainCamera;
void Start()
{
rb = GetComponent<Rigidbody>();
MainCamera = GameObject.Find("Main Camera");
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddRelativeForce(movement*speed);
}
private void Update()
{
bool player_jump = Input.GetButtonDown("Jump");
if (player_jump && isGrounded)
{
rb.AddForce(Vector3.up * jump);
}
if (rb.transform.position.y < fallZone)
{
rb.transform.position = SpawnPoint.transform.position;
rb.velocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = true;
}
if (!SpawnPoint)
{
isSpawn = true;
}
if (collision.gameObject.CompareTag("Finish"))
{
Application.LoadLevel("Level_2");
}
}
void OnCollisionExit(Collision collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = false;
}
if (!SpawnPoint)
{
isSpawn = true;
}
}
}
Answer by Liraseth · Oct 09, 2019 at 07:04 PM
Ok I found this to work well with the help of other posts. Make sure that at the start of the script there is...
public float speed;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
then the main part...
void FixedUpdate()
{
Vector3 movement;
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
GameObject cameraCompass = new GameObject("Camera Compass");
// "Correct" the compass so that it lies flat in the game plane
cameraCompass.transform.eulerAngles = new Vector3(0, GameObject.FindGameObjectWithTag("MainCamera").transform.eulerAngles.y, 0f);
Vector3 movementX = cameraCompass.transform.right * moveHorizontal;
Vector3 movementZ = cameraCompass.transform.forward * moveVertical;
movement = movementX + movementZ;
rb.AddForce(movement * speed);
Destroy(cameraCompass); // We don't need the Compass anymore!
}
Answer by visca_c · Oct 08, 2019 at 02:00 PM
I assume you want your camera to follow the target. The easiest way is to attach your camera to your character(which is to make it a child object of the character). You can do that by going into editor hierarchy view, drag the camera, and drop it onto your character.
Answer by GubaLord · Oct 08, 2019 at 02:09 PM
I am not sure if it's that you want, but try to multiply the movement with the rotation of the camera. Right before you add it as force.
movement = MainCamera.transform.rotation * movement;
When i put this in the code, the ball was struggling to know what it needs to do so it jest rolls back and forward
Answer by gusbmurphy · Oct 08, 2019 at 03:51 PM
I'm working on a little project where I think I've tackled a similar problem to what you're looking at. I'm using the WASD keys instead of an input axis, but I have a camera that follows the player, and I use this method to get the up, down, left and right directions relative to the camera:
private void SetUpCameraDirections()
{
GameObject cameraCompass = new GameObject("Camera Compass");
// "Correct" the compass so that it lies flat in the game plane
cameraCompass.transform.eulerAngles = new Vector3(0, GameObject.FindGameObjectWithTag("MainCamera").transform.eulerAngles.y, 0);
cameraForward = cameraCompass.transform.forward;
cameraBack = -cameraCompass.transform.forward;
cameraRight = cameraCompass.transform.right;
cameraLeft = -cameraCompass.transform.right;
Destroy(cameraCompass); // We don't need the Compass anymore!
}
Wow, I wish these boards would format code better! Anyway, I call that method in the Start method of the script on the object my player is controlling. It's creating that "camera compass" that lies flat on the 2D plane that movement happens on, and is rotated to look in the direction that the MainCamera is looking. I then use those cameraForward, Back, Right and Left vectors to move the player. Hope this is helpful!
Unity says camera'direction' does not exist in the current context?!?!
Your answer
Follow this Question
Related Questions
How to prevent player from moving in air while jumping in place?? 0 Answers
Non slippery movement 1 Answer
How to make the player move where camera is facing? 1 Answer
Problem with camera and character controller (fighting game) 1 Answer
Third-Person Camera keeps intersecting with terrain/buildings 2 Answers