Question by
TechnoWings · Jul 03, 2016 at 11:22 PM ·
c#script.
I have this movement script that works just fine, but how do i integrate jumping and running?
using UnityEngine; using System.Collections;
public class CaracterController : MonoBehaviour {
public float speed = 10.0f;
// Use this for initialization
void Start ()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update () {
float translation = Input.GetAxis("Vertical") * speed;
float straffe = Input.GetAxis("Horizontal") * speed;
translation *= Time.deltaTime;
straffe *= Time.deltaTime;
transform.Translate(straffe, 0, translation);
if (Input.GetKeyDown("escape"))
Cursor.lockState = CursorLockMode.None;
}
}
Comment
UPDATE
I have found a solution to this issue and just needed to fiddle with the code.
FIX:
using UnityEngine; using System.Collections;
public class CaracterController : $$anonymous$$onoBehaviour {
public float speed = 10.0f;
bool onGround = true;
public CursorLock$$anonymous$$ode wanted$$anonymous$$ode;
// Use this for initialization
void Start ()
{
Cursor.lockState = CursorLock$$anonymous$$ode.Locked;
wanted$$anonymous$$ode = CursorLock$$anonymous$$ode.Locked;
}
// Update is called once per frame
void Update() {
float translation = Input.GetAxis("Vertical") * speed;
float straffe = Input.GetAxis("Horizontal") * speed;
translation *= Time.deltaTime;
straffe *= Time.deltaTime;
transform.Translate(straffe, 0, translation);
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Escape))
{
Cursor.lockState = CursorLock$$anonymous$$ode.None;
wanted$$anonymous$$ode = CursorLock$$anonymous$$ode.None;
}
RaycastHit hit;
Vector3 physicsCentre = this.transform.position +
this.GetComponent<CapsuleCollider>().center;
Debug.DrawRay(physicsCentre, Vector3.down*3f,Color.red,1);
if (Physics.Raycast(physicsCentre, Vector3.down, out hit, 3f))
{
onGround = true;
}
else
{
onGround = false;
}
Debug.Log(onGround);
if (Input.Get$$anonymous$$eyDown("space") && onGround)
{
this.GetComponent<Rigidbody>().AddForce(Vector3.up * 300);
}
}
}