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 imp903 · Jul 10, 2015 at 01:03 PM · javascriptpositionclampcover

clamping player position while in cover

So, I'm trying to integrate a cover system, and so far i have it to the point where the player can lerp to the object, and walk along it's surface. But, I'm running into issues right now, because if once the player walks along the object, they can just keep walking beyond the edges of the cover object, which is, needless to say sort of a problem. I tried clamping the x and z positions of the player, creating an effective window that they can sort of walk in, and because the direction of movement is also limited, it seemed a fair attempt. But, when they hit the edge of that window, they slide along it like a collider I think, which causes the player to then be pushed into the cover object, but then my script resets the position to avoid having the player be displaced. That creates a very noticeable jitter at the edges of that walkable window. Secondly: I'm not sure how to detect the edges of a cover object, and I don't want to manually have to enter dimensions for every single object face that the player could ever want to take cover on. Here's what I have so far, and the problem right now lies in those last few lines of code about limiting the character to walking only behind the cover. Any help improving this code would be much appreciated.

 function restrictMovement()
 {
     //establishes normal reference to be applied to object later
     var normal : Vector3 = new Vector3 (0, 1, 0);
     //establishes tangnt vector based on the normal reference vector and the transform of the object
     var tangent : Vector3 = Vector3.Cross(normal, coverObject.transform.forward);
     
     //ensures no errors are acquired if the magnitude is 0
     if (tangent.magnitude == 0)
     {
         tangent = Vector3.Cross(normal, Vector3.up);
     }
     //can only move after lerp is done
     yield WaitForSeconds(lerpTime + 0.1);
     
     //adjust movement to respond to the player differently    
     fpsController.moveDirection =-Input.GetAxis("Horizontal") * tangent;
     fpsController.moveDirection = fpsController.moveDirection * fpsController.speed;
     
     //clamps player movement to size of object
     transform.position.x = Mathf.Clamp(transform.position.x, coverObject.transform.position.x - 3, coverObject.transform.position.x+3);
     transform.position.z = Mathf.Clamp(transform.position.z, coverObject.transform.position.z-3,coverObject.transform.position.z + 3);
 }
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 imp903 · Jul 11, 2015 at 06:16 PM 0
Share

Ok, so I replaced the last two lines of code, and I like this better just because I can visualize what my own code is saying a little easier, but it still has heavy jitter at the edge of the walk-able area, which I can't seem to fix. Here's the replaced code:

     relPosition = transform.position - coverObject.transform.position;
     
     transform.position = coverObject.transform.position + Vector3.Clamp$$anonymous$$agnitude(relPosition, 4);

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

21 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

Related Questions

How Can I Stop Rotation From This Script? 0 Answers

Clamping "X" Position Movement On Platform (Code Inside) 2 Answers

Array.Push() for Vector3[] or how to add items to Vector3 array without knowing index 1 Answer

How to tell if two blocks are right next to each other?(2D) 1 Answer

2D get touch input 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