- Home /
[C#] JUMP - What i must add to this code to jump? :>
Please about help :/ I'm new in Unity and C## :/
Here my code:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public float moveSpeed;
// Use this for initialization
void Start () {
//Player spawn point
//This is where our player star when the game is played.
//player == game object. Game object == transform!
transform.position = new Vector3 (-5, -1, 0);
}
// Update is called once per frame
void Update () {
//Player to move left, righ
//player (gameobject) aka transform to move when i press the wsad
if(Input.GetKey(KeyCode.D))
{
transform.Translate(new Vector3(moveSpeed, 0, 0) * Time.deltaTime) ;
}
if(Input.GetKey(KeyCode.A))
{
transform.Translate(new Vector3(-moveSpeed, 0, 0) * Time.deltaTime) ;
}
}
}
Screen with player inspector panel
Screen with ground inspector panel
Comment
Answer by spiceboy9994 · Mar 02, 2015 at 04:03 PM
How about adding force to the rigid body? I'm defining the jumpForce as local, but this may be a global class variable. Placed within the code just for reference. Also the value may require to be tunned. Remove the is kinematic on your rigid body, otherwise you won't be able to apply force to it.
void Update () {
//Player to move left, righ
//player (gameobject) aka transform to move when i press the wsad
float playerJumpForce = 10.0f; //Define as global
if(Input.GetKey(KeyCode.D))
{
transform.Translate(new Vector3(moveSpeed, 0, 0) * Time.deltaTime) ;
}
if(Input.GetKey(KeyCode.A))
{
transform.Translate(new Vector3(-moveSpeed, 0, 0) * Time.deltaTime) ;
}
if (Input.GetKey(KeyCode.Space))
{
rigidbody.AddForce(Vector3.up * playerJumpForce);
}
}
}
Hope this helps
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
I Want My Player To Be Sent Flying In The Air When He Get's Hit By An Enemy 1 Answer
Jump Function C# hassle! 0 Answers
Jump Issue 0 Answers