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:
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.
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);
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.
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.
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
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
Follow this Question
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