• 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 Snakevenom · May 19, 2015 at 09:03 AM · physics

3D sticking to walls

I am using a character controller on a 3D object however when it is moving it sticks to walls. How can I combat this?

Script:

 using UnityEngine;
 using System.Collections;
 
 public class Movement : MonoBehaviour {
     //Key Bindings
     static public KeyCode Forward;
     static public KeyCode Left;
     static public KeyCode Right;
     static public KeyCode Down;
     static public KeyCode Jump;
     static public KeyCode Run;
 
     public Vector3 movement;
     public Vector3 grav;
 
     public CharacterController controller;
 
     public static string dir;
 
     public static bool canMove = true;
 
     public float friction;
     public float moveSpeed = 14f;
     private float acceleration;
     public float maxSpeed = 30f;
     public float jumpSpeed = 2.0F;
     public float gravity = 5.0F;
 
     private float msd;
     private float movesd;
     private float jsd;
     private float gd;
 
     public bool Moving = false;
     public bool Ascending = false;
 
     public bool BillBoard = false;
 
 
     void Start () {
 
         dir = "";
         acceleration = 0.01f;
         Forward = KeyCode.W;
         Left = KeyCode.A;
         Right = KeyCode.D;
         Down = KeyCode.S;
         Jump = KeyCode.Space;
         Run = KeyCode.LeftShift;
         msd = maxSpeed; //Max Speed Default
         movesd = moveSpeed; //Move Speed Default.
         jsd = jumpSpeed; //Jump Speed Default
         gd = gravity; //Gravity Default
         CharacterController controller = GetComponent<CharacterController>();
     }
 
 
     void Update()
     {
 
 
         if (Input.GetKey(Forward)) // Z+
         {
             dir = "up";
             if (movement.z < moveSpeed)
             {
                 movement.z += moveSpeed;
             }
 
             if (movement.z < maxSpeed)
             {
                 movement.z += acceleration;
             }
         }
         else if (Input.GetKeyUp(Forward))
         {
             if (movement.z > 0)
             {
                 movement.z = 0;
             }
         }
 
         if (Input.GetKey(Left)) // X-
         {
             dir = "left";
             if (movement.x > -moveSpeed)
             {
                 movement.x -= moveSpeed;
             }
 
             if (movement.x > -maxSpeed)
             {
                 movement.x -= acceleration;
             }
         }
         else if (Input.GetKeyUp(Left))
         {
             if (movement.x < 0)
             {
                 movement.x = 0;
             }
         }
 
         if (Input.GetKey(Right)) // X+
         {
             dir = "right";
             if (movement.x < moveSpeed)
             {
                 movement.x += moveSpeed;
             }
 
             if (movement.x < maxSpeed)
             {
                 movement.x += acceleration;
             }
         }
         else if (Input.GetKeyUp(Right))
         {
             if (movement.x > 0)
             {
                 movement.x = 0;
             }
         }
 
         if (Input.GetKey(Down)) // Z-
         {
             dir = "down";
             if (movement.z > -moveSpeed)
             {
                 movement.z -= moveSpeed;
             }
 
             if (movement.z > -maxSpeed)
             {
                 movement.z -= acceleration;
             }
         }
         else if (Input.GetKeyUp(Down))
         {
             if (movement.z < 0) //Not moving forward.
             {
                 movement.z = 0;
 
             }
         }
         if (grav.y > 0)
         {
             Ascending = true;
         }
         else
         {
             Ascending = false;
         }
         if (!Input.GetKey(Forward) && !Input.GetKey(Down) && !Input.GetKey(Left) && !Input.GetKey(Right))
         {
             movement.x = 0;
             movement.z = 0;
         }
 
         if (movement.x > maxSpeed)
         {
             movement.x = maxSpeed;
         }
         else if (movement.x < -maxSpeed)
         {
             movement.x = -maxSpeed;
         }
 
 
         if (grav.y > maxSpeed)
         {
             grav.y = maxSpeed;
         }
         else if (grav.y < -maxSpeed)
         {
             grav.y = -maxSpeed;
         }
 
         if (controller.isGrounded)
         {
             if (Input.GetKey(Jump))
                 grav.y = jumpSpeed;
         }
         if (Input.GetKey(Run))
         {
             if (controller.isGrounded)
             {
                 maxSpeed = msd + 5;
                 moveSpeed = movesd + 5;
             }
             if ((movement.x != 0 || movement.z != 0) && controller.isGrounded)
             {
                 jumpSpeed = jsd + 5;
                 gravity = gd + 5;
 
             }
             else
             {
                 maxSpeed = msd;
                 moveSpeed = movesd;
                 jumpSpeed = jsd;
                 gravity = gd;
             }
         }
 
 
 
             if (movement.x == 0 && movement.z == 0 && controller.isGrounded)
             {
                 Moving = false;
             }
             else
             {
                 Moving = true;
             }
             if (BillBoard)
             {
                 transform.rotation = Camera.main.transform.rotation;
                 //controller.height = 0.85f;
             }
             else
             {
                 transform.rotation = Quaternion.identity;
                 //controller.height = 0.95f;
             }
     
             //-----------------------------------------------------
           
             if(!canMove)
             {
                 movement = new Vector3(0, 0, 0);
             }
             grav.y -= gravity * Time.deltaTime;
             controller.Move(grav * Time.deltaTime);
             transform.Translate(movement * Time.deltaTime, Space.World);
         
       
         
 
         }
     
 
     
         
 
 }
 
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

2D 360 degress platformer example needed 0 Answers

Calculating trajectory of elliptical orbit? 2 Answers

Calculating a projectiles angle required to hit object. 2 Answers

Is there a way to automatically make physics collider frictionless on sides but not on top? 2 Answers

Simulator physics - how to get objects to "stick" to each other 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