Search Unity

Relative to camera movement

Discussion in 'Scripting' started by Hyperka, Mar 17, 2019.

  1. Hyperka

    Hyperka

    Joined:
    Mar 16, 2019
    Posts:
    15
    Hi there! I use this free camera asset: https://assetstore.unity.com/packages/tools/camera/ms-advanced-camera-controller-81759. 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:
    Code (CSharp):
    1. public class PlayerController : MonoBehaviour {
    2.  
    3.     public float moveSpeed;
    4.     public float jumpForce;
    5.     public CharacterController controller;
    6.  
    7.     private Vector3 moveDirection;
    8.     public float gravityScale;
    9.  
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         controller = GetComponent<CharacterController>();      
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.         moveDirection = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, moveDirection.y, Input.GetAxis("Vertical") * moveSpeed);
    19.  
    20.         if (controller.isGrounded)
    21.         {
    22.             moveDirection.y = 0f;
    23.             if (Input.GetButtonDown("Jump"))
    24.             {
    25.                 moveDirection.y = jumpForce;
    26.             }
    27.         }
    28.         moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale * Time.deltaTime);
    29.         controller.Move(moveDirection * Time.deltaTime);
    30.     }
    31. }
    32.  
    Thanks for any help.
     
  2. Marcos-Schultz

    Marcos-Schultz

    Joined:
    Feb 24, 2014
    Posts:
    381
    Hello.

    What camera style are you using?
     
  3. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Just before you do the Move, try
    Code (CSharp):
    1. moveDirection = transform.TransformDirection(moveDirection);
     
  4. Hyperka

    Hyperka

    Joined:
    Mar 16, 2019
    Posts:
    15
    Orbital that follows (3rd person)
     
    Last edited: Mar 22, 2019
  5. Hyperka

    Hyperka

    Joined:
    Mar 16, 2019
    Posts:
    15
    Can you please tell me on which line should I past it? Thanks
     
  6. Marcos-Schultz

    Marcos-Schultz

    Joined:
    Feb 24, 2014
    Posts:
    381
    With this code, you simply associate the camera with it and the movement should already work.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(CharacterController))]
    6. public class PlayerController : MonoBehaviour {
    7.  
    8.     public float _moveSpeed = 6.0f;
    9.     public float jumpForce = 8.0f;
    10.     public float gravityForce = 20.0f;
    11.     public GameObject myCamera;
    12.     Vector3 _moveDirection = Vector3.zero;
    13.     CharacterController myController;
    14.  
    15.     void Start () {
    16.         transform.tag = "Player";
    17.         myController = GetComponent<CharacterController> ();
    18.     }
    19.     void Update () {
    20.         Vector3 forwordDirection = new Vector3 (myCamera.transform.forward.x, 0, myCamera.transform.forward.z);
    21.         Vector3 sideDirection = new Vector3 (myCamera.transform.right.x, 0, myCamera.transform.right.z);
    22.         forwordDirection.Normalize ();
    23.         sideDirection.Normalize ();
    24.         forwordDirection = forwordDirection * Input.GetAxis ("Vertical");
    25.         sideDirection = sideDirection * Input.GetAxis ("Horizontal");
    26.         Vector3 direcFinal = forwordDirection + sideDirection;
    27.  
    28.         if (direcFinal.sqrMagnitude > 1) {
    29.             direcFinal.Normalize ();
    30.         }
    31.            
    32.         if (myController.isGrounded) {
    33.             _moveDirection = new Vector3 (direcFinal.x, 0, direcFinal.z);
    34.             _moveDirection *= _moveSpeed;
    35.             if (Input.GetButton ("Jump")) {
    36.                 _moveDirection.y = jumpForce;
    37.             }
    38.         }
    39.  
    40.         _moveDirection.y -= gravityForce * Time.deltaTime;
    41.         myController.Move (_moveDirection * Time.deltaTime);
    42.     }
    43. }
    You may need to enter another command to rotate the main player object.
     
  7. Hyperka

    Hyperka

    Joined:
    Mar 16, 2019
    Posts:
    15
    Thank you so much for help!