Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Sphere Movement and Rotation Problem

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

  1. sherlockturtle

    sherlockturtle

    Joined:
    Jan 23, 2012
    Posts:
    592
    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. private var moveDirection : Vector3 = Vector3.zero;
    22.  
    23.  
    24.  
    25. function Update() {
    26.  
    27.     var controller : CharacterController = GetComponent(CharacterController);
    28.  
    29.     if (controller.isGrounded) {
    30.  
    31.         // We are grounded, so recalculate
    32.  
    33.         // move direction directly from axes
    34.  
    35.         moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
    36.  
    37.                                 Input.GetAxis("Vertical"));
    38.  
    39.         moveDirection = transform.TransformDirection(moveDirection);
    40.  
    41.         moveDirection *= speed;
    42.  
    43.        
    44.  
    45.         if (Input.GetButton ("Jump")) {
    46.  
    47.             moveDirection.y = jumpSpeed;
    48.  
    49.         }
    50.  
    51.     }
    52.  
    53.  
    54.  
    55.     // Apply gravity
    56.  
    57.     moveDirection.y -= gravity * Time.deltaTime;
    58.  
    59.    
    60.  
    61.     // Move the controller
    62.  
    63.     controller.Move(moveDirection * Time.deltaTime);
    64.  
    65. }
    and i have the rotation script
    Code (csharp):
    1. var target : Transform;
    2.  
    3. var distance = 5.0;
    4.  
    5. var xSpeed = 125.0;
    6.  
    7. var ySpeed = 50.0;
    8.  
    9.  
    10.  
    11. private var x = 0.0;
    12.  
    13. private var y = 0.0;
    14.  
    15.  
    16.  
    17. @script AddComponentMenu("Camera-Control/Mouse Orbit")
    18.  
    19.  
    20.  
    21. function Start () {
    22.  
    23.     var angles = transform.eulerAngles;
    24.  
    25.     x = angles.y;
    26.  
    27.     y = angles.x;
    28.  
    29.    
    30.  
    31. }
    32.  
    33.  
    34.  
    35. function LateUpdate () {
    36.  
    37.     if (target) {
    38.  
    39.         x += Input.GetAxis("Mouse X") * xSpeed * distance* 0.02;
    40.  
    41.         y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
    42.  
    43.         var rotation = Quaternion.Euler(y, x, 0);
    44.  
    45.         var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
    46.  
    47.         transform.rotation = rotation;
    48.  
    49.         transform.position = position;
    50.  
    51.     }
    52.  
    53.  
    54.  
    55. }
    Camera follows sphere
    Code (csharp):
    1. var target : Transform;
    2.  
    3. var smooth : float = 5;
    4.  
    5. function Update () {
    6.  
    7.     transform.position.x = Mathf.Lerp(transform.position.x,target.position.x,Time.deltaTime*smooth);
    8.  
    9.     transform.position.y = Mathf.Lerp(transform.position.y,target.position.y,Time.deltaTime*smooth);
    10.  
    11.  
    12.  
    13. }
    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_SM

    Cameron_SM

    Joined:
    Jun 1, 2009
    Posts:
    915
    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

    Joined:
    Dec 16, 2011
    Posts:
    194
    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

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

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    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. var moveDirection = (forward + right).normalized;
    8.  
    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

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

    Wolfos

    Joined:
    Mar 17, 2011
    Posts:
    949
    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

    Joined:
    Jan 23, 2012
    Posts:
    592
    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

    Joined:
    Jan 23, 2012
    Posts:
    592
    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

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    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. private var moveDirection : Vector3 = Vector3.zero;
    13.  
    14. function Update() {
    15.     var controller : CharacterController = GetComponent(CharacterController);
    16.     if (controller.isGrounded) {
    17.         // We are grounded, so recalculate
    18.         // move direction directly from axes
    19.        
    20.         var cpos = Camera.main.transform.position;
    21.         cpos.y=transform.y;
    22.         var direction = (transform.position - cpos).normalized;
    23.         var forward = direction * Input.GetAxis("Vertical");
    24.         var right = Vector3.Cross(Vector3.up, direction) * Input.GetAxis("Horizontal");
    25.         var moveDirection = (forward + right).normalized;
    26.        
    27.         moveDirection *= speed;
    28.        
    29.         if (Input.GetButton ("Jump")) {
    30.             moveDirection.y = jumpSpeed;
    31.         }
    32.     }
    33.  
    34.     // Apply gravity
    35.     moveDirection.y -= gravity * Time.deltaTime;
    36.    
    37.     // Move the controller
    38.     controller.Move(moveDirection * Time.deltaTime);
    39. }
    40.  
    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

    Joined:
    Jan 23, 2012
    Posts:
    592
    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

    Joined:
    Dec 16, 2011
    Posts:
    194
    it because it missing a ) at the end!

    Code (csharp):
    1.  
    2.  var right = Vector3.Cross(Vector3.up, direction) * Vector3(Input.GetAxis("Horizontal"));
    3.  
    to end the Vector3
     
  13. sherlockturtle

    sherlockturtle

    Joined:
    Jan 23, 2012
    Posts:
    592
    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

    Joined:
    Jan 23, 2012
    Posts:
    592
  15. elveatles

    elveatles

    Joined:
    May 2, 2009
    Posts:
    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

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    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

    Joined:
    Jan 23, 2012
    Posts:
    592
    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?