- Home /
Question by
CrazyDictator · Oct 08, 2016 at 07:33 AM ·
scripting problemjumpmoveplatform
Player Controls
I wrote a script and attached it to a cube, expecting the cube to move when the arrow keys or wasd keys are pressed, and jump when the space bar is pressed, I have no erros in my console, but the cube wont work. This is my script:
using UnityEngine; using System.Collections;
public class PlyerControl : MonoBehaviour {
Rigidbody rb;
public float Speed = 10f;
public float jumpSpeed = 10f;
bool OnGround;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
OnGround = transform.position.y == 0.5f;
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 move = new Vector3(h, 0f, v) * Speed;
Vector3 movemt = move + transform.position;
rb.AddForce(movemt);
if(Input.GetKeyDown("space") && OnGround)
{
Vector3 up = Vector3.up * jumpSpeed;
Vector3 upMove = up + transform.position;
rb.AddForce(upMove);
}
}
}
Comment
Take look on character controller prefab from standart assets it ready to use so no need to write script from scratch.
I just wanna know what is wrong with my script.
Your answer
Follow this Question
Related Questions
Jumping in 3D 1 Answer
Get current platform 1 Answer
Move an object from A to B to C etc.? 2 Answers
controller.Move not working correctly 0 Answers
Whats wrong with this script? 1 Answer