- Home /
Rotate and move the sphere while the up key is pressed
I'm kinda new to unity scripting and i can't manage to make my ball rotate and in the same time moving while the up button is pressed. My code attached to the sphere looks like this but unfortunately is not rotating the ball:
//Moving around and jump private var moveDirection : Vector3 = Vector3.zero; var speed : float = 6.0; var jumpSpeed : float = 8.0; var gravity : float = 20.0; //Rotate the ball while moving private var thisObj : Transform; var rotSpeed = 10.0;
function Start() { // Cachhe the transform link` thisObj = this.transform; } function Update() { var controller : CharacterController = GetComponent(CharacterController); if (controller.isGrounded){ moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection); moveDirection *= speed; } if (Input.GetButton ("Jump")) { moveDirection.y = jumpSpeed; }
// Apply gravity moveDirection.y -= gravity Time.deltaTime; // Move the controller controller.Move(moveDirection Time.deltaTime); transform.LookAt(target);}
function LateUpdate() { if (Input.GetButton("up")) { thisObj.Rotate(0, Time.deltaTime*rotSpeed, 0, Space.World); } }
Answer by AngryOldMan · Mar 05, 2011 at 02:42 PM
stick these variables in (adjust as necessary)
var TurnSpeed : float = 50;
put this under the starndard update function under if controller is grounded.(change the axis after eulerAngles if it's not the right rotation)
transform.eulerAngles.x += Input.GetButton("up")*(Time.deltaTime*TurnSpeed)
This may sound noobish, but the compiler give me an error which i wasn't able to solve it.
Operator '*' cannot be used with a left hand side of type 'boolean' and a right hand side of type 'float'.
what line specifically? can u type it please, cant be sure that me counting your post will still be accurate.
At the solution provided by you.
Input.GetButton("up")*(Time.deltaTime*TurnSpeed)
ah yeah my bad. Input.GetAxis is what u need not Input.GetButton.
Well, this looks like this solve's the problem, but after a while the ball stops from movement and rotation too. Some kind of loop will solve the issue?
Your answer
![](https://koobas.hobune.stream/wayback/20220613111448im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Making a sphere move and rotate like a real ball on ground 2 Answers
Rotate multiple objects around or rather move them on the surface of another object 1 Answer
Rotating Player 0 Answers
rotation snap, or any kinda snap 3 Answers
Why am I getting 2 different results with RotateAround? Trying to move cubes on surface of sphere. 0 Answers