Unity Community


Results 1 to 17 of 17

  1. Location
    United States
    Posts
    547

    Sphere Movement and Rotation Problem

    I Have a movement script for the sphere
    Code:  
    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:  
    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:  
    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. Location
    Brisbane, Australia
    Posts
    712
    Quote Originally Posted by sherlockturtle View Post
    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?
    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. Location
    Denmark
    Posts
    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
    Im looking for projekt.
    Im a modeler and a coder.
    My models/Portfolio.
    Programming Portfolio
    My projekt.

    Contact:
    Skype Name: snotop
    email: snortop (snabelA) hotmail.com


  4. Location
    United States
    Posts
    547
    How can i change the way the sphere is facing and the camera?


  5. Location
    Tennessee, USA
    Posts
    3,468
    No no no...

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

    Code:  
    1. var cpos = Camera.main.transform.position;
    2. cpos.y=transform.y;
    3. var direction = (transform.position - cpos).normalized;
    4. var forward = direction * Input.GetAxis("Vertical");
    5. var right = Vector3.Cross(Vector3.up, direction) * Vector3(Input.GetAxis("Horizontal");

    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.
    Unity 101 - Programming in General
    Unity 202 - Know your objects
    Unity 303 - Object Relations and Components
    Unity 404 - Advanced Classes
    Unity 505 - Networking (Bringing it all together)

    If you don't know the stuff where you think you are, go back and learn the other stuff first.


  6. Location
    United States
    Posts
    547
    Wait what part of the movment script to i put that?
    Last edited by sherlockturtle; 01-23-2012 at 03:48 PM.


  7. Posts
    644
    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. Location
    United States
    Posts
    547
    I just don't know which part of the script to put the script BigMister gave me.

    Edit: Spell Check
    Last edited by sherlockturtle; 01-23-2012 at 06:19 PM.


  9. Location
    United States
    Posts
    547
    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. Location
    Tennessee, USA
    Posts
    3,468
    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:  
    1. /// This script moves the character controller forward
    2. /// and sideways based on the arrow keys.
    3. /// It also jumps when pressing space.
    4. /// Make sure to attach a character controller to the same game object.
    5. /// It is recommended that you make only one call to Move or SimpleMove per frame.    
    6.  
    7. var speed : float = 6.0;
    8. var jumpSpeed : float = 8.0;
    9. var gravity : float = 20.0;
    10.  
    11.  
    12. function Update() {
    13.     if (controller.isGrounded) {
    14.         // We are grounded, so recalculate
    15.         // move direction directly from axes
    16.        
    17.         var cpos = Camera.main.transform.position;
    18.         cpos.y=transform.y;
    19.         var direction = (transform.position - cpos).normalized;
    20.         var forward = direction * Input.GetAxis("Vertical");
    21.         var right = Vector3.Cross(Vector3.up, direction) * Input.GetAxis("Horizontal");
    22.         var moveDirection = (forward + right).normalized;
    23.        
    24.         moveDirection *= speed;
    25.        
    26.         if (Input.GetButton ("Jump")) {
    27.             moveDirection.y = jumpSpeed;
    28.         }
    29.     }
    30.  
    31.     // Apply gravity
    32.    
    33.     // Move the controller
    34. }

    Quote Originally Posted by Wolfos View Post
    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.
    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 by BigMisterB; 01-27-2012 at 08:26 PM. Reason: Edited Code
    Unity 101 - Programming in General
    Unity 202 - Know your objects
    Unity 303 - Object Relations and Components
    Unity 404 - Advanced Classes
    Unity 505 - Networking (Bringing it all together)

    If you don't know the stuff where you think you are, go back and learn the other stuff first.


  11. Location
    United States
    Posts
    547
    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 by sherlockturtle; 01-24-2012 at 07:59 PM.


  12. Location
    Denmark
    Posts
    188
    it because it missing a ) at the end!

    Code:  
    1. var right = Vector3.Cross(Vector3.up, direction) * Vector3(Input.GetAxis("Horizontal"));

    to end the Vector3
    Im looking for projekt.
    Im a modeler and a coder.
    My models/Portfolio.
    Programming Portfolio
    My projekt.

    Contact:
    Skype Name: snotop
    email: snortop (snabelA) hotmail.com


  13. Location
    United States
    Posts
    547
    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. Location
    United States
    Posts
    547


  15. 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. Location
    Tennessee, USA
    Posts
    3,468
    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.
    Unity 101 - Programming in General
    Unity 202 - Know your objects
    Unity 303 - Object Relations and Components
    Unity 404 - Advanced Classes
    Unity 505 - Networking (Bringing it all together)

    If you don't know the stuff where you think you are, go back and learn the other stuff first.


  17. Location
    United States
    Posts
    547
    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •