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 Kruczek · May 11, 2018 at 02:34 PM · camera-movement

Is it possible to reuse input's acceleration in custom script?

I'd like to implement an RTS-like camera with panning. I'd like the movement to be smooth, and I can do that easily with

 transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime)

Because GetAxis returns a [0..1] value to simulate acceleration.

Now I'd like to be able to move the camera by moving my mouse to the edge of the screen. Is it possible to reuse the acceleration/smoothing code from GetAxis? I'd like those two methods to move the camera identically.

And I'd like not writing my own smoothing effect from scrach.

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

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ImpOfThePerverse · May 11, 2018 at 03:24 PM

I'm pretty sure the input manager information is still inaccessible, as discussed in this thread:

https://forum.unity.com/threads/input-manager.166712/

What you'll need to do is create your own acceleration value and tune it to match the key acceleration.

 public float timeToFullSpeed;
 float speedTimer;

 // Calculate mouseIsRight and mouseIsLeft from mouse screen position
 void MouseMovement(bool mouseIsRIght, bool mouseIsLeft)
 {
     if (mouseIsRight)
     {
         speedTimer = Mathf.Min(timeToFullSpeed, speedTimer + Time.deltaTime);
     }
     else if (mouseIsLeft)
     {
         speedTimer = Mathf.Max(-timeToFullSpeed, speedTimer - Time.deltaTime);
     }
     else
     {
         if (speedTimer > 0)
         {
             speedTimer = Mathf.Max(0f, speedTimer - Time.deltaTime)
         }
         else
         {
             speedTimer = Mathf.Min(0f, speedTimer + Time.deltaTime)
         }
     }
     transform.Translate(Vector3.Right * (speedTimer / timeToFullSpeed) * Time.deltaTime);
 }
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 Kruczek · May 12, 2018 at 09:29 PM 0
Share

Thanks! That’s what I ended up doing.

avatar image
0

Answer by Happeloy · May 11, 2018 at 02:46 PM

Sure, no problem.

 float value = Camera.main.ScreenToViewportPoint(Input.mousePosition).x;

will give you a value from 0-1, where 0 is the left side of the screen, 0.5 is the middle, and 1 is the right side of the screen.

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 Kruczek · May 11, 2018 at 02:54 PM 0
Share

No no, I'm trying to make a camera like in RTS games for example. You can pan with keyboard or by moving your mouse to the edge of the screen. The question is, it possible to reuse the acceleration from keyboard input, so I'm able to pan smoothly using the edges of the screen.

avatar image Happeloy Kruczek · May 11, 2018 at 10:29 PM 0
Share

Well, in that case, I think that either I've misunderstood your question, or you have misunderstood what Input.GetAxis("Horizontal") does, it does not give you an acceleration, it gives you the current amount of horizontal movement on an axis. If you were to attach a gamepad to your system, and put it half way to the right, it would read 0.5.

If you want to get the acceleration of an input, you should look at the delta value of the input. In other words, you get the input position from one frame,and compare it to the input of the last frame. Something like this:

  float currentvalue  = Camera.main.ScreenToViewportPoint(Input.mousePosition).x;
 
 float acceleration = lastvalue - currentvalue;
 lastvalue = currentvalue;

where lastvalue is decleared globally.

avatar image ImpOfThePerverse · May 12, 2018 at 07:07 AM 0
Share

I think what he's going for is having the speed ramp up over time. Using GetAxis for a keypress returns a value that ramps up from 0 to 1 at a rate deter$$anonymous$$ed by the axis' sensitivity setting, so the window movement would accelerate from zero ins$$anonymous$$d of being a simple go/no go. The code I wrote below should make the mouse-position-driven movement take timeToFullSpeed seconds to go from 0 to full speed. Just set timeToFullSpeed to half a second or so.

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

88 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

Related Questions

Moving between two cameras 1 Answer

It is possible to add a Cinemachine ColliderComponent to Cinemachine FreeLookComponent 0 Answers

How to make the camera follow the player while still being able to be rotated? 1 Answer

Objects randomly dissapear in Game View 1 Answer

Rotate Camera to the object 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