- Home /
Changing speed and rotation of a rigidbody gets bugy
Hello everyone I have 2 rigidbodys with scripts attached. I made the first script to change the rotation of an object1 and the speed to negative(just because the controls would not go reverse) when the object1 X coordinates are bigger than object2 X coordinates. The problem is that when the object1 coordinates gets bigger than the object2, object1 gets stuck in that position and goes bugy. I don't understand why does that happen and I would really appreciate your help :)
The object1 move script:
var object1 : Transform;
var object2 : Transform;
var Xspeed = 10.0;
var Yspeed = 10.0;
function Start() {
}
function FixedUpdate () {
var x = Input.GetAxis("Horizontal") * Time.deltaTime * Xspeed;
var y = Input.GetAxis("Vertical") * Time.deltaTime * Yspeed;
transform.Translate(x, y, 0);
if(object1.transform.position.x > object2.transform.position.x){
transform.Rotate(new Vector3(0,180,0));
Xspeed = -10.0;
print("Rotating");
}
else{
transform.Rotate(new Vector3(0,0,0));
Xspeed = 10.0;
print("RotatingBack");
}
}
The object2 move script:
#pragma strict
var speed = 10.0;
function FixedUpdate () {
var x = Input.GetAxis("Horizontal2") * Time.deltaTime * speed;
var y = Input.GetAxis("Vertical2") * Time.deltaTime * speed;
transform.Translate(x, y, 0);
}
Answer by TomatoOrgyLT · Oct 18, 2013 at 08:48 PM
After hours of pain I finally did it :D All I had to do was to change the transform.Rotate(new Vector3(0,180,0));
into transform.rotation = Quaternion.Euler(0,180,0);
Anyway here is the final script:
private var fliped : boolean = false;
function Start() {
}
function FixedUpdate () {
var x = Input.GetAxis("Horizontal") * Time.deltaTime * Xspeed;
var y = Input.GetAxis("Vertical") * Time.deltaTime * Yspeed;
transform.Translate(x, y, 0);
if(object1.transform.position.x > object2.transform.position.x){
if(fliped == false){
flip();
}
}
if(object1.transform.position.x < object2.transform.position.x) {
if(fliped == true){
flip();
}
}
}
function flip()
{
if(fliped == false){
transform.rotation = Quaternion.Euler(0,180,0);
Xspeed = -10.0;
fliped = true;
print("Rotating");
}
else {
transform.rotation = Quaternion.Euler(0,0,0);
Xspeed = 10.0;
fliped = false;
print("RotatingBack");
}
}
Answer by whydoidoit · Oct 16, 2013 at 04:53 PM
You need to change the rigidbody.rotation not the transform.rotation in FixedUpdate:
rigidbody.MoveRotation(Quaternion.AngleAxis(180, Vector3.up) * rigidbody.rotation);
And in the second script you need to affect the rigidbody's position:
rigidbody.MovePosition(rigidbody.position + rigidbody.rotation * new Vector3(x,y,0));
I'm still getting the same result.
if(object1.transform.position.x > object2.transform.position.x){
rigidbody.$$anonymous$$oveRotation(Quaternion.AngleAxis(180, Vector3.up) * rigidbody.rotation);
Xspeed = -10.0;
print("Rotating");
}
else{
rigidbody.$$anonymous$$oveRotation(Quaternion.AngleAxis(0, Vector3.up) * rigidbody.rotation);
Xspeed = 10.0;
print("RotatingBack");
}
Second script
function FixedUpdate () {
var x = Input.GetAxis("Horizontal2") * Time.deltaTime * speed;
var y = Input.GetAxis("Vertical2") * Time.deltaTime * speed;
transform.Translate(x, y, 0);
rigidbody.$$anonymous$$ovePosition(rigidbody.position + rigidbody.rotation * new Vector3(x,y,0));
}
I forgot to mention that when it gets stuck it looks like the object is rotating in both directions and after some random time it randomly decides in what direction it should be facing to.
Your answer
Follow this Question
Related Questions
Why does my rigidbody slows down at time ? 1 Answer
Change movement speed based on rotation 0 Answers
How can I slow-stop the RigidBody Speed? 2 Answers
Rigidbody.AddForce with Raycast? 1 Answer