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 /
avatar image
0
Question by xyelenemuisc · Mar 30, 2018 at 01:47 AM · movementvector3limit

How to set maximum distance for a player going left and right?

I have a player model that goes left and right with the arrow keys in front of the camera. I would like to set a Maximum Distance for the left and right side so the player doesn't go outside of the camera's view.

Help would be appreciated :)

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 RyanAtEvno · Mar 30, 2018 at 01:57 AM

Does the camera position move? and does the player move a specific distance each time?

Comment
Add comment · Show 3 · 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 xyelenemuisc · Mar 30, 2018 at 02:17 AM 0
Share

The Player is moving forward at the same speed as the camera but the camera does not follow the player as it goes left and right.

avatar image RyanAtEvno xyelenemuisc · Mar 30, 2018 at 03:07 AM 0
Share

I used this script in a project a year or so ago. Ignore the stuff you don't need but here. It assumes you want to kind of "switch lanes" left and right. Like an infinite runner maybe.`public class LaneSW : $$anonymous$$onoBehaviour {

 public AudioSource tickSource;
 public float speed = 6.0F;
 public float jumpSpeed = 8.0F;
 public float gravity = 20.0F;
 private Vector3 moveDirection = Vector3.zero;

 public int laneNumber = 0;
 public int lanesCount = 4;
 bool didChangeLastFrame = false;
 public float laneDistance = 2;
 public float firstLaneXPos = 0;
 public float deadZone = 0.1f;
 public float sideSpeed = 5;

 void Start() 
 {
     tickSource = GetComponent<AudioSource> ();
 }

 void Update() 
 {
     //Access the objects Character Controller
     CharacterController controller = GetComponent<CharacterController>();

     float input = Input.GetAxis("Horizontal");
     //$$anonymous$$ake sure the object is grounded before doing the math for lane changes
     if (controller.isGrounded)
     if($$anonymous$$athf.Abs(input) > deadZone) {
         if(!didChangeLastFrame) {
             didChangeLastFrame = true; //Prevent overshooting lanes
             laneNumber += $$anonymous$$athf.RoundToInt($$anonymous$$athf.Sign(input));
             if(laneNumber < 0) laneNumber = 0;
             else if(laneNumber >= lanesCount) laneNumber = lanesCount - 1;
         }
     }
     else 
     {
         didChangeLastFrame = false; 

         moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
         moveDirection = transform.TransformDirection(moveDirection);
         moveDirection *= speed;
         if (Input.GetButton("Jump"))
             moveDirection.y = jumpSpeed;

     }
     Vector3 pos = transform.position;
     pos.x = $$anonymous$$athf.Lerp(pos.x, firstLaneXPos + laneDistance * laneNumber, Time.deltaTime * sideSpeed);
     transform.position = pos;

     moveDirection.y -= gravity * Time.deltaTime;        
     controller.$$anonymous$$ove (moveDirection * Time.deltaTime);
 }


 void OnTriggerEnter(Collider other) 
 {
     if (other.gameObject.CompareTag ("Pick Up"))
     {
         tickSource.Play ();

         other.gameObject.SetActive (false);
     }
 }
     

     

} `

avatar image xyelenemuisc RyanAtEvno · Apr 01, 2018 at 12:47 AM 0
Share

Could you please point out which part stops the player from going off the camera?

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

130 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 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 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

Limit object position which is moved by accelerometer 0 Answers

Smooth motion physics Playmaker 0 Answers

Convert WASD to local rotation 1 Answer

Run animation click/touch to move problem 0 Answers

Player moving up and down hill at different speeds (vector projection) 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