Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
-3
Question by maroonrs2 · Apr 19, 2012 at 05:42 AM · camerarotationfps

First Person Shooter Help With Cam

Ok hi, I came up with this: :]

 var walkSpeed = 10;
 var horizontalSpeed = 2.0;
 var verticalSpeed = 2.0;
 
 function Update()
 {
     var e:Event = Event.current;
     if(e.isMouse)
     {
         var currentPos = e.mousePosition;
         if(e.x > currentPos.x)
         {
             transform.rotate(horizontalSpeed,0,0);
             e.x = currentPos.x;
         } else if(e.x < currentPos.x)
         {
             transform.Rotate(0,verticalSpeed,0);
         }
     }
     if(Input.GetButton("W"))
     {
         transform.position += transform.foward * walkSpeed * Time.deltaTime;
     }
     if(Input.GetButton("S"))
     {
         transform.position += -transform.foward * walkSpeed * Time.deltaTime;
     }
     if(Input.GetButton("A"))
     {
         transform.position += -transform.right * walkSpeed * Time.deltaTime;
     }
     if(Input.GetButton("D"))
     {
         transform.position += transform.right * walkSpeed * Time.deltaTime;
     }
     
 }


As you can see my wasd keys move the character. Also i do not want the camera to do a barrel roll when i look over.

Comment
Add comment · Show 5
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 maroonrs2 · Apr 19, 2012 at 06:14 AM 0
Share

no one? REALLY?

avatar image syclamoth · Apr 19, 2012 at 06:22 AM 0
Share

Well, for starters this is pretty far removed from any FPS controller I would have written. For example, why are you using hard-coded keys, ins$$anonymous$$d of input axes? Why aren't you using the Input class for anything? It occurs to me that your mouselook is overly simplistic and error-prone. Look through the standard assets, there's a few good things in there for all of this- have a read through them, and learn how to do things properly.

avatar image maroonrs2 · Apr 19, 2012 at 06:25 AM 0
Share

wow harsh. there is no jumping in this game so im using a strafe. I have been learning slowly. Ins$$anonymous$$d of being a critic, you should actually provide factual help.

avatar image syclamoth · Apr 19, 2012 at 06:35 AM 0
Share

$$anonymous$$aybe if you weren't so argumentative, your karma would be over -20. Just saying.

avatar image maroonrs2 · Apr 19, 2012 at 07:05 AM 0
Share

I'm not argumentative, there are random answer hoppers disliking stuff.

1 Reply

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

Answer by syclamoth · Apr 19, 2012 at 06:47 AM

1: Use the Input class. It includes several very useful functions for simplifying input calculations- for example, you can do something like this to reduce your movement section to just a few lines:

 var inputVector : Vector3 = 
     Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
 transform.Translate(inputVector * Time.deltaTime * speed);

This will give you a simple, physics-free strafe movement in just 2 lines.

2: As for the mouselook, I find a good way of handing this is to split the camera up into a hierarchy of objects- with the base player at one end, and the camera at the other:

 Player (rotates around y axis)
 ---> Camera (rotates around Player's x axis)

Then, do something like this:

 var xSensitivity : float;
 var ySensitivity : float;
 
 var maxHeight : float;
 var minHeight : float;

 private var xPos : float;
 private var yPos : float;

 var myTrans : Transform;
 var camTrans : Transform;

 function Update () {
     yPos = Mathf.Clamp(yPos - Input.GetAxis ("Mouse Y") * ySensitivity, minHeight, maxHeight);
     xPos = xPos + Input.GetAxis("Mouse X") * xSensitivity;
     myTrans.rotation = Quaternion.AngleAxis(xPos, Vector3.up);
     camTrans.localRotation = Quaternion.AngleAxis(yPos, Vector3.right);
 }

Assign things to the correct slots (camera in cam slot, character in myTrans slot), and it will give you a nice, limited viewport.

Comment
Add comment · Show 8 · 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 maroonrs2 · Apr 19, 2012 at 07:00 AM 0
Share

ok i get this. ut the one thing that stumps me is: heres my tree Character > main camera... now myTras would be? and Cam trans would be?

avatar image syclamoth · Apr 19, 2012 at 07:05 AM 0
Share

myTrans is the character, camTrans is the camera. Assign them in the editor.

avatar image syclamoth · Apr 19, 2012 at 07:05 AM 0
Share

Oh, make sure the camera is a transform child of the charater, it won't work otherwise.

avatar image maroonrs2 · Apr 19, 2012 at 07:19 AM 0
Share

ok i had to play with some variables. works fine. How do i lock my mouse? so i can press esc and it unlocks and vise versa

avatar image syclamoth · Apr 20, 2012 at 02:29 AM 0
Share

Use Screen.lockCursor = true / false;

Show more comments

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

Can't stop camera from rotating on Z Axis 1 Answer

Rotating Player with Mouse 0 Answers

How does this first person camera script work? 0 Answers

Setting camera pitch - Never works? 0 Answers

Unity3d FPS Gun rotating on camera issue 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