I’m trying to have a camera move overhead simply with wasd, don’t have much knowledge about coding but tried to figure out and made this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
float movex = 0;
float movez = 0;
public static bool GetKeyDown(string w, string a, string s, string d);
// Start is called before the first frame update
void Start()
{
Vector3 move = new Vector3(movex, 0.0f, movez);
transform.position(move)
}
// Update is called once per frame
void Update()
{
switch (Input.GetKeyDown())
{ case "w": movex++; break;
case "s": movex--; break;
case "a": movez--; break;
case "d": movez++; break;
default: movex = 0.0; movez = 0.0; break;
}
transform.Translate(move)
}
}
Unity gives “Assets\Camera\Movement.cs(14,33): error CS1002: ; expected” when I try to use unity, not that I didn’t expect it to be wrong in some way. Any help, especially with using transform? links to helpful guides are appreciated although i’ve been going off the unity docs and have been over my head, so I can’t guarantee the same won’t happen with other resources.
Movement is just changing the numbers of Transform component’s Position, like when you move an object in the editor.
Input is also just a number, usually 0, so you just have to add it to the position. transform.Translate will automatically add to the position every frame.
void Update
{
float horizontalAxis = Input.GetAxis("Horizontal"); // A and D - see Project Settings > Input
float verticalAxis = Input.GetAxis("Vertical"); // W and S
Vector3 moveDir = new Vector3(horizontalAxis, 0f, verticalAxis); // Vector3 is just 3 floats in the one structure
transform.Translate(moveDir * Time.deltaTime); // deltaTime to get the same result at different frame rates
// Or all in one line
//transform.Translate(new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical")) * Time.deltaTime);
}
first of all, thank you, I think I understand what your code is doing to translate the camera using input. A couple questions: first you declared the axes in Update instead of Start, would it not work in start? I thought maybe it would break or slow the program down if you declared the same variable every time Update runs, but that’s just my guess. Also I noticed you didn’t include the start function, would it work how I put it in my code? I was trying to set the camera’s initial position with translate.position but i didn’t think that would work. Also, do you know how one would set the camera’s automatic rotation so it is looking down at a 60 deg. angle automatically? would it be in the start function with translate.rotation or something?
You’d have to get the input value every frame, because input can change every frame.
If you did it in Start, you would read the input once (which would likely be no input), and nothing will happen.
Yes, you don’t actually need to include the default Start and Update methods that are generated when you create a new MonoBehaviour script.
That makes sense, but what I was asking is if Stardog’s code would work on its own without anything in Start, or if he was just providing a snippet. If it was just a snippet, do you think you could help me with what to put in Start to set the initial position and rotation of the camera?
I made some changes with both @Stardog and the manual’s help, but the Start stuff to set position and rotation is still just a shot in the dark, could I get any more help?
public class Movement : MonoBehaviour
{
float movex = 0;
float movez = 0;
// Start is called before the first frame update
void Start()
{
Transform tr = GetComponent<Transform>();
Vector3 moveDir = new Vector3(movex, 0.0f, movez);
tr.position(moveDir);
tr.rotation(60f, 0f, 0f);
}
// Update is called once per frame
void Update()
{
movex = Input.GetAxis("Horizontal"); // A and D - see Project Settings > Input
movez = Input.GetAxis("Vertical"); // W and S
moveDir = Vector3(movex, 0f, movez); // Vector3 is just 3 floats in the one structure
transform.Translate(moveDir * Time.deltaTime);
}
}
Edit: i changed floats movez and movex to 0f, but as I guessed, the error ("
Assets\Camera\Movement.cs(14,12): error CS1955: Non-invocable member ‘Transform.position’ cannot be used like a method.") still persists.
Thanks so much for your help, there aren’t any code errors now! at first, it was moving ahead according to the rotated camera instead of on the z axis only when forward/backward was inputted, probably due to using horizontal and vertical input instead of straight from the keys, but after a long time I figured out i could just snap the camera to 0 and back to 60 before the frame renders. Would that maybe cause lag or am I good?
You’ll be good.
Don’t worry too much about performance until it starts becoming an issue (not that setting the camera’s position here would cause any performance issues).