- Home /
2D Movement Within a constraint (C#)
Hi I'm making a ShMUp game My player is moved using transform.position and has a box collider trigger. What are some ways I can keep the player within the constraints(In this case, in the camera's view)?
I have tried many things - adding another box collider on the player and a box collider wall (Either killed the player or he flew straight through it)
I would Appreciate All Help! Thanks
Try moving the player with a rigid body ins$$anonymous$$d of setting the position of the transform.
Answer by lloladin · Sep 25, 2015 at 06:09 AM
the box colliders you tryed aint working bc you are using transform.position which just moves the player instantly ignoring all collisions if you want youre player to collide with walls then moving try using Rigidbody.velocity like MikeNewall said
Example
GetComponet<RigidBody2D>().velocity = new Vector 2(x,y);
you can also use getaxis which i would recomend since getaxis is controller supported
Example 2
float move = Input.GetAxisRaw("Horizontal");
movement = new Vector2(move * moveSpeed, myRigidbody2d.velocity.y);
myRigidbody2d.velocity = movement;
heres some documentation http://docs.unity3d.com/ScriptReference/Input.GetAxis.html
Answer by ZalmakiZ · Sep 25, 2015 at 08:09 PM
If you want to stick with Transform.position = ... Then you Can use worldToScreenPoint(newPosition) and check if the screen point is still in view, if its outside then readjust.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
How to make a tick speed for heart health system?,How to make a tick speed for health script? 1 Answer
How to Position the object perfectly with the Screen Bounds 0 Answers