Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Player Movement & Camera Assistance!

Discussion in 'Scripting' started by romariilol, Sep 20, 2019.

  1. romariilol

    romariilol

    Joined:
    Feb 9, 2019
    Posts:
    3
    I'm trying to copy the movement from the hand in Black & White 2 and have been struggling the last 2 days. I've done a lot of research over 2 days worth but am still having the problem. I basically wanna copy the movement in the video below but have no clue how to reference it. I've been using the code below but basically it move's on the Y axis whenever I want it to only move on X & Z. The main issue is it goes up on the y axis instead of forward

    TLDR
    What I got now https://imgur.com/a/JNIrZ3x
    What I'm trying to do https://imgur.com/a/7X7ACdZ

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class MagicGodHand : MonoBehaviour
    6. {
    7.     public float horizontalSpeed = 2.0F;
    8.     public float verticalSpeed = 2.0F;
    9.  
    10.     private void Start()
    11.     {
    12.         Cursor.visible = false;
    13.     }
    14.  
    15.  
    16.     void Update()
    17.     {
    18.  
    19.      
    20.             if (Input.GetKey(KeyCode.Mouse2))
    21.             {
    22.                 float h = horizontalSpeed * Input.GetAxis("Mouse X");
    23.                 float v = verticalSpeed * Input.GetAxis("Mouse Y");
    24.             }
    25.      }
    26.  
    27.  
    28.  
    29.  
    30.        {
    31.             Vector3 temp = Input.mousePosition;
    32.             temp.z = 30f; // Set this to be the distance you want the object to be placed in front of the camera.
    33.             this.transform.position = Camera.main.ScreenToWorldPoint(temp);
    34.         }
    35.     }
    36.    

    Bonus Points : My camera also moves on the Y using the code below but I would prefer it to move like in the video and go forward on the Z instead of moving on a Y. I'll pay if someone can solve this for me. I'll put my camera code below. I've tried a million different things with the camera but am still unable to resolve it as well. I've tried all the .transforms but am unsucessful . The only thing wrong with my camera is it zooms in on w instead of moving in the map like on the imgur link. Here is my code for the camera

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. [RequireComponent(typeof(Camera))]
    5. public class DrakeCamera : MonoBehaviour
    6. {
    7.     #region UI
    8.  
    9.     [Space]
    10.  
    11.     [SerializeField]
    12.     private bool _active = true;
    13.  
    14.     [Space]
    15.  
    16.     [SerializeField]
    17.     private bool _enableRotation = true;
    18.  
    19.     [SerializeField]
    20.     private float _mouseSense = 1.8f;
    21.  
    22.     [Space]
    23.  
    24.     [SerializeField]
    25.     private bool _enableTranslation = true;
    26.  
    27.     [SerializeField]
    28.     private float _translationSpeed = 55f;
    29.  
    30.     [Space]
    31.  
    32.     [SerializeField]
    33.     private bool _enableMovement = true;
    34.  
    35.     [SerializeField]
    36.     private float _movementSpeed = 10f;
    37.  
    38.     [SerializeField]
    39.     private float _boostedSpeed = 50f;
    40.  
    41.     [Space]
    42.  
    43.     [SerializeField]
    44.     private bool _enableSpeedAcceleration = true;
    45.  
    46.     [SerializeField]
    47.     private float _speedAccelerationFactor = 1.5f;
    48.  
    49.     [Space]
    50.  
    51.     [SerializeField]
    52.     private KeyCode _initPositonButton = KeyCode.R;
    53.  
    54.     #endregion UI
    55.  
    56.     private CursorLockMode _wantedMode;
    57.  
    58.     private float _currentIncrease = 1;
    59.     private float _currentIncreaseMem = 0;
    60.  
    61.     private Vector3 _initPosition;
    62.     private Vector3 _initRotation;
    63.  
    64. #if UNITY_EDITOR
    65.     private void OnValidate()
    66.     {
    67.         if (_boostedSpeed < _movementSpeed)
    68.             _boostedSpeed = _movementSpeed;
    69.     }
    70. #endif
    71.  
    72.  
    73.     private void Start()
    74.     {
    75.         _initPosition = transform.position;
    76.         _initRotation = transform.eulerAngles;
    77.     }
    78.  
    79.     private void CalculateCurrentIncrease(bool moving)
    80.     {
    81.         _currentIncrease = Time.deltaTime;
    82.  
    83.         if (!_enableSpeedAcceleration || _enableSpeedAcceleration && !moving)
    84.         {
    85.             _currentIncreaseMem = 0;
    86.             return;
    87.         }
    88.  
    89.         _currentIncreaseMem += Time.deltaTime * (_speedAccelerationFactor - 1);
    90.         _currentIncrease = Time.deltaTime + Mathf.Pow(_currentIncreaseMem, 3) * Time.deltaTime;
    91.     }
    92.  
    93.     private void Update()
    94.     {
    95.         if (!_active)
    96.             return;
    97.  
    98.         // Translation
    99.         if (_enableTranslation)
    100.         {
    101.             transform.Translate(Vector3.forward * Input.mouseScrollDelta.y * Time.deltaTime * _translationSpeed);
    102.         }
    103.  
    104.         // Movement
    105.         if (_enableMovement)
    106.         {
    107.             Vector3 deltaPosition = Vector3.zero;
    108.             float currentSpeed = _movementSpeed;
    109.  
    110.             if (Input.GetKey(KeyCode.LeftShift))
    111.                 currentSpeed = _boostedSpeed;
    112.  
    113.             if (Input.GetKey(KeyCode.W))
    114.                 deltaPosition += transform.forward;
    115.  
    116.             if (Input.GetKey(KeyCode.S))
    117.                 deltaPosition -= transform.forward;
    118.  
    119.             if (Input.GetKey(KeyCode.A))
    120.                 deltaPosition -= transform.right;
    121.  
    122.             if (Input.GetKey(KeyCode.D))
    123.                 deltaPosition += transform.right;
    124.  
    125.             // Calc acceleration
    126.             CalculateCurrentIncrease(deltaPosition != Vector3.zero);
    127.  
    128.             transform.position += deltaPosition * currentSpeed * _currentIncrease;
    129.         }
    130.  
    131.         // Rotation
    132.         if (Input.GetKey(KeyCode.Mouse2))
    133.         {
    134.             // Pitch
    135.             transform.rotation *= Quaternion.AngleAxis(
    136.                 -Input.GetAxis("Mouse Y") * _mouseSense,
    137.                 Vector3.right
    138.             );
    139.  
    140.             // Paw
    141.             transform.rotation = Quaternion.Euler(
    142.                 transform.eulerAngles.x,
    143.                 transform.eulerAngles.y + Input.GetAxis("Mouse X") * _mouseSense,
    144.                 transform.eulerAngles.z
    145.             );
    146.         }
    147.  
    148.         // Return to init position
    149.         if (Input.GetKeyDown(_initPositonButton))
    150.         {
    151.             transform.position = _initPosition;
    152.             transform.eulerAngles = _initRotation;
    153.         }
    154.     }
    155. }
    156.  
    157.  

    i
     
    Yoreki likes this.
  2. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    Maybe this will help:
    https://docs.unity3d.com/Manual/CameraRays.html

    Shoot a raycast through your mouse cursor, if it hits anything, place your hand on that position minus some offset. A little bit of math there but it should work.

    Also, your camera is zooming in instead of going forward because you are moving it on the local forward axis (which is towards the floor if your camera is looking at the floor). This might help:
    https://stackoverflow.com/questions/41464037/move-toward-transform-forward-ignoring-rotation
     
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    While Dextozz already described how it's done, i feel like this video may help you implementing it:


    (Have a look at the result at ~14:30, which i believe is exactly what you want)
    You basically want to do that whenever your mouse is not over the GUI (in case you plan a GUI like in the example).

    Off-topic:
    I also wanted to stop by to say how great i found your "TLDR" example videos. No seriously, that made it very fast and obvious to understand what you want. You also offered a decent description and posted your code. I wish everybody did it like that. Helping people is already fun, but there are always those who write a single line of text to ask on help for some complex topic or something :D
     
    Last edited: Sep 21, 2019
    Dextozz likes this.