- Home /
This question was
closed Sep 30, 2015 at 01:36 PM by
meat5000 for the following reason:
No Question
Question by
shabazahmed6 · Sep 30, 2015 at 12:46 PM ·
unity 5ui
Im just using the basic code to move the player added Clamp values and my player automatically going towards top left corner im not using any gravity
using UnityEngine; using System.Collections;
[System.Serializable] public class Boundary { public float xMin, xMax, zMin, zMax;
}
public class PlayerMovement : MonoBehaviour { public float speed; public float tilt; public Boundary boundary; private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody> ();
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.velocity = movement * speed;
Vector3 tempPos = rb.position;
tempPos.x = Mathf.Clamp(rb.position.x, boundary.xMin, boundary.xMax);
tempPos.y = 0.0f;
tempPos.z = Mathf.Clamp (rb.position.z, boundary.zMin, boundary.zMax);
rb.position = tempPos;
rb.rotation = Quaternion.Euler (0.0f, 0.0f, rb.velocity.x * -tilt);
}
}
Comment
Follow this Question
Related Questions
Detect if UI Image is in a trigger 1 Answer
How to shrink text size 1 Answer
Check if visible in UI Mask 2 Answers