- Home /
3d platform character moves through objects
Need Unity help. Working on greylevel 3D street. Have some cubes using as placeholders for automobiles. My player can walk through them if I hold a "move" key. When I let go, player moves outside of automobile. What am I doing wrong?
Answer by IrthosKepesk · Feb 09 at 01:29 AM
So I updated my code to
if ((Input.GetKey(KeyCode.UpArrow)) || (Input.GetKey(KeyCode.W))) { Vector3 GLC = transform.TransformDirection(Vector3.forward); if (!(Physics.Raycast(transform.position, GLC, .5f))) { transform.position += Vector3.forward Time.deltaTime Speed; } }
this has fixed the situation at the moment. basically had to use a raycast for all 4 directions. thanks for all the help!! @Ronelo
Answer by Ronelo · Feb 06 at 04:55 PM
probably you moving player without physics like transform.position and change it every frame ,if you replace it to move using velocity then it will calculate physics stuff and will collide. using Rigidbody.AddForce(new Vector3()) or Rigidbody.velocity = new Vector3().
I have Rigidbody on the player character but not on the "inanimate objects". here is a piece of the code I use to control movement.
if ((Input.GetKey(KeyCode.UpArrow)) || (Input.GetKey(KeyCode.W))) { transform.position += Vector3.forward Time.deltaTime Speed; }
So am i scripting something wrong or do I have rigidbody wrong somewhere?
It’s okey but it will not collide because transform.position work like teleportation. So you firstly set Rigidbody2d then replace transform.position to rigidbody2d.velocity. Example Rigidbody2d playerRigidbody;
Void update() {
Vector3 inputDiraction = Input.GetAxisRaw(“Forward”) , playerRigidbody.velocity.y,Input.GetAxisRaw(“Horizontal”).normalized * Time.deltaTime * playerSpeed;
playerRigidbody.velocity = inputDiraction;
}
Your answer
Follow this Question
Related Questions
Why Do I Suddenly Have This Problem? 0 Answers
Find amount of vertical movement of object 0 Answers
Terrain to object 1 Answer
Converting Actionscript to C# 1 Answer
Accelerometer calibration 2 Answers