Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Gravity Direction via set velocity movement

Discussion in 'Physics' started by Chaos-Fusion, Jun 16, 2020.

  1. Chaos-Fusion

    Chaos-Fusion

    Joined:
    Mar 19, 2013
    Posts:
    9
    Currently, I want to try and implement the ability to wall on different angled surfaces in my game however I suspect that the way my movement code is written will prevent me from doing so without major rewrites. Getting the gravity direction based on the terrain was easy enough however my movement vectors doesn't like the change in gravity direction.

    The basic movement script was based off a tutorial I followed from Swindle Creative which modifies the Velocity variable directly which worked well enough for basic gravity. Using this as the template as a base however has caused some headaches with applying forces.

    Here is a stripped version of my script with only the basic movement included.
    Code (CSharp):
    1. private void MovementCalc()
    2. {
    3.      xRaw = Input.GetAxisRaw("Horizontal");
    4.      zRaw = Input.GetAxisRaw("Vertical");
    5.      x = Input.GetAxis("Horizontal");
    6.      z = Input.GetAxis("Vertical");
    7.      //Use Raw inputs to get a 0, -1 or 1 value.
    8.      if (xRaw != 0 || zRaw != 0)
    9.      {
    10.          //Get Camera direction
    11.          directionIntentX = Camera.right;
    12.          directionIntentX.y = 0;
    13.          directionIntentX.Normalize();
    14.          directionIntentY = Camera.forward;
    15.          directionIntentY.y = 0;
    16.          directionIntentY.Normalize();
    17.          //Preserve the original direction so character direction is saved when decellerating
    18.          xSaved = x;
    19.          zSaved = z;
    20.      }
    21.      //Seperate Y velocity so I can manipulate it later
    22.      MovementVector = (directionIntentY * zSaved * TotalSpeed) + (directionIntentX * xSaved * TotalSpeed);
    23.      
    24.      //Original Y Velocity is preserved and added to the movement variable.
    25.      //Up Axis is meant to be the act as the gravity direction. Was formely Vector3.up
    26.      CurrentYVel = (UpAxis * rb.velocity.y);
    27.      
    28.      //If grounded and player is on a slope, allow player to walk up and down slopes smoothly
    29.      if (GroundedActions() && (OnSlope()))
    30.      {
    31.          //Apply Movement Vectors.
    32.          rb.velocity = Vector3.ProjectOnPlane((MovementVector + (CurrentYVel)), groundNormal);
    33.          _slopeDirection = rb.velocity.normalized;
    34.          //There was code here that prevents character from launching off slopes and falling off slopes
    35.      }
    36.      else
    37.      {
    38.          //If not on slope, just set velocity directly
    39.          rb.velocity = ((MovementVector + (CurrentYVel)));
    40.      }
    41. }
    42.  
    With my current Movement script, what can I do to convert my current velocity to local velocity so I can move on walls and the ceiling? I've tried using Transform.TransformDirection + Inverse however applying it directly to the velocity messes with the players ability to walk forwards(W and S switch direction based on where on the map I am.). Also tried looking at the Gravity FPS Walker script however I couldn't implement how it handles the change in gravity direction and the scipt prevents the player from walking up slopes last time I checked it.

    Additionally, I've also made the mistake of adding some checks to the rb.velocity.y to perform some functionality however with the change in direction, velocity.y may no longer represent the up.
    Code (CSharp):
    1. Vector3 localVel = transform.InverseTransformDirection(rb.velocity);
    2. if (localVel.y < 0)
    3. {
    4.     rb.velocity += UpAxis * currentGravity.y * (FallMulitplier - 1)*Time.fixedDeltaTime;
    5. }
    I've tried this which gives the same results however I haven't really been able to confirm it works since I haven't gotten the different gravity direction working.

    Add comment
     
    Last edited: Jul 5, 2020
  2. Chaos-Fusion

    Chaos-Fusion

    Joined:
    Mar 19, 2013
    Posts:
    9
    Bumping since it's been more than a week
     
  3. Chaos-Fusion

    Chaos-Fusion

    Joined:
    Mar 19, 2013
    Posts:
    9
    Second Bump. Rewrote the thread to clarify more with better word choices to explain what I want to achieve. Included what I tried as well.

    Should I have posted this in the Scripting Forum?
     
    Last edited: Jul 5, 2020