Advertisement
Guest User

MovingTest.cs

a guest
Mar 30th, 2014
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. [RequireComponent (typeof(Rigidbody))]
  5. public class MovingTest : MonoBehaviour {
  6.  
  7.     [SerializeField] float moveSpeed = 10f;
  8.  
  9.     [SerializeField] bool a = true;
  10.  
  11.     Transform trans;
  12.  
  13.     Rigidbody rig;
  14.  
  15.     void Awake()
  16.     {
  17.         trans = transform;
  18.         rig = rigidbody;
  19.     }
  20.  
  21.     void Update()
  22.     {
  23.         float moveHorizontal = Input.GetAxis("Horizontal");
  24.         float moveVertical = Input.GetAxis("Vertical");
  25.         rig.velocity = new Vector3(moveHorizontal * moveSpeed, 0, moveVertical * moveSpeed);
  26.  
  27.         if (a)
  28.         {
  29.             rig.position = new Vector3
  30.                 (
  31.                     Mathf.Clamp(rig.position.x, -1, 1),
  32.                     0.0f,
  33.                     Mathf.Clamp(rig.position.z, 0, 10)
  34.                 );
  35.         }
  36.         else
  37.         {
  38.             trans.localPosition = new Vector3
  39.                 (
  40.                     Mathf.Clamp(trans.localPosition.x, -1, 1),
  41.                     0.0f,
  42.                     Mathf.Clamp(trans.localPosition.z, 0, 10)
  43.                 );
  44.         }
  45.     }
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement