topical media & game development
lib-unity-tutorial-m2h-2-Assets-Game-2-Jump-game-Scripts-Playermovement.cs / cs
using UnityEngine;
using System.Collections;
public class Playermovement : MonoBehaviour
{
public float movementSpeed = 5.0f;
private bool isGrounded = false;
void Update() {
rigidbody.velocity = new Vector3(0, rigidbody.velocity.y, 0); //Set X and Z velocity to 0
transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime * movementSpeed, 0, 0);
/*if (Input.GetButtonDown("Jump") && isGrounded)
{
Jump(); //Manual jumping
}*/
}
void Jump()
{
if (!isGrounded) { return; }
isGrounded = false;
rigidbody.velocity = new Vector3(0, 0, 0);
rigidbody.AddForce(new Vector3(0, 700, 0), ForceMode.Force);
}
void FixedUpdate()
{
isGrounded = Physics.Raycast(transform.position, -Vector3.up, 1.0f);
if (isGrounded)
{
Jump(); //Automatic jumping
}
}
}
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.