- Home /
How do I include the transform direction when changing move direction?
Hello. I am currently using the FPSWalkerEnhanced Script found here. It has air control, but it stops and moves on a dime. I wanted to add momentum to it, e.g it takes a second to slow down and move the other direction, because I will be using a jetpack in my game. I modified the air control section to the code below.It works great when I am facing straight forward. My problem is that if i change the rotation of my character, it goes crazy, spinning is circles and jerking around. How do I include the rotation in this, so that it will work which ever way I am looking? Any help would be greatly appreciated. The code did not turn out right on here. It looks better on Unity forum.
if(airControl){ if (Input.GetAxis("Horizontal") > 0){ if(moveDirection.x < 25){ moveDirection.x +=(inputX * inputModifyFactor * airAcc * Time.deltaTime ); moveDirection = myTransform.TransformDirection(moveDirection); } } if (Input.GetAxis("Horizontal") < 0){ if(moveDirection.x > -25){ moveDirection.x +=(inputX * inputModifyFactor * airAcc * Time.deltaTime ); moveDirection = myTransform.TransformDirection(moveDirection); } } else if (Input.GetAxis("Horizontal") == 0){ if(moveDirection.x > 0){ moveDirection.x -= (airDecc *Time.deltaTime); } if(moveDirection.x < 0){ moveDirection.x += (airDecc *Time.deltaTime); } } if (Input.GetAxis("Vertical") > 0){ if(moveDirection.z < 25){ moveDirection.z +=(inputY * inputModifyFactor * airAcc * Time.deltaTime ); moveDirection = myTransform.TransformDirection(moveDirection); } } if (Input.GetAxis("Vertical") < 0){ if(moveDirection.z > -25){ moveDirection.z +=(inputY * inputModifyFactor * airAcc * Time.deltaTime ); moveDirection = myTransform.TransformDirection(moveDirection); } } else if (Input.GetAxis("Vertical") == 0){ if(moveDirection.z > 0){ moveDirection.z -= (airDecc *Time.deltaTime); } if(moveDirection.z < 0){ moveDirection.z += (airDecc *Time.deltaTime); } } }
Answer by robertbu · Jul 06, 2013 at 04:12 PM
You haven't included enough code for me to be sure of what you are doing in your code. But to solve your problem you will need to introduce the concept of velocity and drag to your code. Your moveDirection, once converted to world coordinates will need to be added to the velocity each frame. And each frame some drag needs to be applied so that the old velocity decays. Here is a example. Attach a character controller and this script to a game object:
#pragma strict
var maxVelocity = 3.0;
var drag = 1.0;
var speed = .2;
private var velocity = Vector3.zero;
private var cc : CharacterController;
function Start () {
cc = GetComponent(CharacterController);
}
function Update () {
var moveDirection : Vector3;
moveDirection.x = Input.GetAxis("Horizontal") * speed;
moveDirection.z = -Input.GetAxis("Vertical") * speed;
moveDirection = transform.TransformDirection(moveDirection);
// Apply drag
velocity = Vector3.MoveTowards(velocity, Vector3.zero, drag * Time.deltaTime);
velocity += moveDirection;
if (velocity.magnitude > maxVelocity)
velocity = velocity.normalized * maxVelocity;
cc.Move(velocity * Time.deltaTime);
}
You will want to handle gravity/'Y' separately since you don't want drag being applied to 'Y'.
The whole script I use is posted in the top of the thread under the hyperlink "here." The whole code is the same, except the air control section. I already have the jetpack working, and also the gravity, the only problem is when I rotate the player, the code seems to still be using global direction ins$$anonymous$$d of local. Thank you very much for the reply! The question has been on the Unity forums for over a month with no reply, but you replied the day after I put it on Unity Answers. $$anonymous$$uch appreciated.
Oh I know nothing about JS by the way. I strictly script in C#.
$$anonymous$$nowing how to translate between the two languages is a beneficial skill and not hard to acquire. $$anonymous$$ost scripts can be translated knowing only half a dozen syntax differences. Here is a link:
http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html
And here is the translation:
using UnityEngine;
using System.Collections;
public class JetPack : $$anonymous$$onoBehaviour {
public float maxVelocity = 3.0f;
public float drag = 1.0f;
public float speed = .2f;
private Vector3 velocity = Vector3.zero;
private CharacterController cc;
void Start () {
cc = GetComponent<CharacterController>();
}
void Update () {
Vector3 moveDirection = Vector3.zero;
moveDirection.x = Input.GetAxis("Horizontal") * speed;
moveDirection.z = -Input.GetAxis("Vertical") * speed;
moveDirection = transform.TransformDirection(moveDirection);
velocity = Vector3.$$anonymous$$oveTowards(velocity, Vector3.zero, drag * Time.deltaTime);
velocity += moveDirection;
if (velocity.magnitude > maxVelocity)
velocity = velocity.normalized * maxVelocity;
cc.$$anonymous$$ove(velocity * Time.deltaTime);
}
}
Taking the concepts from the script I wrote and applying them to the FPSWalkerEnhanced:
At the top of the file add:
public float airDrag = 1.0f;
public float airSpeed = 1.0f;
public float maxAirVelocity = 4.0f;
Then down below add edit:
if (airControl && playerControl) {
Debug.Log (Time.fixedDeltaTime);
Vector3 v3 = new Vector3(inputX * speed * input$$anonymous$$odifyFactor, 0.0f, inputY * speed * input$$anonymous$$odifyFactor);
v3 = myTransform.TransformDirection(v3) * airSpeed * Time.fixedDeltaTime;
moveDirection = Vector3.$$anonymous$$oveTowards(moveDirection, Vector3.zero, airDrag * Time.deltaTime);
moveDirection += v3;
if (moveDirection.magnitude > maxAirVelocity)
moveDirection = moveDirection.normalized * maxAirVelocity;
// Hack to keep flying
if (inputX != 0.0 || inputY != 0.0)
moveDirection.y += gravity * Time.fixedDeltaTime;
}
Start walking and then hit the jump key. It works better with a lower gravity setting.
Note there are bugs in the FPSWalkerEnhanced code. Since it is being executed inside FixedUpdate(), all references to 'Time.deltaTime' should be replaced with 'Time.fixedDeltaTime'.
Thank you very much robertbu! The Jetpack code itself is the exact effect I want, only when it is active, I can't get the player to move on the y axis. If I add the above script to the FPSWalkerEnhanced, I am stuck mid air, even after disabling the Hack to keep flying. I don't know what I am doing wrong.
Your answer
![](https://koobas.hobune.stream/wayback/20220613105558im_/https://answers.unity.com/themes/thub/images/avi.jpg)