Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
This question was closed Dec 28, 2015 at 04:35 PM by PaxStyle for the following reason:

Other

avatar image
0
Question by PaxStyle · Dec 26, 2015 at 11:18 AM · positionmathf.clamp

Mathf.Clamp with the rigidbody

Hi guys, in this script if you press Q the object goes up, instead if you press E, it goes down. but it should have limits for both (max hight and minimum down position). I sow that I have to use Mathf.Clamp to set the limits, but in the unity API ther's an example with "transform.position", I don't have It in my script. Where Can I put The Mathf.Clamp limits in my script? Thank you and happy holidays to everyone :)

 #pragma strict
 
     
     var GoUp = 1.0;
     var GoDown = 5.0;
 
    function FixedUpdate() {
    
    if (Input.GetKey(KeyCode.Q)) {
       GetComponent.<Rigidbody>().AddForce(transform.up * GoUp);
    }
    else if (Input.GetKey(KeyCode.E)) {
       GetComponent.<Rigidbody>().AddForce(-transform.up * GoDown);
    }
    else if (Input.GetKey(KeyCode.W)) {
       GetComponent.<Rigidbody>().velocity = GetComponent.<Rigidbody>().velocity * 0.9;
    }
 }
 
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 jullykado · Aug 18, 2020 at 08:07 AM 0
Share

this is the code that I use for that

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PlayerController : $$anonymous$$onoBehaviour { public float speed = 10f;

 private float _limiteH = 8f;
 private float _limiteV = 4f;
 
 private Vector2 _moveH;
 private Vector2 _moveV;
 private Rigidbody2D _rb;
 
 void Awake()
 {
     _rb = GetComponent<Rigidbody2D>();
 }
 
 // Update is called once per frame
 void Update()
 {
     float horizontalInput = Input.GetAxisRaw("Horizontal");
     float verticalInput = Input.GetAxisRaw("Vertical");
     
     _moveH= new Vector2(horizontalInput,0f);
     _moveV = new Vector2(0f,verticalInput);
     
 }
 
 void FixedUpdate()
 {
     
     float horizontalVelocity = _moveH.normalized.x * speed;
     if((_rb.position.x >= _limiteH && horizontalVelocity == speed)|| _rb.position.x <= -_limiteH && horizontalVelocity == -speed)
     {
         _rb.velocity = new Vector2(0,_rb.velocity.y);
     }
     else
     {
         _rb.velocity = new Vector2(horizontalVelocity, _rb.velocity.y);
     }
     
     float verticalVelocity = _moveV.normalized.y * speed;
     if((_rb.position.y >= _limiteV && verticalVelocity == speed)|| _rb.position.y <= -_limiteV && verticalVelocity == -speed)
     {
         _rb.velocity = new Vector2(_rb.velocity.x,0);
     }
     else
     {
         _rb.velocity = new Vector2(_rb.velocity.x, verticalVelocity);
     }
     
 }

}

1 Reply

  • Sort: 
avatar image
0

Answer by JimNero_009 · Dec 26, 2015 at 03:46 PM

The clamp function restricts the value of something to be between two values. In your case, you need to restrict your y position, so you need to firstly define two floats (ymin, ymax) and add a check in your code that your rigidbody's y position is within this range. You could do this without a clamp function - have an if statement that checks the rigidbody's y position, and if it is outside of this range, set the velocity in either the up or down direction to be zero. Otherwise, just before you end your FixedUpdate function I think adding a line a bit like this;

transform.position.y = new Vector3(Mathf.Clamp(transform.position.y, ymin, ymax), 0, 0);

might do the trick, but have not tested it.

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

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

Camera rotation around player while following. 6 Answers

Networking: How do I send player positions? 2 Answers

How Do I Drag And drop GUITextures From Set Positions? 1 Answer

Camera movement 1 Answer

change properties of gameObject 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