- Home /
Simple Movment
i am new to unity, how can i create a simple movement for an object that don't sink to other objects while moving?
i've add Rigidbody and BoxCollider to them all.
i wrote this and attached them to the object
var s = 5;
function Update () {
if(Input.GetKey ("up")){
transform.Translate(0,0,s);
}
if(Input.GetKey ("down")){
transform.Translate(0,0,-s);
}}
The problem is that When the player collisions another object, it sinks into the object a little and if I hold the arrow key down, the player sinks into and comes out from the object regularly.
It leads the camera which is behind the player, to go back and forth regularly. How do I solve this problem?
What language are you using? What type of object is it? What are you trying to do?
Answer by danilonishimura · Dec 13, 2012 at 06:09 PM
Hi Seyyed.
I suppose you're trying to make a simple adventure game. First, check if your object has a collider (BoxCollider, MeshCollider, whatever). Then, add a RigidBody to your object (menu: Physics > Rigidbody). Then attach a script to your object who will access the Rigidbody and add forces accordingly to your needs. You may also freeze each axis rotation and/or position on the Rigidbody component (ie: you'll want to freeze the z position if you're trying to make a 2D adventure game).
thank you danilonishimura but i knew that! (i said "i am new" not "much new!" :) ) now tell me about the script. what i have to write?!
i wrote this and attached them to the object
var s = 5; function Update () { if(Input.Get$$anonymous$$ey ("up")){ transform.Translate(0,0,s); } if(Input.Get$$anonymous$$ey ("down")){ transform.Translate(0,0,-s); }}
i have add Rigidbody and BoxCollider to them all. The problem is that When the player collisions another object, it sinks into the object a little and if I hold the arrow key down, the player sinks into and comes out from the object regularly. It leads the camera which is behind the player, to go back and forth regularly. How do I solve this problem?
thanks!
Answer by GooseNinja · Dec 13, 2012 at 08:18 PM
Have you put constraint on rotation in the rigidbody component?
Try changing the variable to
var s = 5.0;
And also try changing your code to
if(Input.GetKey ("up")){
transform.Translate(Vector3(0,0,s) * Time.deltaTime);
}
if(Input.GetKey ("down")){
transform.Translate(Vector3(0,0,-s) * Time.deltaTime);
}
yeah, i freezed x,y,z of rotation in Rigidbody
I've tested your code but still not working and the player sink a few in other objects
i have an idea:
before the "up" code, check there is any object in front or not, if there isn't anything, let the player to moving forward
any solution?
Your answer
![](https://koobas.hobune.stream/wayback/20220613085356im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotation and movement? 1 Answer
Making a bubble level (not a game but work tool) 1 Answer
Third Person Controller Rotation 0 Answers
Movement in mid air 3 Answers
Moving character....northwest...? 1 Answer