Search Unity

Top down strafing

Discussion in 'Scripting' started by stormsurgestudio, Aug 6, 2020.

  1. stormsurgestudio

    stormsurgestudio

    Joined:
    May 6, 2018
    Posts:
    4
    Hello guys,

    A pleasant day to you. I need your help. First, forgive me if I can't explain properly. Anyway, I created a top down character that can move forward, back, left, right. In the script, I also added a strafing movement but for now it can only do the proper animation when the character is facing forward. If the character is facing left, right or back the script would treat it as forward. Is there a way that wherever the character is facing, the strafe movement will follow? Anyway, here's the video to see what I'm saying and also attached is the script that I use.



    Here's the script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.  
    8.     private CharacterController controller;
    9.     public float speed = 6f;
    10.     public float turnSmoothTime = 0.1f;
    11.  
    12.     private Animator animator;
    13.  
    14.     private void Start()
    15.     {
    16.         controller = GetComponent<CharacterController>();
    17.         animator = GetComponentInChildren<Animator>();
    18.     }
    19.     private void Update()
    20.     {
    21.         float horizontal = Input.GetAxisRaw("Horizontal");
    22.         float vertical = Input.GetAxisRaw("Vertical");
    23.         Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
    24.         animator.SetInteger("motion", 0);
    25.        
    26.  
    27.         if (direction.magnitude >= 0.1f)
    28.         {
    29.             float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
    30.  
    31.  
    32.  
    33.             controller.Move(direction * speed * Time.deltaTime);
    34.             animator.SetInteger("motion", 1);
    35.  
    36.             if (Input.GetButton("Strafe"))
    37.             {
    38.  
    39.                     animator.SetBool("Strafe", true);
    40.                     Quaternion newDirection = Quaternion.LookRotation(direction);
    41.                     animator.SetFloat("H", horizontal);
    42.                     animator.SetFloat("V", vertical);
    43.  
    44.             }
    45.             else
    46.             {
    47.  
    48.                 animator.SetBool("Strafe", false);
    49.                 Quaternion newDirection = Quaternion.LookRotation(direction);
    50.                 transform.rotation = Quaternion.Slerp(transform.rotation, newDirection, Time.deltaTime * turnSmoothTime);
    51.  
    52.             }
    53.         }
    54. }
    thank you and looking forward to hearing from you and have a nice day all of you
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    The main trick to this has to do with the difference between the shortcuts
    Vector3.right / Vector.forward
    , versus the shortcuts
    transform.right / transform.forward


    The former (with Vector3) is global, always in world space: Vector3.right is (1,0,0), Vector3.forward is (0,0,1), Vector3.up is (0,1,0)

    The latter (with transform) is the local axes for your character. This means transform.forward is wherever your transform +Z is facing (blue arrow).

    I am going to stop short at that explanation because I am not able to understand enough about how you have set up your character. But basically you want to have at least one transform that is your reference "I am facing this way" transform, and then base your strafing motions off that.

    By "base it off that" I mean you can multiply a single input axis (such as horizontal) by transform.right, and then add that to your position. This allows you spin your player around any direction, and right will still be "right" for that player.
     
    Yavuz_Selim likes this.