• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sprogobalionas · Apr 13, 2021 at 12:11 PM · movementcamera-movementsphereroll a ballpitch

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 t$$anonymous$$s problem for 2 months now. I was testing new stuff, trying Cinamac$$anonymous$$ne, but not$$anonymous$$ng worked, the only t$$anonymous$$ng i know t$$anonymous$$s is possible to ac$$anonymous$$eve 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, w$$anonymous$$ch 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 $$anonymous$$ghest speed i can ac$$anonymous$$eve 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 $$anonymous$$t)
 {
     if ($$anonymous$$t.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;

 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

177 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using WASD keys instead of mouse to roll ball? 1 Answer

Making character rotate to where camera is facing 1 Answer

Sphere move only on ground 0 Answers

Sphere + rigidbody + character controller 1 Answer

How can I cause a sphere to bounce forward infinitely with the same distance 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges