- Home /
how can i rotate the ball in left direction deirection if it was in forward direction and vice versa , like a real ball?
if (Input.GetMouseButtonDown(0) && !isDead)
{
isPlaying = true;
score++;
scoreText.text = score.ToString();
if (dir==Vector3.forward)
{
dir = Vector3.left;
}
else
{
dir = Vector3.forward;
}
}
float amountToMove = speed * Time.deltaTime;
transform.Translate(dir * amountToMove);
}
Question: Do you want your ball to essentially look like it is "rolling" ins$$anonymous$$d of just moving? I didn't quite understand form the title.
@General-Troll yes i want it to look like rolling while moving
transform.Rotate(new Vector3(dir[2], 0, dir[0]) Time.deltaTime speed);
Answer by Rajeesh_AR · Jul 26, 2016 at 11:18 AM
You could use this.
void Update ()
{
transform.Rotate(Vector3.forward * Time.deltaTime*50);
}
You can change Vector3.forward to Vector3.left, Vector3.down etc. Also to avoid calling this code all the time (since it is in update), you could use a boolean
bool canRevolve = false;
void Update ()
{
if(canRevolve)
transform.Rotate(Vector3.forward * Time.deltaTime*50);
}
Activate and Deactivate boolean necessarily. Hope this will help. Happy Coding..
$$anonymous$$y ball is my player , and it is only attach with ball
So... With the code which I provided, it will rotates only the player(ball).
And if whole platform is rotating, means the code is in some other script or in the camera also.
What didn't you understand? Please be more specific.
I have to make player a child of camera or camera to be child of player??
Answer by General-Troll · Jul 27, 2016 at 05:07 PM
Rajeesh is right, but that will constantly make it move in just one direction while the boolean is true. If moving to the right makes the boolean true, than it will still rotate forward instead of right. The solution to this is binding rotation AND movement to an axis. For example:
var speed : float = 5;
function Update () {
var forwardMovement = Input.GetAxis ("Vertical") * speed;
var horizontalMovement = Input.GetAxis ("Horizontal") * speed * Time.deltaTime;
transform.Rotate(forwardMovement, 0, horizontalMovement);
transform.Translate(horizontalMovement, 0, forwardMovement);
}
I'm not positive, but due to the nature of rigidbodies and AddRelativeForce/AddForce, you might be able to invoke both movement and rotation using either of those, since force affects both rotation and position instead of just position. But I'm not 100% sure it will work.
Example code for this might be:
var speed: float = 5;
function Update () {
var forwardMovement = Input.GetAxis ("Vertical") * speed;
var horizontalMovement = Input.GetAxis ("Horizontal") * speed;
rigidbody.AddRelativeForce (horizontalMovement, 0, forwardMovement);
}
I can't test (and won't be able to for a while) any of these, so if someone can test out both ideas for me, that would be great.
Hope this helps!
Which doesn't do anything. If it's the second one, make sure you have a rigidbody attached.
Create a new Javascript script and copy-paste the first line group of code into it, and attach it to the ball and see if it works. If it does, than I can convert it to c# to work with your current script.
Your answer
Follow this Question
Related Questions
Why is force only being added in the same direction? 1 Answer
Rigidbody.AddTorque/AddForce Question. 1 Answer
re-orienting velocity 1 Answer
Slerp object in direction of traveling 1 Answer
AddForce to sphere 1 Answer