• 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 Mike875 · Feb 29, 2016 at 12:22 PM · c#unity 5

Why won't the X axis of the player lock like the Z axis?

I have some code here that I am using to lock the Player's movement along one axis (The one that will lead it to fall off of the fence). The code for the locking of the Z axis works but the code for the X axis won't even though I just copy and pasted it and then changed the values that needed changing.

Here's my code:

  void OnTriggerStay(Collider other)
         {
             if(other.tag == "Fence") //If the player is on the fence
             {
                 if(other.transform.rotation.y == 180 || other.transform.rotation.y == 0) //If the fence's rotation is 180 or 0
                 {
                     gameObject.transform.position = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, Mathf.Lerp(other.transform.position.z, transform.position.z, Time.deltaTime * 1)); //Lock the z axis of the player to match that of the fence
                 }
                 
                 if(other.transform.rotation.y == 90 || other.transform.rotation.y == 270) //If the fence's rotation is 270 or 90
                 {
                     gameObject.transform.position = new Vector3(Mathf.Lerp(other.transform.position.x, transform.position.x, Time.deltaTime * 1), gameObject.transform.position.y, gameObject.transform.position.z); //Lock the x axis of the player to match that of the fence
                 }
             }
         }

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 Tom01098 · Feb 29, 2016 at 01:07 PM 0
Share

Please re-format your code so it is readable.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by brunocoimbra · Feb 29, 2016 at 07:07 PM

Don't use Time.deltaTime in Lerp. I know, the documentation gives an example doing it that way, but there is no need to use it.

Lerp just pick the value relative to value x and y passed to it, so having X = 2 and Y = 10, we have:

If Mathf.Lerp(x, y, 0), we receive 2 (the first value). if Mathf.Lerp(x, y, 1), we receive 10 (the last value). if Mathf.Lerp(x, y, 0.5f), we receive 6 (the middle point between value x and y).

so if you want to copy the value, don't use Time.deltaTime, as it would give you a somewhat random value next to 0.02, what would not result in a number a little bit bigger than the first one passed to the fuction..

You can use it:

 float x = Mathf.Lerp(other.transform.position.z, transform.position.z, 0)

or it:

 float x = other.transform.position.z

Also, change those:

 if(other.transform.rotation.y == 180)
 if(other.transform.rotation.y == 90 || other.transform.rotation.y == 270)

to those:

 if(other.transform.rotation.y % 180 == 0 || other.transform.rotation.y == 0)
 if(other.transform.rotation.y % 180 != 0 && other.transform.rotation.y % 90 == 0)

the difference? checking if the rotation is a multiple of 180 or 90 is better, as it will fit in when the rotation is negative too, what seems to be your problem, as a rotation of -90 would NOT lock the X axis.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity project build issue 0 Answers

Need help with c# 1 Answer

May I ask why scrpt cannot be triggered after watching the advertisement?,May I ask why [HandleOnAdRewarded] cannot be triggered after watching the advertisement? 0 Answers

OnMouseDown not working! 0 Answers

Trouble with virtual and protected 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges