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 be7a · May 02, 2012 at 04:36 PM · charactercontrollerjumpjoystick

Joystick - movement and jump in the same direction

Hi, I have looked in to Penelope's character controller scripts and played around with the code to come up with the code below. At the moment the left joystick moves the player and faces the direction you leave the character, even on release of the joystick (which is what i want). However, I am struggling to make the player jump in the direction the player is facing. The right joystick plays the jump animation how I want it, but it only jumps forward. Any suggestions? I really cant work it out!


var rotationPlayer : Quaternion;

function Start() {

 thisTransform = GetComponent( Transform );
 character = GetComponent( CharacterController );    
 animationController = GetComponent( AnimationController );
 
 animationController.maxForwardSpeed = speed;
 
 var spawn = GameObject.Find( "PlayerSpawn" );
 if ( spawn )
     thisTransform.position = spawn.transform.position;    

}

function FaceMovementDirection() {
horizontalVelocity = character.velocity; horizontalVelocity.y = 0; // if ( horizontalVelocity.magnitude > 0.1) //botTransform.forward = horizontalVelocity.normalized * 180; }

function OnEndGame() { // Disable joystick when the game ends
moveJoystick.Disable(); rotateJoystick.Disable();

 // Don't allow any more control changes when the game ends
 this.enabled = false;

}

function Update() { rotationPlayer = transform.rotation; var movement = cameraTransform.TransformDirection( Vector3( moveJoystick.position.x, 0, moveJoystick.position.y ) ); // We only want the camera-space horizontal direction

 movement.y = 0;
 movement.Normalize(); // Adjust magnitude after ignoring vertical movement
 inAirMultiplier = velocity.z;
 // Let's use the largest component of the joystick position for the speed.
 var absJoyPos = Vector2( Mathf.Abs( moveJoystick.position.x ), Mathf.Abs( moveJoystick.position.y ) );
 movement *= speed * ( ( absJoyPos.x > absJoyPos.y ) ? absJoyPos.x : absJoyPos.y );
 

 // Check for jump
 if ( character.isGrounded )
 {
     if ( rotateJoystick.tapCount == 2 )
     {
         velocity.y += 16;//height of jump
         Debug.Log(velocity.z+" velocity");
     
     }
 }else{        
     
     
     velocity.y -= 1;
 
     
     
     if(velocity.z < 1){
     movement.z += rotationPlayer.z + 20;
     movement.x += rotationPlayer.x;
     
             
     }
     else{
     movement.z += velocity.z + 0.25;
     }
     
     
 }

 

 
 Debug.Log(horizontalVelocity);

 movement += velocity;
 movement += Physics.gravity;
 movement *= Time.deltaTime;
 
 character.Move( movement );
 
 if ( character.isGrounded )
     velocity = Vector3.zero;
 
 FaceMovementDirection();    
 
 var camRotation = rotateJoystick.position;
 camRotation.x *= rotationSpeed.x;
 camRotation.y *= rotationSpeed.y;
 camRotation *= 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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Integrating Dual TouchPads into FPSInputController for CharacterMotor 0 Answers

Character Controller Initial Problem With Mobile 0 Answers

controller.Move not working correctly 0 Answers

How to make the CC(character controller) jump and push 1 Answer

Character Jumping Not Working Sometimes 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