- Home /
How do I make a ball (sphere) roll same speed, no matter what heights my orbit camera is looking at?
How do I make it roll same speed, no matter what heights my orbit camera is looking at?
I had this problem for 2 months now. I was testing new stuff, trying Cinamachine, but nothing worked, the only thing i know this is possible to achieve by editing code.
So, I am working on a game about a ball rolling through obstacles and collecting "bulbs" (inspired by roll-a-ball tutorial). I have made an orbit camera, which rotates along with mouse, and the ball is goes forward comparing by camera (that means camera forward is same as ball forward direction).
The problem is, when i look down (with orbit camera) and roll the ball, it rolls slower, as it should be "going down" same with looking up. The highest speed i can achieve is looking 90° Pitch, but i want the speed to be always same, no matter what pitch I am looking.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour {
public float speed;
public Text scoreText;
private int count = 0;
public Text wintText;
public GameObject exitPlatf;
public GameObject exitChain;
public float jumpHeight = 8.0f;
private bool isFalling = true;
private bool grounded = false;
public int lives = 5;
private Vector3 resetPos;
private bool isGrounded = false;
private Vector3 currentPos;
void Start()
{
resetPos.x = 0.0f;
resetPos.y = 20.0f;
resetPos.z = 0.0f;
}
void OnCollisionEnter(Collision hit)
{
if (hit.gameObject.CompareTag("ground"))
{
grounded = true;
}
else
{
grounded = true;
}
}
public void OnCollisionStay(Collision col)
{
isFalling = false;
}
public void OnCollisionExit()
{
isFalling = true;
}
void Update()
{
currentPos = new Vector3(transform.position.x, transform.position.y + 0.1f, transform.position.z);
if (Physics.Raycast(currentPos, Vector3.down, 1.4f))
{
isGrounded = true;
}
else
{
isGrounded = false;
}
if (Input.GetKeyDown(KeyCode.Space) && !isFalling && grounded && isGrounded == true)
{
GetComponent<Rigidbody>().AddForce(Vector3.up * jumpHeight, ForceMode.Impulse);
}
if (transform.position.y <= -50)
{
resetPlayer();
}
if (count >= 12 && exitPlatf.transform.position.y <= 19)
{
exitPlatf.transform.position = new Vector3(exitPlatf.transform.position.x, exitPlatf.transform.position.y + 0.05f, exitPlatf.transform.position.z);
transform.position = new Vector3(transform.position.x, transform.position.y + 0.05f, transform.position.z);
}
if (count >= 16 && exitChain.transform.position.y >= 9)
{
exitChain.transform.position = new Vector3(exitChain.transform.position.x, exitChain.transform.position.y - 0.1f, exitChain.transform.position.z);
}
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
movement = Camera.main.transform.TransformDirection(moveHorizontal, 0.0f, moveVertical);
Vector3 forcedmovement = new Vector3(movement.x * speed * Time.deltaTime, 0, movement.z * speed * Time.deltaTime);
forcedmovement.z = Mathf.Clamp(forcedmovement.z, -100, 100);
forcedmovement.x = Mathf.Clamp(forcedmovement.x, -100, 100);
GetComponent<Rigidbody>().AddForce(forcedmovement.x - GetComponent<Rigidbody>().velocity.x/1, 0 , forcedmovement.z - GetComponent<Rigidbody>().velocity.z/1);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "cp1")
{
resetPos = new Vector3 (0.0f, 21.0f, 60.0f);
}
if (other.gameObject.tag == "Enemy")
{
resetPlayer();
}
if (other.gameObject.tag == "PickUp")
{
other.gameObject.SetActive(false);
count += 1;
scoreText.text = "Score: " + count;
}
if (other.gameObject.tag == "portal")
{
wintText.gameObject.SetActive(true);
}
}
void resetPlayer()
{
transform.position = resetPos;//new Vector3(0.0f, 1.0f, 0.0f);
GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 0);
lives = lives - 1;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613032222im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Using WASD keys instead of mouse to roll ball? 1 Answer
How to select one character and play it's animation? 1 Answer
How can I create a sphere rotate in based on the direction it's moving? 1 Answer
Sphere + rigidbody + character controller 1 Answer
Why does this IF statement loop without finishing? 0 Answers