- Home /
Ancient and duplicate
How do I make my object not fly away?
I'm making a tron game and if my bike gets bumped to much it starts flying away. Its like if you were on the moon and you get pushed you fly away. Can anyone help me fix this? Also here is the script that makes my bike constantly go forward, I don't know if it has to do with it.
var speed:int = 10;
function Update()
{
transform.Translate(Vector3(0,0,speed) * Time.deltaTime);
}
A solution for this problem is to use a CharacterController ins$$anonymous$$d of a Rigidbody to interact with the environment.
This is what worked for me: First I Freeze the Rigidbodys Y position and Y rotation. Then in the attached script:
public float speed = 10;
public int direction = -1; // depends what direction you want you z axis to go
private void Awake(){
rb = GetComponent<Rigidbody>();
}
private void FixedUpdate(){
rb.AddForce(0, 0, direction * speed * Time.deltaTime , Force$$anonymous$$ode.Impulse);
}
Answer by SvenMokveldje · Jan 23, 2018 at 06:15 PM
For the Fly away part you cloud try to make the rigidbody mass higher. This makes sure that there need to be more force to push them away.
Answer by LTonon · Jan 23, 2018 at 08:17 PM
I may be wrong, but I guess that using the transform.Translate() method moves the Game Object without checking for collisions on the next position before actually moving, and then if the bike moves inside another object and it detects a collision, it's going to be pushed away. Try using the velocity attribute of the RigidBody to move the bike, something like this:
var rb : RigidBody;
function Awake()
{
rb = GetComponent.<RigidBody>();
}
function Update()
{
rb.velocity = new Vector3(0f, 0f, speed * Time.deltaTime);
}
Follow this Question
Related Questions
Ramp for stairs causes player to slide down. 1 Answer
How to enable gravity on gameobject when interact by player 3 Answers
Adding a force(gravity) to a planet. 0 Answers
Why can't I move along the Y axis when I shift gravity from the Y to Z axis? 1 Answer
Character teleports to the apex of full jump height 0 Answers