Hi there! I use this free camera asset: MS Advanced Camera Controller | Camera | Unity Asset Store . It’s really useful for me, but I can’t find out how to relative player movement to it. I’ve been searching a while, but nothing works. Here’s my script:
public class PlayerController : MonoBehaviour {
public float moveSpeed;
public float jumpForce;
public CharacterController controller;
private Vector3 moveDirection;
public float gravityScale;
// Use this for initialization
void Start () {
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
moveDirection = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, moveDirection.y, Input.GetAxis("Vertical") * moveSpeed);
if (controller.isGrounded)
{
moveDirection.y = 0f;
if (Input.GetButtonDown("Jump"))
{
moveDirection.y = jumpForce;
}
}
moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale * Time.deltaTime);
controller.Move(moveDirection * Time.deltaTime);
}
}
Thanks for any help.
Hello.
What camera style are you using?
Just before you do the Move, trymoveDirection = transform.TransformDirection(moveDirection);
Orbital that follows (3rd person)
Can you please tell me on which line should I past it? Thanks
With this code, you simply associate the camera with it and the movement should already work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class PlayerController : MonoBehaviour {
public float _moveSpeed = 6.0f;
public float jumpForce = 8.0f;
public float gravityForce = 20.0f;
public GameObject myCamera;
Vector3 _moveDirection = Vector3.zero;
CharacterController myController;
void Start () {
transform.tag = "Player";
myController = GetComponent<CharacterController> ();
}
void Update () {
Vector3 forwordDirection = new Vector3 (myCamera.transform.forward.x, 0, myCamera.transform.forward.z);
Vector3 sideDirection = new Vector3 (myCamera.transform.right.x, 0, myCamera.transform.right.z);
forwordDirection.Normalize ();
sideDirection.Normalize ();
forwordDirection = forwordDirection * Input.GetAxis ("Vertical");
sideDirection = sideDirection * Input.GetAxis ("Horizontal");
Vector3 direcFinal = forwordDirection + sideDirection;
if (direcFinal.sqrMagnitude > 1) {
direcFinal.Normalize ();
}
if (myController.isGrounded) {
_moveDirection = new Vector3 (direcFinal.x, 0, direcFinal.z);
_moveDirection *= _moveSpeed;
if (Input.GetButton ("Jump")) {
_moveDirection.y = jumpForce;
}
}
_moveDirection.y -= gravityForce * Time.deltaTime;
myController.Move (_moveDirection * Time.deltaTime);
}
}
You may need to enter another command to rotate the main player object.
Marcos-Schultz:
With this code, you simply associate the camera with it and the movement should already work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class PlayerController : MonoBehaviour {
public float _moveSpeed = 6.0f;
public float jumpForce = 8.0f;
public float gravityForce = 20.0f;
public GameObject myCamera;
Vector3 _moveDirection = Vector3.zero;
CharacterController myController;
void Start () {
transform.tag = "Player";
myController = GetComponent<CharacterController> ();
}
void Update () {
Vector3 forwordDirection = new Vector3 (myCamera.transform.forward.x, 0, myCamera.transform.forward.z);
Vector3 sideDirection = new Vector3 (myCamera.transform.right.x, 0, myCamera.transform.right.z);
forwordDirection.Normalize ();
sideDirection.Normalize ();
forwordDirection = forwordDirection * Input.GetAxis ("Vertical");
sideDirection = sideDirection * Input.GetAxis ("Horizontal");
Vector3 direcFinal = forwordDirection + sideDirection;
if (direcFinal.sqrMagnitude > 1) {
direcFinal.Normalize ();
}
if (myController.isGrounded) {
_moveDirection = new Vector3 (direcFinal.x, 0, direcFinal.z);
_moveDirection *= _moveSpeed;
if (Input.GetButton ("Jump")) {
_moveDirection.y = jumpForce;
}
}
_moveDirection.y -= gravityForce * Time.deltaTime;
myController.Move (_moveDirection * Time.deltaTime);
}
}
You may need to enter another command to rotate the main player object.
Thank you so much for help!