Search Unity

Can't get the code that should rotate a player and change the speed to work

Discussion in 'Editor & General Support' started by TomatoOrgyLT, Oct 18, 2013.

  1. TomatoOrgyLT

    TomatoOrgyLT

    Joined:
    Aug 10, 2012
    Posts:
    6
    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. Here is the video of my problem https://www.youtube.com/watch?v=1yjfxz1W17I, I find it difficult to explain this by writing :)

    The object1(player1) move script:

    Code (csharp):
    1. var object1 : Transform;
    2. var object2 : Transform;
    3.  
    4. var Xspeed = 10.0;
    5. var Yspeed = 10.0;
    6.  
    7. function Start() {
    8.  
    9. }
    10.  
    11. function FixedUpdate () {
    12. var x = Input.GetAxis("Horizontal") * Time.deltaTime * Xspeed;
    13. var y = Input.GetAxis("Vertical") * Time.deltaTime * Yspeed;
    14. transform.Translate(x, y, 0);
    15.  
    16. if(object1.transform.position.x > object2.transform.position.x){
    17.     transform.Rotate(new Vector3(0,180,0));
    18.     Xspeed = -10.0;
    19.     print("Rotating");
    20. }
    21. else{
    22.     transform.Rotate(new Vector3(0,0,0));
    23.     Xspeed = 10.0;
    24.     print("RotatingBack");
    25. }
    26. }
    The object2(player2) move script:

    Code (csharp):
    1. #pragma strict
    2.  
    3. var speed = 10.0;
    4.  
    5. function FixedUpdate () {
    6. var x = Input.GetAxis("Horizontal2") * Time.deltaTime * speed;
    7. var y = Input.GetAxis("Vertical2") * Time.deltaTime * speed;
    8. transform.Translate(x, y, 0);
    9. }
     
  2. mrDave777

    mrDave777

    Joined:
    Aug 10, 2013
    Posts:
    8
    It may have something to do with rotating it 180 degrees and back to 0, do you really need to do that? Do you just want player one to not go past player2? You could just try something like if (object1.position.x > object2.position.x) { object1.position.x = object2.position.x } or to keep a little further away... if (object1.position.x > object2.position.x-1) { object1.position.x = object2.position.x-1 }