Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to make the ball to roll based on camera's position

Discussion in 'Scripting' started by Karen_M_2005, Jan 7, 2019.

  1. Karen_M_2005

    Karen_M_2005

    Joined:
    Sep 27, 2018
    Posts:
    12
    Hello, I tried everything to synchronize the ball and the camera. Because using the Unity official Rollerball script the ball moves in the same axis so the the ball don't match the camera. Please help with that!

    Thanks!
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    You clearly didn't try everything because everything includes a solution that works. :p

    Instead of moving the ball based on the absolute world axis (Vector3.up, for example), move the ball based on the camera axis (camera.transform.forward, for example).
     
  3. Karen_M_2005

    Karen_M_2005

    Joined:
    Sep 27, 2018
    Posts:
    12
    how could I combine these and the official script to make the ball still roll instead of just moving and I'm in 8th grade (just additional info)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour {
    5.  
    6.     public float speed;
    7.  
    8.     private Rigidbody rb;
    9.  
    10.     void Start ()
    11.     {
    12.         rb = GetComponent<Rigidbody>();
    13.     }
    14.  
    15.     void FixedUpdate ()
    16.     {
    17.         float moveHorizontal = Input.GetAxis ("Horizontal");
    18.         float moveVertical = Input.GetAxis ("Vertical");
    19.  
    20.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    21.  
    22.         rb.AddForce (movement * speed);
    23.     }
    24. }