Sphere Movement and Rotation Problem

Discussion in 'Scripting' started by sherlockturtle, Jan 23, 2012.

  1. sherlockturtle

    sherlockturtle

    Member

    Joined:
    Jan 23, 2012
    Messages:
    578
    I Have a movement script for the sphere
    Code (csharp):
    1. /// This script moves the character controller forward
    2.  
    3. /// and sideways based on the arrow keys.
    4.  
    5. /// It also jumps when pressing space.
    6.  
    7. /// Make sure to attach a character controller to the same game object.
    8.  
    9. /// It is recommended that you make only one call to Move or SimpleMove per frame.    
    10.  
    11.  
    12.  
    13. var speed : float = 6.0;
    14.  
    15. var jumpSpeed : float = 8.0;
    16.  
    17. var gravity : float = 20.0;
    18.  
    19.  
    20.  
    21.  
    22.  
    23.  
    24. function Update() {
    25.  
    26.  
    27.     if (controller.isGrounded) {
    28.  
    29.         // We are grounded, so recalculate
    30.  
    31.         // move direction directly from axes
    32.  
    33.         moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
    34.  
    35.                                 Input.GetAxis("Vertical"));
    36.  
    37.  
    38.         moveDirection *= speed;
    39.  
    40.        
    41.  
    42.         if (Input.GetButton ("Jump")) {
    43.  
    44.             moveDirection.y = jumpSpeed;
    45.  
    46.         }
    47.  
    48.     }
    49.  
    50.  
    51.  
    52.     // Apply gravity
    53.  
    54.  
    55.    
    56.  
    57.     // Move the controller
    58.  
    59.  
    60. }
    and i have the rotation script
    Code (csharp):
    1.  
    2. var distance = 5.0;
    3.  
    4. var xSpeed = 125.0;
    5.  
    6. var ySpeed = 50.0;
    7.  
    8.  
    9.  
    10. private var x = 0.0;
    11.  
    12. private var y = 0.0;
    13.  
    14.  
    15.  
    16. @script AddComponentMenu("Camera-Control/Mouse Orbit")
    17.  
    18.  
    19.  
    20. function Start () {
    21.  
    22.     var angles = transform.eulerAngles;
    23.  
    24.     x = angles.y;
    25.  
    26.     y = angles.x;
    27.  
    28.    
    29.  
    30. }
    31.  
    32.  
    33.  
    34. function LateUpdate () {
    35.  
    36.     if (target) {
    37.  
    38.         x += Input.GetAxis("Mouse X") * xSpeed * distance* 0.02;
    39.  
    40.         y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
    41.  
    42.         var rotation = Quaternion.Euler(y, x, 0);
    43.  
    44.         var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
    45.  
    46.         transform.rotation = rotation;
    47.  
    48.         transform.position = position;
    49.  
    50.     }
    51.  
    52.  
    53.  
    54. }
    Camera follows sphere
    Code (csharp):
    1.  
    2. var smooth : float = 5;
    3.  
    4. function Update () {
    5.  
    6.     transform.position.x = Mathf.Lerp(transform.position.x,target.position.x,Time.deltaTime*smooth);
    7.  
    8.     transform.position.y = Mathf.Lerp(transform.position.y,target.position.y,Time.deltaTime*smooth);
    9.  
    10.  
    11.  
    12. }
    but the problem is if i rotate all around moving forward is backward. It essentialy is that forward is one direction always, and not the direction the camera is in. Is there a way to fix it?
  2. Cameron.

    Cameron.

    Member

    Joined:
    Jun 1, 2009
    Messages:
    714
    I don't understand, that should do as you want. Are you sure you're not getting confused about which way the "sphere" is facing when you rotate the camera? Rotating the camera will not rotate the sphere. The sphere will always move "forward" based on the way it's transform is orientated in the game world. Put a texture on the sphere so you can see which way it's facing.
  3. snortop

    snortop

    New Member

    Joined:
    Dec 16, 2011
    Messages:
    188
    just remove the camera script and have the rotation script on camara whit the target set to player.

    The rotation script is for the camera to rotate around the Target/player

    and if you want the character to move where the camara is facing you should look into the 3D character following in standard script
  4. sherlockturtle

    sherlockturtle

    Member

    Joined:
    Jan 23, 2012
    Messages:
    578
    How can i change the way the sphere is facing and the camera?
  5. bigmisterb

    bigmisterb

    Member

    Joined:
    Nov 6, 2010
    Messages:
    3,676
    No no no...

    if you press forward on the keyboard, your move vector is...

    Code (csharp):
    1.  
    2. var cpos = Camera.main.transform.position;
    3. cpos.y=transform.y;
    4. var direction = (transform.position - cpos).normalized;
    5. var forward = direction * Input.GetAxis("Vertical");
    6. var right = Vector3.Cross(Vector3.up, direction) * Vector3(Input.GetAxis("Horizontal");
    7.  
    So now the direction or forward movement is now based on the direction of your camera but based at the level of your object, so if you look down on the ball, it still thinks forward is up on the camera. ;)

    Using this method means that no matter what rotation the sphere is at, forward will always move away from the person's view.
  6. sherlockturtle

    sherlockturtle

    Member

    Joined:
    Jan 23, 2012
    Messages:
    578
    Wait what part of the movment script to i put that?
    Last edited: Jan 23, 2012
  7. Wolfos

    Wolfos

    Member

    Joined:
    Mar 17, 2011
    Messages:
    646
    What is it with people who appear to speak only through Google translate here? We don't know what you mean, you can't learn to program without learning English first.
  8. sherlockturtle

    sherlockturtle

    Member

    Joined:
    Jan 23, 2012
    Messages:
    578
    I just don't know which part of the script to put the script BigMister gave me.

    Edit: Spell Check
    Last edited: Jan 24, 2012
  9. sherlockturtle

    sherlockturtle

    Member

    Joined:
    Jan 23, 2012
    Messages:
    578
    I aded this to it

    transform.rotation.y = Camera.main.transform.rotation.y;

    And it works but if i do a full 360 it reverts back to how it worked before.
  10. bigmisterb

    bigmisterb

    Member

    Joined:
    Nov 6, 2010
    Messages:
    3,676
    Sorry, I thought it was a bit more obvious. It replaces the part of your script that gathers user input and converts it to direction.

    In concept, it takes the forward direction of the camera if it were at the plane of the object and gathers two pieces of information. What it's forward vector is and what it's side vector is. It then adds them and normalizes them. (normalizing prevents you from moving faster if you hold both the forward and side keys down at the same time, since you are basically moving 1,0,1 that would be greater than 0,0,1)

    Here is your code correctly inserted in red.

    Code (csharp):
    1.  
    2. /// This script moves the character controller forward
    3. /// and sideways based on the arrow keys.
    4. /// It also jumps when pressing space.
    5. /// Make sure to attach a character controller to the same game object.
    6. /// It is recommended that you make only one call to Move or SimpleMove per frame.    
    7.  
    8. var speed : float = 6.0;
    9. var jumpSpeed : float = 8.0;
    10. var gravity : float = 20.0;
    11.  
    12.  
    13. function Update() {
    14.     if (controller.isGrounded) {
    15.         // We are grounded, so recalculate
    16.         // move direction directly from axes
    17.        
    18.         var cpos = Camera.main.transform.position;
    19.         cpos.y=transform.y;
    20.         var direction = (transform.position - cpos).normalized;
    21.         var forward = direction * Input.GetAxis("Vertical");
    22.         var right = Vector3.Cross(Vector3.up, direction) * Input.GetAxis("Horizontal");
    23.         var moveDirection = (forward + right).normalized;
    24.        
    25.         moveDirection *= speed;
    26.        
    27.         if (Input.GetButton ("Jump")) {
    28.             moveDirection.y = jumpSpeed;
    29.         }
    30.     }
    31.  
    32.     // Apply gravity
    33.    
    34.     // Move the controller
    35. }
    36.  
    Seriously, you expect everyone from every country to learn English before asking questions about programming? Please be a little more understanding that there is more than one language on this planet.
    Last edited: Jan 28, 2012
  11. sherlockturtle

    sherlockturtle

    Member

    Joined:
    Jan 23, 2012
    Messages:
    578
    Thanks BigMister
    Edit it says this Assets/movement.js(23,96): UCE0001: ';' expected. Insert a semicolon at the end. At line 23 but there one at the end.

    Edit: and this on line 23

    Assets/movement.js(23,95): BCE0044: expecting ), found ';'.
    Last edited: Jan 25, 2012
  12. snortop

    snortop

    New Member

    Joined:
    Dec 16, 2011
    Messages:
    188
  13. sherlockturtle

    sherlockturtle

    Member

    Joined:
    Jan 23, 2012
    Messages:
    578
    now it says

    Assets/movement.js(23,67): BCE0024: The type 'UnityEngine.Vector3' does not have a visible constructor that matches the argument list '(float)'.
  14. sherlockturtle

    sherlockturtle

    Member

    Joined:
    Jan 23, 2012
    Messages:
    578
  15. elveatles

    elveatles

    New Member

    Joined:
    May 2, 2009
    Messages:
    147
    Vector3(Input.GetAxis("Horizontal")) is like writing Vector3(0.2f) or Vector3(-1.0f). You can't create a Vector3 with one float.
  16. bigmisterb

    bigmisterb

    Member

    Joined:
    Nov 6, 2010
    Messages:
    3,676
    Edited the post above to clear the errors. One must learn how to read code and edit it. If you have a question about something, especially something on the forum in code, click on the code you have a question about. They made it where all that stuff links to the reference... aint that nice. ;)
  17. sherlockturtle

    sherlockturtle

    Member

    Joined:
    Jan 23, 2012
    Messages:
    578
    I had to take this code out
    cpos.y=transform.y;
    and it works but the jump only gets me about an inch of the ground?