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 /
avatar image
0
Question by SuperNovaXM · Aug 30, 2016 at 08:07 AM · rotationinputvector3

How to rotate input based velocity vector based on relative angle of colliding wall so that player moves smoothly?

I have attempted to create a movement system that only uses player input (no physics or rigidbodies), and I want the player to interact dynamically with barriers and walls, sliding across them if the player tries to run against them. I have created a basic flowchart showing how I want to pull this off: Flowchart

However, the way I have implemented this so far has resulted in strange and glitchy behavior, most notably when trying to rotate the movement vector so the player will travel perpendicular to a wall they are running against.

Here is the script I have (sorry if it's a mess, it's a Frankenstein's monster of forum posts, previous assets, and novel code that I haven't gotten to clean up yet):

 public class PlayerMovement : MonoBehaviour
 {
     public Vector3 dir;
     public Vector3 move;
     public Vector3 normal;
     public float speedC;
     public float angle;
     public RaycastHit ray;
     public bool collision;
     public Quaternion rotation = Quaternion.identity;
 
     // Use this for initialization
     void Start()
     {
         angle = 90f;
     }
 
 
     
 
 
     void Update()
     {
 
         move = transform.position;
         
         //Get input
         var inputV = Input.GetAxis("Vertical");
         var inputH = Input.GetAxis("Horizontal");
         
         //Test for collision with wall
         if (Physics.SphereCast(move, 0.25f, move, out ray, 0.25f))
         {
             Debug.Log("Collision");
             collision = true;
         }
         
         else
         {
             collision = false;
         }
 
         //placeholder vector for input direction
         dir.x = inputH;
         dir.z = inputV;
 
         //determine angle between wall and move vector
         if (collision)
         {
             angle = Vector3.Angle(dir, ray.normal);
             Vector3 cross = Vector3.Cross(dir, ray.normal);
             if (cross.y < 0) angle = -angle;
         }
         else
         {
             angle = 90;
         }
         //debugging variables
         normal = ray.normal;
         rotation.eulerAngles = new Vector3(0, angle, 0);
 
         //slow player down based on the angle of the wall
         float speed = speedC * (angle/90);
 
         Vector3 up = new Vector3(0, angle, 0);
         move.x += inputH * Time.deltaTime * speed;
         move.z += inputV * Time.deltaTime * speed;
 
         //rotate the move vector
 if (collision)
         {
             move = move - transform.position;
             move = Quaternion.Euler(0, angle, 0) * move;
             move = move + transform.position;
         }
         //move the player
         transform.position = move;
 
 
 
     }
 
 
 }
 

This is currently very glitch as it is, phasing through walls and vibrating intensely when up against a wall, as if it's only sometimes rotating properly. If anyone has any insight on how to properly do any of this, please let me know.

untitled.png (27.1 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SunnyChow · Aug 30, 2016 at 09:18 AM

I am not good at math. but i think you should use Vector3. ProjectOnPlane() instead of Cross()

 move = Vector3. ProjectOnPlane(dir, ray.normal)
 transform.position += move;

but this doesn't work with corner. I guess it's much easier if you just use unity physics engine

Comment
Add comment · Share
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

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

Rotate player to LookAt a touch position 2 Answers

Quaternion amount/value of rotation? 2 Answers

Rotate object instead of camera around it? 0 Answers

Having Trouble with Vector3.Angle 1 Answer

Rotate player (rigidbody) towards his movement 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