- Home /
Problem with rotation and groundchek _ 2D - C#
Hi everybody, I searched a lot but I didn't find a solution to my problems.
I have a sphere. I want that this sphere jumps after "x" seconds of a timer. I did it but I have 2 problems.
First problem: Trying to understand which is the relation from "second" to "distance" (i.e. after how many seconds the sphere covers 4x unit??) I did a few attempts:
rigidbody.velocity = new Vector3 (2,0,0);
The sphere rotates BUT the gravity doesn't work and the jump is not natural.
transform.position += Vector3.right speed Time.deltaTime;
The gravity and the jump are perfect BUT the sphere instead of rotating, crawls.
Second problem:
About the "groundcheck", I wrote this code:
public bool isJumping = false;
void FixedUpdate ()
{
//stuff
if(myCondition)
{
//rigidbody.velocity = new Vector3 (2,0,0);
//transform.position += Vector3.right * speed * Time.deltaTime;
if (myCondition && isJumping == false)
{
rigidbody.velocity = new Vector3 (0,5,0); //
isJumping = true;
}
}
}
void OnCollisionEnter(Collision col)
{
if(col.gameObject.tag == "Ground")
{
isJumping = false;
}
}
}
What happens is this:
IF #1: The sphere jumps and then, in the air, tries to jump again -> doesn't jump (perfect!)
IF #2: The sphere is falling, tries to jump -> jump . . . How can I fix this?
I already sat the tag "Ground" to my ground.
I hope you understand my problems (despite my english) and somebody are able to tell me the solutions.
thanks, bye.
Ins$$anonymous$$d of modyfing velocity directly, you might want to use rigidbody.AddForce or AddForceAtPoint. That will take care of your first problem.
If I use rigidbody.AddForce I can't know after how many seconds my sphere covers #x units. $$anonymous$$y "0" problem is that I need to know where my sphere, in each seconds, will be in the x axis.
With rigidbody.AddForce the speed is not constant . . .
Answer by Kiwasi · Sep 17, 2014 at 02:05 AM
Not sure exactly what your first problem is. Typically you create a jump by adding a force straight up. Unity takes care of the rest automatically.
For the second problem consider setting isJumping to true in OnCollisionExit, instead of as part of your jump.
Edit: Convention tends to be to use isGrounded instead of isJumping. isGrounded is a more accurate name for situations where the character is not actually jumping, but still cannot jump. Like falling or on a soft/liquid surface.
First problem is about the movement to right, not a jump. If I use the rigidbody.velocity the sphere go to the right doing a 360° movement on the floor (but the jump is not natural and the gravity doesn't work). If, ins$$anonymous$$d, I use transform.position the gravity works but the sphere goes to the right without rotation, as I check "freeze rotation".
For the second problem, I'm new in the use of unity and C# so if you have the "right" code I can correct (and understand) what you mean with "OnCollisionExit". PS: I need to set also as a part of the jump to avoid the "double jump"
Thx
Your answer
Follow this Question
Related Questions
Issue finding an angle with trigonometry 1 Answer
How to make the missile behave realistic by looking on the direction it is moving? 2 Answers
i want to get my to cube to rotate 90 degrees on the x-axis as it jumps 0 Answers
how do I script this jump animation? 1 Answer
Let Character Controller jump. 1 Answer