duck

Unity C# Rigidbody Character Control

Oct 1st, 2012
2,102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.89 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Player : MonoBehaviour {
  5.  
  6.     public int turnSpeed;
  7.     public int moveSpeed;
  8.     public int jumpForce;
  9.    
  10.     bool onGround;
  11.    
  12.     // Use this for initialization
  13.     void Start () {
  14.    
  15.     }
  16.    
  17.     // Update is called once per frame
  18.     void FixedUpdate () {
  19.    
  20.         float h = Input.GetAxis("Horizontal");
  21.         float v = Input.GetAxis("Vertical");
  22.        
  23.         transform.Rotate( 0, h * turnSpeed * Time.deltaTime, 0 );
  24.         Vector3 moveAmount = transform.forward * v * moveSpeed;
  25.         rigidbody.MovePosition( transform.position + moveAmount * Time.deltaTime );
  26.         rigidbody.velocity = moveAmount + Vector3.Scale(rigidbody.velocity, new Vector3(0,1,0));   
  27.        
  28.         if (onGround && Input.GetKey(KeyCode.Space))
  29.         {
  30.             rigidbody.AddForce( transform.up * jumpForce, ForceMode.Impulse ); 
  31.             onGround = false;
  32.         }
  33.     }
  34.    
  35.     void OnCollisionEnter() {
  36.    
  37.         onGround = true;
  38.        
  39.     }
  40.    
  41. }
Advertisement
Add Comment
Please, Sign In to add comment