The rigidbody sphere moves in y axis automatically without adding force in that direction
So i am trying to recreate rolling sky (a mobile game) just as a test to see if i actually learnt anything and not for publishing, as im not getting any idea for my own game.
So now i created some tiles and created a sphere and addded a rigidbody to it and placed it one the tiles.
Then i wrote a code to change the rigidbody.velocity.z to a value of a variable which determines the speed
so now it moves forward continuously, the way it to but then there's a increase in rigidbody.velocity.y which makes it move upward and im not sure why dats happening
So i tried placing the ball in a extended cube instead of tiles and it move only forward (theres no increase in velocity.y)
But if place the ball in the tiles it moves upwards. Gravity brings it down but when the ball has to hit a tile and has to perform some function it just moves up and not touch the tile and completely skips the tile before coming back down automatically by gravity.
If i dont enable the gravity option it just keeps moving up with a fixed velocity which it acquired while it touches the first tile.
I have no clue wat is happening.
Thanks in advance if anyone has a solution to this,
so this is the script attached to the ball
using UnityEngine;
using System.Collections;
public class PlayerManager : MonoBehaviour {
public float ZSpeed;
public float XSpeed;
Rigidbody rigidbody;
public float JumpForce;
public Vector3 Velocity;
void Start ()
{
rigidbody = this.gameObject.GetComponent<Rigidbody> ();
}
void Update ()
{
ForwardMove ();
VerticalMove ();
Velocity = rigidbody.velocity;
}
public void ForwardMove ()
{
rigidbody.velocity = new Vector3 (rigidbody.velocity.x, rigidbody.velocity.y, ZSpeed);
}
public void VerticalMove ()
{
float XKey = Input.GetAxis ("Horizontal");
if (XKey != 0)
{
rigidbody.velocity = new Vector3 (XKey * XSpeed, rigidbody.velocity.y, rigidbody.velocity.z);
}
else
{
rigidbody.velocity = new Vector3 (0f, rigidbody.velocity.y, rigidbody.velocity.z);
}
}
public void OnTriggerEnter (Collider C)
{
if (C.transform.tag == "Destroy")
{
GameObject.Destroy (this.gameObject);
Debug.Log ("Destroy done");
}
if (C.transform.tag == "Jump")
{
rigidbody.AddForce (0f, JumpForce, 0f);
Debug.Log ("Jump Done");
}
}
}
At the beginning its like this
Then it starts flying
Your answer
Follow this Question
Related Questions
Moving rigid body objects inside another rigid body object (like a box) 1 Answer
How to make a cube rotate while moving? 1 Answer
Best way to pick up objects in unity? 2 Answers
First Person Controller flying upwards when moving mouse 0 Answers
how to make 3 instances with interchangeable position 0 Answers