Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 awplays49 · Aug 08, 2015 at 05:35 PM · playercontroller

3D Custom Character Controller Movement Directions are going Haywire

I have a player controller script, and it's moving the guy in weird directions when moving and I can't seem to nail the pattern of it's behavior. But it does seem to move sideways after turning 90 degrees, then forward again after another 90 degrees.

Player Script:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof (PlayerController))]
 public class Player : MonoBehaviour {
 
     private PlayerController controller;
     public GameObject mainCamera;
     private float mouseX;
     private float mouseY;
     public float xSensitivity;
     public float ySensitivity;
     private Vector3 velocity;
     public float moveSpeed;
     public float jumpForce;
     
     void Start () {
         controller = GetComponent <PlayerController> ();
     }
     
     void Update () {
         if (Input.GetKey (KeyCode.W))
         {
             velocity.z = moveSpeed;
         }
         else if (Input.GetKey (KeyCode.S))
         {
             velocity.z = -moveSpeed;
         }
         else
         {
             velocity.z = 0;
         }
         if (controller.isGrounded)
         {
             if (Input.GetKey (KeyCode.Space))
             {
                 velocity.y = jumpForce;
             }
             else
             {
                 velocity.y = 0;
             }
         }
         else
         {
             velocity.y += Physics.gravity.y * Time.deltaTime;
         }
         controller.Move (velocity * Time.deltaTime);
         mouseX += Input.GetAxisRaw ("Mouse X") * xSensitivity * Time.deltaTime;
         transform.rotation = Quaternion.Euler (0, mouseX, 0);
         mouseY -= Input.GetAxisRaw ("Mouse Y") * ySensitivity * Time.deltaTime;
         mainCamera.transform.localRotation = Quaternion.Euler (mouseY, 0, 0);
     }
 }
 

Player Controller Script:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof (CapsuleCollider))]
 public class PlayerController : MonoBehaviour {
 
     private CapsuleCollider colliderRef;
     public float skinWidth;
     public int rayCount;
     private float raySpacing;
     private float left, right, bottom, top, back, front;
     public LayerMask layermask;
     public bool isGrounded;
     
     void Start () {
         colliderRef = GetComponent <CapsuleCollider> ();
         UpdateRaycastOrigins ();
         CheckForGround ();
     }
     
     void Update () {
         CheckForGround ();
     }
     
     public void Move (Vector3 velocity) {
         UpdateRaycastOrigins ();
         CalculateRaySpacing ();
         CollideHorizontally (ref velocity);
         CollideVertically (ref velocity);
         ColliderDepthically (ref velocity);
         transform.Translate (transform.right * velocity.x + transform.up * velocity.y + transform.forward * velocity.z);
     }
     
     void UpdateRaycastOrigins () {
         left = colliderRef.bounds.min.x + skinWidth;
         right = colliderRef.bounds.max.x - skinWidth;
         bottom = colliderRef.bounds.min.y + skinWidth;
         top = colliderRef.bounds.max.y - skinWidth;
         back = colliderRef.bounds.min.z + skinWidth;
         front = colliderRef.bounds.max.z - skinWidth;
     }
     
     void CalculateRaySpacing () {
         float bodyHeight = colliderRef.height - colliderRef.radius * 2;
         raySpacing = (bodyHeight - skinWidth * 2) / (rayCount - 1);
     }
     
     void CollideHorizontally (ref Vector3 velocity) {
         float direction = Mathf.Sign (velocity.x);
         float rayLength = Mathf.Abs (velocity.x) + skinWidth;
         for (int x = 0; x < rayCount; x ++)
         {
             Vector3 rayPos = Vector3.zero;
             rayPos.z = (back + front) / 2;
             if (direction == -1)
             {
                 rayPos.x = left;
             }
             else
             {
                 rayPos.x = right;
             }
             rayPos.y += x * raySpacing;
             RaycastHit hit;
             if (Physics.Raycast (rayPos, Vector3.right * direction, out hit, rayLength, layermask))
             {
                 velocity.x = (hit.distance - skinWidth) * direction;
                 rayLength = hit.distance;
             }
         }
     }
     
     void CollideVertically (ref Vector3 velocity) {
         float direction = Mathf.Sign (velocity.y);
         float rayLength = Mathf.Abs (velocity.y) - skinWidth;
         Vector3 rayPos = Vector3.zero;
         if (direction == -1)
         {
             rayPos.y = bottom;
         }
         else
         {
             rayPos.y = top;
         }
         RaycastHit hit;
         if (Physics.Raycast (rayPos, Vector3.up * direction, out hit, rayLength, layermask))
         {
             velocity.y = hit.distance * direction;
             rayLength = hit.distance + skinWidth;
         }
     }
     
     void ColliderDepthically (ref Vector3 velocity) {
         float direction = Mathf.Sign (velocity.z);
         float rayLength = Mathf.Abs (velocity.z) + skinWidth;
         for (int z = 0; z < rayCount; z ++)
         {
             Vector3 rayPos = Vector3.zero;
             rayPos.x = (left + right) / 2;
             if (direction == -1)
             {
                 rayPos.z = back;
             }
             else
             {
                 rayPos.z = front;
             }
             rayPos.y += z * raySpacing;
             RaycastHit hit;
             if (Physics.Raycast (rayPos, Vector3.forward * direction, out hit, rayLength, layermask))
             {
                 velocity.z = (hit.distance - skinWidth) * direction;
                 rayLength = hit.distance;
             }
         }
     }
     
     void CheckForGround () {
         Vector3 rayPos = new Vector3 (transform.position.x, bottom, transform.position.z);
         RaycastHit hit;
         if (Physics.Raycast (rayPos, Vector3.down, out hit, skinWidth, layermask))
         {
             isGrounded = true;
         }
         else
         {
             rayPos = new Vector3 (left, colliderRef.bounds.min.y + colliderRef.radius, back);
             if (Physics.Raycast (rayPos, Vector3.down, out hit, colliderRef.radius, layermask))
             {
                 isGrounded = true;
             }
             else
             {
                 rayPos = new Vector3 (right, colliderRef.bounds.min.y + colliderRef.radius, back);
                 if (Physics.Raycast (rayPos, Vector3.down, out hit, colliderRef.radius, layermask))
                 {
                     isGrounded = true;
                 }
                 else
                 {
                     rayPos = new Vector3 (left, colliderRef.bounds.min.y + colliderRef.radius, front);
                     if (Physics.Raycast (rayPos, Vector3.down, out hit, colliderRef.radius, layermask))
                     {
                         isGrounded = true;
                     }
                     else
                     {
                         rayPos = new Vector3 (right, colliderRef.bounds.min.y + colliderRef.radius, front);
                         if (Physics.Raycast (rayPos, Vector3.down, out hit, colliderRef.radius, layermask))
                         {
                             isGrounded = true;
                         }
                         else
                         {
                             isGrounded = false;
                         }
                     }
                 }
             }
         }
     }
 }
 

Comment
Add comment · Show 1
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 awplays49 · Aug 10, 2015 at 12:48 AM 0
Share

I still am having trouble with this, it would be nice to get some help soon :/

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

23 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

Related Questions

3d animation controller 0 Answers

If player is restricted on a value, why does it not "snap" back? 1 Answer

Make controller able to move backwards if mouse button is held down 0 Answers

Stick player (my own rigidbody controller) to the ground when going down the slope 2 Answers

How do I set up my players controller script (How do I change the controls used to move and look) 2 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