-
-
Save robertcedwards/dbaa2955a6e6e13f06df to your computer and use it in GitHub Desktop.
private float speed = 2.0f; | |
public GameObject character; | |
void Update () { | |
if (Input.GetKey(KeyCode.RightArrow)){ | |
transform.position += Vector3.right * speed * Time.deltaTime; | |
} | |
if (Input.GetKey(KeyCode.LeftArrow)){ | |
transform.position += Vector3.left* speed * Time.deltaTime; | |
} | |
if (Input.GetKey(KeyCode.UpArrow)){ | |
transform.position += Vector3.forward * speed * Time.deltaTime; | |
} | |
if (Input.GetKey(KeyCode.DownArrow)){ | |
transform.position += Vector3.back* speed * Time.deltaTime; | |
} | |
} |
i love this
void Update () {
if (Input.GetKey(KeyCode.RightArrow)){ transform.position += Vector3.right * speed * Time.deltaTime; } if (Input.GetKey(KeyCode.LeftArrow)){ transform.position += Vector3.left * speed * Time.deltaTime; } if (Input.GetKey(KeyCode.UpArrow)){ transform.position += Vector3.up*speed* Time.deltaTime; }forward; if (Input.GetKey(KeyCode.DownArrow)){ transform.position += Vector3.down *speed * Time.deltaTime; } }
it says: (1,6): error CS0116: A namespace cannot directly contain members such as fields or methods
private float speed = 4.0f;
public GameObject character;
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.RightArrow)){
transform.position += Vector3.right * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.LeftArrow)){
transform.position += Vector3.left* speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.UpArrow)){
transform.position += Vector3.forward * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.DownArrow)){
transform.position += Vector3.back* speed * Time.deltaTime;
}
}
use this as movement script
then make another script for jump and copy paste this
rivate float speed = 10.0f;
// Use this for initialization
void Start () {
}
public GameObject character;
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.Space)){
transform.position += Vector3.up* speed * Time.deltaTime;
}
}
im still cant use that too
when I press play my character falls through the floor
You need to add a box collider to the floor
how do you program controller buttons
when I press play my character falls through the floor
add a collider to your player and the object you want to stand on
if i paste out it don't works! it has a problem with the { but the next page ???? please help me! i don't find a perfect code. just i want to controll the camera with the w a s d or the arrows
the "vector3" in my visual studio it's not gteen, and it not works. please help!
what does DeltaTiem stands for?
Uses a real world time per init scale instead of a frame by frame speed.
for Xulum12 if you still need the answer. I got the problem too but it was easy to solve. i think, im not sure if its same by you, yo uneed to
make a "{" after "public class (your script name) : MonoBehaviour" and at the end . For me it works.
what do i change so i use both WASD and ARROWKEYS for control
what do i change so i use both WASD and ARROWKEYS for control
Just add a second set of if statements with the same parameters. For example:
Void moveFunction()
{
if (Input.GetKey(KeyCode.W))
transform.Translate(0,0,1);
if (Input.GetKey(KeyCode.insert new 2nd key code here))
transform.Translate(0,0,1);
}
what does DeltaTiem stands for?
Basically, it is the time it took to run the previous frame of instance
Add a cube into your game. Add a Rigidbody component to the cube. Add a Movement Script to the cube.
Paste the following.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public float forwardsForce = 40f;
public float sidewaysForce = 40f;
public Rigidbody rb;
void Update()
{
MovePlayer();
}
void MovePlayer()
{
if (Input.GetKey(KeyCode.UpArrow))
{
rb.AddForce(0, 0, forwardsForce * Time.deltaTime, ForceMode.VelocityChange);
}
if (Input.GetKey(KeyCode.DownArrow))
{
rb.AddForce(0, 0, -forwardsForce * Time.deltaTime, ForceMode.VelocityChange);
}
if (Input.GetKey(KeyCode.LeftArrow))
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey(KeyCode.RightArrow))
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
}
}
Ctrl+ s to save go back into unity refrence the rigidbody.
And it should work.
Oh and when you say for example you have a variable called moveSpeed that is = 10 if you say moveSpeed * Time.deltaTime basically your making moveSpeed frame rate independent.
What I mean is if you have a computer that runs 100 fps and a computer that runs 20 fps
moveSpeed would be 1 on the 100 fps computer. And it would be 5 on the 20 fps computer so it basically evens it out independent of your frame rate.
how to use mouse to look up,left/right,down
Add a cube into your game. Add a Rigidbody component to the cube. Add a Movement Script to the cube.
Paste the following.using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Movement : MonoBehaviour
{
public float forwardsForce = 40f;
public float sidewaysForce = 40f;public Rigidbody rb; void Update() { MovePlayer(); } void MovePlayer() { if (Input.GetKey(KeyCode.UpArrow)) { rb.AddForce(0, 0, forwardsForce * Time.deltaTime, ForceMode.VelocityChange); } if (Input.GetKey(KeyCode.DownArrow)) { rb.AddForce(0, 0, -forwardsForce * Time.deltaTime, ForceMode.VelocityChange); } if (Input.GetKey(KeyCode.LeftArrow)) { rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange); } if (Input.GetKey(KeyCode.RightArrow)) { rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange); } }
}
Ctrl+ s to save go back into unity refrence the rigidbody.
And it should work.
### that didn't work for me so i modified it a bit:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public float forwardsForce = 40f;
public float sidewaysForce = 40f;
public Vector3 up;
public Vector3 down;
public float jumpForce = 4f;
public Rigidbody rb;
void Start()
{
up = new Vector3(0.0f, 2f, 0.0f);
down = new Vector3(0.0f, -2f, 0.0f);
}
void Update()
{
MovePlayer();
}
void MovePlayer()
{
if (Input.GetKey(KeyCode.W))
{
rb.AddForce(up * jumpForce, ForceMode.Impulse);
}
if (Input.GetKey(KeyCode.S))
{
rb.AddForce(down * jumpForce, ForceMode.Impulse);
}
if (Input.GetKey(KeyCode.A))
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey(KeyCode.D))
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
}
}
follow the same instructions.
Hope i helped someone!
None of these work for me :/
Alright, if anyone is still here, apply a rigidbody and collider to whatever object your player is, apply a script called "PlayerMovement", and copy this code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed = 2f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetKey(KeyCode.W))
{
transform.position += Vector3.forward * speed * Time.deltaTime;
}
if(Input.GetKey(KeyCode.S))
{
transform.position += Vector3.back * speed * Time.deltaTime;
}
if(Input.GetKey(KeyCode.A))
{
transform.position += Vector3.left * speed * Time.deltaTime;
}
if(Input.GetKey(KeyCode.D))
{
transform.position += Vector3.right * speed * Time.deltaTime;
}
}
}
It should work perfectly fine.
It's good but very slow
when I press play my character falls through the floor
Add Rigid body
what does DeltaTiem stands for?
the time that takes to load another frame
It's good but very slow
you can change the speed
Alright, if anyone is still here, apply a rigidbody and collider to whatever object your player is, apply a script called "PlayerMovement", and copy this code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public float speed = 2f; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { if(Input.GetKey(KeyCode.W)) { transform.position += Vector3.forward * speed * Time.deltaTime; } if(Input.GetKey(KeyCode.S)) { transform.position += Vector3.back * speed * Time.deltaTime; } if(Input.GetKey(KeyCode.A)) { transform.position += Vector3.left * speed * Time.deltaTime; } if(Input.GetKey(KeyCode.D)) { transform.position += Vector3.right * speed * Time.deltaTime; } }
}
It should work perfectly fine.
it doesn't work,instead it does this
help.mp4
It's good but very slow
you can change the speed
HOWWW
Thanks it worked I just had to remove the Rigidbody so it doesn't fly all over the terrain
THANKS !!
It did, but it worked very slowly. I'll try another method. Thanks anyway! :)
making rigidbody a class in unity doesn't work for me. it gives me an error
the "vector3" in my visual studio it's not gteen, and it not works. please help!
hover on it and there will be a drop which is showing "using:UnityEngine" or something start with "using" tap on it but make sure you are using the lastest version of the unity.
@WotsitGamerYT
it says: (1,6): error CS0116: A namespace cannot directly contain members such as fields or methods