Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 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;

 }

}

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.

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

Character triple+ Jumping: Please Help! 0 Answers

Moving smoothly around the bounds of a circle 1 Answer

say i wanted to have my character travel around a spherical plane, how would i accomplish this task? 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 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