Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 fonetic · Aug 30, 2016 at 03:00 PM · playercharactercontrollerjumping

Need help with CharacterController script

So basicaly I have a script on my player that when I am jumping the player is moving in the same direction he was before he jumped, it is working good, but I want to add additional feature when player is in the air and he presses either left or right he stops moving in the dirrection he was before jumping. I hope you understand what I mean.

this is the part of script wich is moving the player in direction he was before jumping :
moveVector.x = lastMotion.x;

and if i change it to :
moveVector.x = inputDirection;

then i can move while i'm jumping but I don't want that I want the feature that player is going the direction he was last in and it's locked, but he stops when he is pressing input direction.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Ardaaytac · Aug 31, 2016 at 05:40 AM

Attach this script to your ground object . Not your player character,

   public bool IsGrounded;
         ex. Class Name = GroundHit
     In this script create:
     public static GroundHit instance;
     void Awake()
     {
        instance = this;
     }
      void OnCollisionStay (Collision collisionInfo)
      {
           IsGrounded = true;
      }
      
      void OnCollisionExit (Collision collisionInfo)
      {
           IsGrounded = false;
      }

I didnt understand you clearly but if you want to add something when your player is on air use a boolean and set it true when it is not colliding with ground and set false when it exit the ground. and use this bool in your update function with GetKey. Hope understand you right.

 Then go to your PlayerController script and use that on update function.
 example:
 void Update()
 {
   if(!GroundHit.instance.IsGrounded  && Input.GetKey(KeyCode.P)
    //Make something.
 }




Comment
Add comment · 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
0

Answer by trevorchico · Aug 31, 2016 at 07:54 AM

@fonetic Not sure how your characterController script is set up but couldn't you just stop the movement of the player in mid air?

IF player is in air AND left or right is pressed, freeze position.

you could temporarily put constraints on the rigid body or just freeze the x&z coordinates

if(input.getkeyDown(left or right)) && transform.position.y>1) GetComponent().constraints = RigidbodyConstraints.FreezePositionX; GetComponent().constraints = RigidbodyConstraints.FreezePositionZ;

or create a temporary Vector3 that is set when you hold down the left or right key, then set the X&Y transform.positions to those values.

Comment
Add comment · 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
0

Answer by CapitalLlama · Aug 30, 2016 at 06:22 PM

If I understand you correctly, you want it so that if the play is running to the right, jumps, and then presses left, the player will no longer be moving to the right and fall straight down?

If that is the case then you can just do this

 if(inputDirection != moveVector.x)
      moveVector.x = 0;

That will basically just say if the player ever presses a direction he isn't going, then stop moving on the x axis.

This probably won't be 100% correct since I don't see the rest of your code, but it should point you in the right direction.

Comment
Add comment · 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
0

Answer by Dream_in_code · Aug 31, 2016 at 06:35 AM

So you want to flip the player? if he jumps right and you press left you want it to jump to left? it thats the case use this.

 float masSpeed;//moving speed

    float move = Input.GetAxis("Horizontal");

     myAnim.SetFloat("speed", Mathf.Abs(move)); //playing animation

     myRB.velocity = new Vector2(move * maxSpeed, myRB.velocity.y);


     if (move > 0 && !facingRight)

     {
         flip();
     }
     else if (move < 0 && facingRight)

     {
         flip();
     }

 }
 void flip()
 {
     facingRight = !facingRight;
     Vector2 theScale = transform.localScale;
     theScale.x *= -1;
     transform.localScale = theScale;

 }
Comment
Add comment · 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

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

7 People are following this question.

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

Related Questions

Player can't move. 1 Answer

can anyone help me to smooth the jump? 0 Answers

How to move while jumping? 1 Answer

how do I get rid of the word transform in my script and destroy a player (Character Controller) without an issue? 1 Answer

Make a collider NOT collide with CharacterController 1 Answer


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