Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Vynatic · Jul 04, 2016 at 03:07 PM · rotationcamera rotatez-axis

How do I rotate player's camera to peak around objects. (first person shooter leaning)

After about 2-3 years of doing just 3D modeling and not really getting anywhere with my games, I decided to start learning programming. I know a bit of UnityScript(Java) and even less of C#. I decided to test out my knowledge of programming by making a script called Peaking for both C# and Java as tests. I then left C# alone as I got confused so in one section of code(for Java), I typed:

if(Input.GetKeyDown(KeyCode.E)) Transform.Rotate(0, 0, 1);

The console then gave me an error saying: "An instance of type 'UnityEngine.Transform' is required to access non static member 'Rotate'.:

I then messed around a bit and came up with this script:

function Update () { if(Input.GetKeyDown(KeyCode.E)) transform.Translate(1, 0, 0);

 if(Input.GetKeyUp(KeyCode.E))
     transform.Translate(-1, 0, 0);

 if(Input.GetKeyDown(KeyCode.Q))
     transform.Translate(-1, 0, 0);

 if(Input.GetKeyUp(KeyCode.Q))
     transform.Translate(1, 0, 0);

}

What I'm basically trying to do is rotate the camera along the Z axis to simulate the peaking effect going around a corner instead of making the head shift to the side like I did.

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 Cherno · Jul 04, 2016 at 03:25 PM 0
Share
  1. It's called peeking ;) Peaking would be when the highest point of something is reached (the peak). Compare "Peak Oil".

  2. Transform.Translate is for moving the transform. To rotate it, you have to use one of the rotation functions, such as Transform.Rotate.

  3. It seems that this question has been asked before several times, maybe looking at some of the other threads can help you:

unity fps leaning

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by GameMaker_ · Jul 04, 2016 at 04:46 PM

Firstly, each movement has to be made according to the frame rate. Update is a function which is called each frame, and according to the frame rate, you will have to make your character move more or less. Usually, it will look like:

      if(Input.GetKey(KeyCode.W))
          transform.Translate(0, 0, Time.deltaTime * speed);
     if(Input.GetKey(KeyCode.S))
          transform.Translate(0, 0, -Time.deltaTime * speed);
     if(Input.GetKey(KeyCode.A))
          transform.Translate(Time.deltaTime * speed, 0, 0);
     if(Input.GetKey(KeyCode.D))
          transform.Translate(-Time.deltaTime * speed, 0, 0);

This code can be simplified, but this one is explicit.

Then to make your character to rotate, you need to go to Edit > Project settings > Input and you will see ("Mouse X" and "Mouse Y"). It enables you to write:

          transform.Rotate(0, 0, Input.GetAxis("Mouse X"));

And you will be able to set the sensitivity (°/pixel) in the Input settings.

In the Input settings, you can see "Horizontal" and "Vertcial" too. It enables to simplify the translation script:

          transform.Translate(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, Input.GetAxis("Vertical") * speed * Time.deltaTime);
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.

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

Weird shake when I try to rotate player according to Virtual Cameras rotation. 0 Answers

rotation z of child wont work can some one help me plz 0 Answers

Get current Z rotation of transform in degrees 0 Answers

how to set z rotation to be always 0 1 Answer

How to "clamp" a y rotation in unity? 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