• 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 /
This question was closed Jan 23, 2015 at 08:13 PM by Ujean917 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Ujean917 · Jan 19, 2015 at 05:52 PM · c#mathf.clamp

c#: Mathf.Clamp causing unwanted behavior

Hello I am trying to make my character move in a specified area and no further so that they would stay in the view of the camera. The things I think to know about the game I'm trying to make, so that it might help us better solve this, is that the playercharacter is childed to an empty game object that moves forward and the playercharacter is free to move anywhere under that forward-moving-empty-game object-parent.

The specific codes I am trying to use to limit the player from just moving out of sight is this:

 private float minX = -15.0f;
     private float maxX = 15.0f;
     private float minZ = 0.0f;
     private float maxZ = 20.0f;
 transform.position = new Vector3(Mathf.Clamp(transform.position.x, minX, maxX), 0, 
                                  Mathf.Clamp(transform.position.z, minZ, maxZ));

And when I try implementing this code into my playerMovement Script the playercharacter cannot move further than the input float variables as expected, but unexpectedly will be stopped by an invisible force that I cannot imagine why. The player can still move left or right but cannot move forward and is pushed back to what appears to be the value where I set the player could move furthest back. If someone could give me advice/help here that would be much appreciated. Here is the code:

 public class PlayerMovement : MonoBehaviour 
 {
 
     public float movementSpeed = 10.0f;
     public float angleChangeSpeed = 100.0f;
 
     public bool isDead = false;
     
     **private float minX = -15.0f;
     private float maxX = 15.0f;
     private float minZ = 0.0f;
     private float maxZ = 20.0f;**
 
     // Update is called once per frame
     void Update () 
     {
         if(!isDead)
         {
             **transform.position = new Vector3(Mathf.Clamp(transform.position.x, minX, maxX), 0, 
                                              Mathf.Clamp(transform.position.z, minZ, maxZ));**
             // new code
             float horizontal = Input.GetAxis ("Horizontal");
             float vertical = Input.GetAxis ("Vertical");
 
             Vector3 direction = new Vector3 (horizontal, 0, vertical);
             Vector3 finalDirection = new Vector3 (horizontal, 0, 1.0f);
 
 
             transform.position += direction * movementSpeed * Time.deltaTime;
 
             transform.rotation = Quaternion.RotateTowards (transform.rotation, Quaternion.LookRotation (finalDirection), Mathf.Deg2Rad*angleChangeSpeed);
         }
     }
 
     void SetDead(bool dead)
     {
         Debug.Log ("SetDead was reached");
         isDead = dead;
     }
 }

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

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by Owen-Reynolds · Jan 19, 2015 at 06:36 PM

Quoting: "playercharacter is childed to an empty game object that moves forward and the playercharacter is free to move anywhere under that forward-moving-empty-game object-parent."

So, you want the player to be "leashed" to the moving object, like it's a small dog being walked by a steady-paced human?

Use localPosition instead of position. It's the distance from your parent.

You can experiment with this in the Inspector. Whenever you have a parent, the Inspector numbers it shows you in the Position slots are really for localPosition. So, you can hand-move the parent, then hand-change the player-child's position (really localPosition) to get a feel.

Same thing goes for rotation (but if the moving parent never turns and always faces 000, you don't have to worry.)

Comment
Add comment · Show 2 · 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
avatar image Ujean917 · Jan 19, 2015 at 06:41 PM 0
Share

Yes I guess leashing is also another good word for it. I'm assu$$anonymous$$g when you said to use localPosition is to replace the ($$anonymous$$athf.Clamp(transform.position.x, $$anonymous$$X, maxX) to something like ($$anonymous$$ath.Clamp(transform.localPosition.x, $$anonymous$$X,maxX)). I'll try that out and many other places that has position to see what happens.

avatar image Ujean917 · Jan 19, 2015 at 06:48 PM 0
Share

Okay so from what I found is that after doing the thing I said which was to make ($$anonymous$$athf.Clamp(transform.position.x, $$anonymous$$X, maxX)) to something like ($$anonymous$$athf.Clamp(transform.localPosition.x, $$anonymous$$X,maxX)), it made the character incapable of moving whatsoever except making it turn or face a direction.

So then I tried making the transform.position, that was to be given a value for after the code did its work, into transform.localPosition it finally did what I wanted it to do which was to move around unhindered up to the value I allowed it and stay motionless yes still be 'leashed' properly and move because the parent is moving forward and not have some invisible force push back.

This is a great feeling. And thanks.

avatar image
0

Answer by Baste · Jan 19, 2015 at 06:11 PM

I copy-pasted your script, and it works just as intended: the object moves freely within the limits given, so it's x will be between -15 and 15, and it's z will be between 0 and 20.

If it's not working for you (you can't change the x or z value by pressing your input), you have probably set up the Horizontal or Vertical axes in the Input Manager wrong, or you have some other script interfering with this script.

Comment
Add comment · Show 1 · 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
avatar image Ujean917 · Jan 19, 2015 at 06:29 PM 0
Share

Good to hear its working for other people. I don't think the Horizontal or Vertical axes are wrong, or is it?, because I have a mouse and keyboard setup for the game and I don't recall changing it in the Input $$anonymous$$anager. So it may be another script like you said. However, it still confuses me how it would act like this but whenever the code is commented out it works perfectly fine, albeit with the player still able to move out of sight.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Mathf.Clamp Negative min not negative? 1 Answer

Locking movement in the X and Z axis 2 Answers

Help with Clamping X Rotation 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