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 /
  • Help Room /
avatar image
0
Question by CraftyMaelyss · Mar 30, 2017 at 01:14 AM · c#cameracamera-movement

I tried a script for moving the camera strictly with the arrow keys(NOT wasd) and it caused errors.

After looking this up and trying out some online scripts, I ran into a whole bunch of errors and I want to ask the community if I've missed something:

 using UnityEngine;
 using System.Collections;
 
 public class KalusCamera : MonoBehaviour {
 private float speed = 5.0f;
     // Use this for initialization
     void Start () {
     
     }
 
     void Update ()
     {
         if (Input.GetKey (KeyCode.RightArrow)) 
         {
             transform.Translate (Vector3 (speed * Time.deltaTime, 0, 0));
         }
 
         if (Input.GetKey (KeyCode.LeftArrow)) 
         {
             transform.Translate (Vector3(-speed * Time.deltaTime, 0, 0));
         }
 
         if (Input.GetKey (KeyCode.DownArrow)) 
         {
             transform.Translate (Vector3(0,-speed * Time.deltaTime, 0, 0));
         }
         if (Input.GetKey (KeyCode.UpArrow)) 
         {
             transform.Translate (Vector3(0,speed * Time.deltaTime, 0, 0));
         }
 
     }
 }

What I'm ultimately aiming for is the camera to move with just the arrow keys as the player is controlled by the WASD keys. In the past I used the FPS prefab with the mouse look but now I want to change it so the player doesn't need the mouse to move the camera.

Again this was the same script that I saw others share with other users who were struggling to make a game and from the looks of it, no one else had experienced these errors from them.

I'm using Unity 5 if it helps.

These are the errors I keep getting: Screenshot Property of CraftyMaelyss

Here's a good example of what I'm aiming for: https://www.youtube.com/watch?v=MIyMN1F-E4Q

See how the camera still stays focused on the player even when the camera is moved around? That is what I'm aiming for. The player will be able to move the camera with the arrow keys and control the player with WASD.

camera-script-error.png (76.0 kB)
Comment
Add comment · Show 2
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 PizzaPie · Mar 31, 2017 at 10:55 AM 0
Share

Well first issue, as mentioned below, is you are not using the "new" keyword before the vector leading the compiler to assume vector3 is a function and not a struct, which it is. Second you pass 4 arguments on the last two vector3 but the vector3 can take 0, 2 or 3 floats as arguments. Here that should work:

 void Update()
     {
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow))
         {
             transform.Translate(new Vector3(Time.deltaTime * speed, 0, 0));
         }
 
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow))
         {
             transform.Translate(new Vector3(-Time.deltaTime * speed, 0, 0));
         }
 
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.DownArrow))
         {
             transform.Translate(new Vector3(0, -Time.deltaTime * speed, 0 ));
         }
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.UpArrow))
         {
             transform.Translate(new Vector3(0, Time.deltaTime * speed,0 ));
         }
     }

Note: you can also use transform.Translate without a vector3 like this: transform.Translate(Time.deltaTime * speed, 0, 0);

avatar image CraftyMaelyss PizzaPie · Apr 01, 2017 at 02:34 AM 0
Share

I tried this but it didn't work properly. The camera moves independent of the player despite being attached to the player model. Pressing any arrow keys makes the camera move like the player model except pressing up makes it fly above the player and makes the player themselves move forward, while down makes the camera sink into the ground.

What I'm trying to accomplish is making the camera move while still remaining behind the player. For example, pressing left will make the camera turn to the left(or rotate) and pressing right will make the camera turn to the right. Pressing up will make the camera look up while pressing down will make the camera look down.

Here's a good example of what I'm ai$$anonymous$$g for: https://www.youtube.com/watch?v=$$anonymous$$Iy$$anonymous$$N1F-E4Q

See how the camera still stays focused on the player even when the camera is moved around? That is what I'm ai$$anonymous$$g for. The player will be able to move the camera with the arrow keys and control the player with WASD.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Rhombuster · Mar 30, 2017 at 01:27 AM

You make repeated calls to a variable called "speed" but you never declared it. Add it to the top just under this class declaration:

 public class KalusCamera : MonoBehaviour {
     private float speed = 5.0f;

Let me know if you have any other questions.

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 CraftyMaelyss · Mar 30, 2017 at 01:45 AM 0
Share

I'm still getting several errors: Screenshot property of Crafty$$anonymous$$aelyss

camera-script-error.png (76.0 kB)
avatar image Rhombuster · Mar 30, 2017 at 01:53 AM 0
Share

A vector3 is an object. You need to tell unity to create the vector object by using the "new" keyword. So your lines that look like this:

transform.Translate (Vector3(-speed * Time.deltaTime, 0, 0)); Should look like this:

 transform.Translate (new Vector3(-speed * Time.deltaTime, 0, 0));

I can keep helping, but I would consider doing some tutorials since

avatar image CraftyMaelyss Rhombuster · Mar 31, 2017 at 07:51 AM 0
Share

That's the thing, if I type "new" in the code, I get errors. This caused a severe error with my script and started working again once "new" had been removed. I even tested it again and it caused more errors.

I have watched several tutorials, I always do when trying something new or trying to code a script or create a new affect in Unity. When those tutorials give me errors and I get no help or feedback from asking, I come here and ask what is wrong.

I understand a lot of new or inexperienced users don't try searching or researching or looking into it properly but I always ask as a last resort. This project I have been working on for 2 years and I've had to learn everything through tutorials, research and asking questions when nothing works, so please don't just assume I haven't tried. I try and I put all my effort into this and I ask when everything I try just doesn't work.

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

342 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 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 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

How to have your player controls change while your camera rotates? 0 Answers

Lerp Not working 1 Answer

Clamping when Zoom changes? 0 Answers

How to improve smooth camera script. 0 Answers

Camera Jitters When Displacing and Rotating Smoothly 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