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 mematfmm · Jul 08, 2019 at 03:22 PM · movementmovement script

achieveing half-life like surfing

hello fellas. i'm trying to achieve surfing in my game. i already have a movement script that emulates half-life like bunnyhopping but the only problem with it is that, when i'm on a slope that is more than 45 degrees steep i don't slide off, i just awkwardly sit there. i don't know where to start with this, so pretty much anything is welcome.

my current movement script (uses a character controller, not a rigidbody):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 /// <summary>
 /// class used for handling localplayer movement.
 /// TODO: source-engine like command system (check c_usercmd)
 /// </summary>
 
 public class local_controller : MonoBehaviour {
     Transform m_camera_transform;
     CharacterController m_controller;
 
     public float m_f_mouse_sensitivity_x = 5f;
     public float m_f_mouse_sensitivity_y = 5f;
     public float m_f_jump_speed = 8f;
     public float m_f_gravity = 20f;
     public float m_f_camera_y_offset = 0.8f;
     public float m_f_friction = 8f;
     public float m_f_ground_acceleration = 100f;
     public float m_f_ground_max_velocity = 8f;
     public float m_f_air_acceleration = 110f;
     public float m_f_air_max_velocity = 30f;
     public float m_f_air_control = 2f;
 
     float m_yaw = 0.022f;
     float m_pitch = 0.022f;
     float m_rotation_x;
     float m_rotation_y;
 
     bool m_is_jumping;
 
     Vector3 m_camera_offset_vector;
     Vector3 m_move_direction = Vector3.zero;
     Vector3 m_move_direction_temp = Vector3.zero;
 
     void Start() {
         m_camera_transform = gameObject.GetComponentInChildren<Camera>().transform;
         m_camera_offset_vector = new Vector3(0, m_f_camera_y_offset, 0);
         m_camera_transform.position = transform.position + m_camera_offset_vector;
         m_controller = gameObject.GetComponent<CharacterController>();
     }
 
     void FixedUpdate() {
         // don't update player movement every single frame, might cause problems on higher framerates
         m_controller.Move(m_move_direction * Time.fixedDeltaTime);
     }
 
     void Update() {
         if (Cursor.lockState != CursorLockMode.Locked && Input.GetButtonDown("Fire1")) {
             Cursor.lockState = CursorLockMode.Locked;
         }
 
         // get mouse input
         m_rotation_y += Input.GetAxisRaw("Mouse X") * m_f_mouse_sensitivity_x * m_yaw;
         m_rotation_x -= Input.GetAxisRaw("Mouse Y") * m_f_mouse_sensitivity_y * m_pitch;
 
         // clamp eyeangles
         if (m_rotation_x < -90)
             m_rotation_x = -90;
         else if (m_rotation_x > 90)
             m_rotation_x = 90;
         
         if (m_rotation_y < -360 || m_rotation_y > 360)
             m_rotation_y = m_rotation_y % 360;
 
         this.transform.rotation = Quaternion.Euler(0, m_rotation_y, 0);
         m_camera_transform.rotation = Quaternion.Euler(m_rotation_x, m_rotation_y, 0);
 
         // more input stuff
         float input_x = Input.GetAxisRaw("Horizontal");
         float input_y = Input.GetAxisRaw("Vertical");
 
         m_move_direction = new Vector3(input_x, 0, input_y).normalized;
         m_move_direction = transform.TransformDirection(m_move_direction);
 
         // handle player related physics / movement
         if (!m_controller.isGrounded || m_is_jumping)
             m_move_direction = move_air(m_move_direction, m_controller.velocity);
         else if (m_controller.isGrounded && !m_is_jumping)
             m_move_direction = move_ground(m_move_direction, m_controller.velocity);
 
         // autojump / bunnyhopping
         if (m_controller.isGrounded)
             m_is_jumping = false;
 
         if (Input.GetButton("Jump") && m_controller.isGrounded) {
             m_is_jumping = true;
             m_move_direction.y = m_f_jump_speed;
         }
 
         m_move_direction.y -= m_f_gravity * Time.deltaTime;
     }
 
     private Vector3 accelerate(Vector3 accelerate_direction, Vector3 previous_velocity, float accelerate, float max_velocity) {
         float projVel = Vector3.Dot(previous_velocity, accelerate_direction);
         float accelVel = accelerate * Time.fixedDeltaTime;
 
         if (projVel + accelVel > max_velocity)
             accelVel = max_velocity - projVel;
 
         return previous_velocity + accelerate_direction * accelVel;
     }
 
     private Vector3 air_accelerate(Vector3 wished_direction, Vector3 previous_velocity, float accelerate, float max_velocity) {
         if (wished_direction.magnitude == 0)
             return previous_velocity;
 
         float proj_velocity = Vector3.Dot(previous_velocity, wished_direction);
         float accel_velocity = accelerate * Time.fixedDeltaTime;
 
         if (proj_velocity < 0) {
             previous_velocity += wished_direction * accel_velocity;
 
             float y_speed = previous_velocity.y;
             previous_velocity.y = 0;
 
             float speed = previous_velocity.magnitude;
             previous_velocity.Normalize();
 
             proj_velocity = Vector3.Dot(previous_velocity, wished_direction);
 
             float k = 32;
 
             k *= m_f_air_control * proj_velocity * proj_velocity * Time.deltaTime;
             
             if (speed > max_velocity)
                 speed *= max_velocity / speed;
 
             previous_velocity.x = previous_velocity.x * speed + wished_direction.x * k;
             previous_velocity.y = previous_velocity.y * speed + wished_direction.y * k;
             previous_velocity.z = previous_velocity.z * speed + wished_direction.z * k;
 
             previous_velocity.Normalize();
 
             previous_velocity.x *= speed;
             previous_velocity.y = y_speed;
             previous_velocity.z *= speed;
 
         } else if (accel_velocity >= 0)
             if (Mathf.Sqrt(Mathf.Pow(previous_velocity.x, 2) + Mathf.Pow(previous_velocity.z, 2)) < 2f)
                 previous_velocity += wished_direction * 2 * Time.fixedDeltaTime;
 
         return previous_velocity;
     }
 
     private Vector3 move_ground(Vector3 accel_direction, Vector3 previous_velocity) {
         float speed = previous_velocity.magnitude;
 
         if (speed != 0 && m_controller.isGrounded) {
             float drop = speed * m_f_friction * Time.fixedDeltaTime;
             previous_velocity *= Mathf.Max(speed - drop, 0) / speed;
         }
 
         return accelerate(accel_direction, previous_velocity, m_f_ground_acceleration, m_f_ground_max_velocity);
     }
 
     private Vector3 move_air(Vector3 accel_direction, Vector3 previous_velocity) {
         return air_accelerate(accel_direction, previous_velocity, m_f_air_acceleration, m_f_air_max_velocity);
     }
 
     private void OnGUI() {
         GUI.Label(new Rect(new Vector2(5, 5), new Vector2(Screen.width - 5, 100)), "m_speed: " + m_controller.velocity.magnitude * 36f + "ups"); // for goldsrc / source-like unit measurements
         GUI.Label(new Rect(new Vector2(5, 15), new Vector2(Screen.width - 5, 100)), "m_is_jumping: " + m_is_jumping);
     }
 }

regards

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

235 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

Related Questions

Switching lanes 1 Answer

How to make an object move, not teleport? 0 Answers

Click to move player flickering when trying to get past object,Click to Move player, flickering when colliding with other objects. 0 Answers

Y-axis movement stuck. Help me please :c,Not Moving by pressing Button :C. Help me please 0 Answers

Limit movement to just forward/backward and left/right 0 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