- Home /
Create bounded movement
Hello, to everyone. I am new in Unity and I am trying to create a simple game in which the player will fire bullets in the direction of the mouse pointer. My problem is that I can not set bounds for the movement of my player, I checked unity manual for bounds but to be honest I couldn't understand. I have put an if condition in which if the x becomes equal to 90 then the object should be placed at x=89. But it doesn't work. Can you please give a help or hint with this??
Here is my code in C#
**using UnityEngine; using System.Collections; public class player : MonoBehaviour { public float speed = 10.0F; public float rotationSpeed = 100.0F; Vector3 pos = new Vector3(0,0,0);
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float translation = Input.GetAxis("Vertical") * speed;
float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate(0, 0, translation);
transform.Rotate(0, rotation, 0);
pos = transform.position ;
if(f.GetMouseButtonDown(0)) {
Plane zeroPlane = new Plane( Vector3.up, Vector3.zero );
Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
float distance;
if (zeroPlane.Raycast(ray, out distance))
{
Vector3 outputPosition = ray.origin + ray.direction * distance;
GameObject go1 = (GameObject)Instantiate(GameObject.Find("bullet"), pos, Quaternion.identity);
go1.transform.LookAt(outputPosition);
go1.rigidbody.AddForce( outputPosition * 10, ForceMode.Impulse);
}
}
if (pos.x ==90)
{
transform.Translate(89,y,z);
}
}**
Answer by kimon · Mar 03, 2013 at 10:06 PM
I found the solution
Just put this line of code
transform.position = new Vector3(Mathf.Clamp(transform.position.x, -90.0F, 90.0F), 0, Mathf.Clamp(transform.position.z, -36.0F, 36.0F));
I apologize for the inconvenience
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Character Rotation 2 Answers
Multiple Cars not working 1 Answer
C# Twitchy Camera Movement 1 Answer
A node in a childnode? 1 Answer