# Sphere Movement and Rotation Problem

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

1. ### 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.
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.
39.
40.
41.         if (Input.GetButton ("Jump")) {
42.
43.             moveDirection.y = jumpSpeed;
44.
45.         }
46.
47.     }
48.
49.
50.
51.     // Apply gravity
52.
53.
54.
55.
56.     // Move the controller
57.
58.
59. }
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.
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. ### Cameron860

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

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

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

5. ### bigmisterb

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

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

Last edited: Jan 23, 2012
7. ### Wolfos

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

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

Joined:
Jan 23, 2012
Posts:
592

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

Joined:
Nov 6, 2010
Posts:
4,167
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.
26.         if (Input.GetButton ("Jump")) {
27.             moveDirection.y = jumpSpeed;
28.         }
29.     }
30.
31.     // Apply gravity
32.
33.     // Move the controller
34. }
35.
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

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

Joined:
Dec 16, 2011
Posts:
194
13. ### 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)'.

Joined:
Jan 23, 2012
Posts:
592
anyone?

15. ### 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

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

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?