Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by badguy0325 · Jul 06, 2013 at 04:49 AM · directionmomentumtransformdirectionjetpack

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);
                    }
                }
            }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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'.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image badguy0325 · Jul 06, 2013 at 10:01 PM 0
Share

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.

avatar image badguy0325 · Jul 06, 2013 at 10:01 PM 0
Share

Oh I know nothing about JS by the way. I strictly script in C#.

avatar image robertbu · Jul 07, 2013 at 03:36 AM 0
Share

$$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);
     }
 }


avatar image robertbu · Jul 07, 2013 at 03:43 AM 0
Share

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'.

avatar image badguy0325 · Jul 07, 2013 at 01:22 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast between TransformDirection and Vector3.Forward? 1 Answer

How to check in which direction an object is facing? 2 Answers

How to get a vector as if it were a local position to an object? 1 Answer

Trigger enter issue 1 Answer

How can I change the "forward" direction of my model? 5 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges