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 /
  • Help Room /
avatar image
0
Question by nebulaisrighthere · Jul 11, 2018 at 06:33 PM · scripting problemmovementscripting beginnercharacter movement

Script problems c#

Im making a game where your character is hops when you press the spacebar (BunnyHopping). And when your character will go faster if you press a (left) and move the mouse left and if you move the d (right) and move the mouse right in the air (Air Strafing).

  • If for more depth and more information about bunnyhopping and air strafing: http://flafla2.github.io/2015/02/14/bunnyhop.html

So heres my bunnyhopping script which i made for the unity engine and it looks good and i put into the firstpersoncamera and nothing has happened and im at a road block, I really need some halpe on this please.

Heres the code:

 public class StrafeMovement : MonoBehaviour
 {
     [SerializeField]
     private float accel = 200f;         // How fast the player accelerates on the ground
     [SerializeField]
     private float airAccel = 200f;      // How fast the player accelerates in the air
     [SerializeField]
     private float maxSpeed = 6.4f;      // Maximum player speed on the ground
     [SerializeField]
     private float maxAirSpeed = 0.6f;   // "Maximum" player speed in the air
     [SerializeField]
     private float friction = 8f;        // How fast the player decelerates on the ground
     [SerializeField]
     private float jumpForce = 5f;       // How high the player jumps
     [SerializeField]
     private LayerMask groundLayers;
 
     [SerializeField]
     private GameObject camObj;
 
     private float lastJumpPress = -1f;
     private float jumpPressDuration = 0.1f;
     private bool onGround = false;
 
     private void Update()
     {
         print(new Vector3(GetComponent<Rigidbody>().velocity.x, 0f, GetComponent<Rigidbody>().velocity.z).magnitude);
         if (Input.GetButton("Jump"))
         {
             lastJumpPress = Time.time;
         }
     }
 
     private void FixedUpdate()
     {
 
         Vector2 input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
 
         // Get player velocity
         Vector3 playerVelocity = GetComponent<Rigidbody>().velocity;
         // Slow down if on ground
         playerVelocity = CalculateFriction(playerVelocity);
         // Add player input
         playerVelocity += CalculateMovement(input, playerVelocity);
         // Assign new velocity to player object
         GetComponent<Rigidbody>().velocity = playerVelocity;
     }
 
     /// <summary>
     /// Slows down the player if on ground
     /// </summary>
     /// <param name="currentVelocity">Velocity of the player</param>
     /// <returns>Modified velocity of the player</returns>
     private Vector3 CalculateFriction(Vector3 currentVelocity)
     {
         onGround = CheckGround();
         float speed = currentVelocity.magnitude;
 
         if (!onGround || Input.GetButton("Jump") || speed == 0f)
             return currentVelocity;
 
         float drop = speed * friction * Time.deltaTime;
         return currentVelocity * (Mathf.Max(speed - drop, 0f) / speed);
     }
 
     /// <summary>
     /// Moves the player according to the input. (THIS IS WHERE THE STRAFING MECHANIC HAPPENS)
     /// </summary>
     /// <param name="input">Horizontal and vertical axis of the user input</param>
     /// <param name="velocity">Current velocity of the player</param>
     /// <returns>Additional velocity of the player</returns>
     private Vector3 CalculateMovement(Vector2 input, Vector3 velocity)
     {
         onGround = CheckGround();
 
         //Different acceleration values for ground and air
         float curAccel = accel;
         if (!onGround)
             curAccel = airAccel;
 
         //Ground speed
         float curMaxSpeed = maxSpeed;
 
         //Air speed
         if (!onGround)
             curMaxSpeed = maxAirSpeed;
 
         //Get rotation input and make it a vector
         Vector3 camRotation = new Vector3(0f, camObj.transform.rotation.eulerAngles.y, 0f);
         Vector3 inputVelocity = Quaternion.Euler(camRotation) *
                                 new Vector3(input.x * curAccel, 0f, input.y * curAccel);
 
         //Ignore vertical component of rotated input
         Vector3 alignedInputVelocity = new Vector3(inputVelocity.x, 0f, inputVelocity.z) * Time.deltaTime;
 
         //Get current velocity
         Vector3 currentVelocity = new Vector3(velocity.x, 0f, velocity.z);
 
         //How close the current speed to max velocity is (1 = not moving, 0 = at/over max speed)
         float max = Mathf.Max(0f, 1 - (currentVelocity.magnitude / curMaxSpeed));
 
         //How perpendicular the input to the current velocity is (0 = 90°)
         float velocityDot = Vector3.Dot(currentVelocity, alignedInputVelocity);
 
         //Scale the input to the max speed
         Vector3 modifiedVelocity = alignedInputVelocity * max;
 
         //The more perpendicular the input is, the more the input velocity will be applied
         Vector3 correctVelocity = Vector3.Lerp(alignedInputVelocity, modifiedVelocity, velocityDot);
 
         //Apply jump
         correctVelocity += GetJumpVelocity(velocity.y);
 
         //Return
         return correctVelocity;
     }
 
     /// <summary>
     /// Calculates the velocity with which the player is accelerated up when jumping
     /// </summary>
     /// <param name="yVelocity">Current "up" velocity of the player (velocity.y)</param>
     /// <returns>Additional jump velocity for the player</returns>
     private Vector3 GetJumpVelocity(float yVelocity)
     {
         Vector3 jumpVelocity = Vector3.zero;
 
         if (Time.time < lastJumpPress + jumpPressDuration && yVelocity < jumpForce && CheckGround())
         {
             lastJumpPress = -1f;
             jumpVelocity = new Vector3(0f, jumpForce - yVelocity, 0f);
         }
 
         return jumpVelocity;
     }
 
     /// <summary>
     /// Checks if the player is touching the ground. This is a quick hack to make it work, don't actually do it like this.
     /// </summary>
     /// <returns>True if the player touches the ground, false if not</returns>
     private bool CheckGround()
     {
         Ray ray = new Ray(transform.position, Vector3.down);
         bool result = Physics.Raycast(ray, GetComponent<Collider>().bounds.extents.y + 0.1f, groundLayers);
         return result;
     }
 }

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

252 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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

1st Person shooter, move player forward where cam is facing 1 Answer

Rigidbody2D movement is lagging 0 Answers

My Character dosen´t move 0 Answers

Why can I only move upwards? 1 Answer

Player not moving in the right direction instantly 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