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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Align Sphere and Blobshadow to Ground?

Discussion in 'Scripting' started by Der_Kevin, Aug 19, 2015.

  1. Der_Kevin

    Der_Kevin

    Joined:
    Jan 2, 2013
    Posts:
    517
    Hi! I am currently working on a tilt/maze Game. the platform movement code comes from this post:http://answers.unity3d.com/questions/1029970/tilting-platform-question.html

    and it looks currently like this:


    as you can see, i have 2 problems:
    • the blob shadow is not moving with the platform and disappears sometimes under the platform
    • and the Ball/Sphere also does not stick to the Ground and sometimes flies in the air

    the blob shadow script looks like this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class BallFollow : MonoBehaviour {
    4.    
    5.      Transform bar;
    6.    
    7.      void Start() {
    8.          bar = GameObject.Find("Ball").transform;
    9.      }
    10.    
    11.      void Update() {
    12.          transform.position = new Vector3(bar.position.x, transform.position.y, bar.position.z);
    13.      }
    14. }
    15.  
    so it just copies the position of the ball, but somehow has also needs to copy the rotation of the platform?

    and then, the ball movement script looks like this:

    Code (CSharp):
    1. sing UnityEngine;
    2. using System.Collections;
    3. namespace qtools.qmaze.example
    4. {
    5.      public class QBallController : MonoBehaviour
    6.      {
    7.          public float moveScaleX = 1.0f;
    8.          public float moveScaleY = 1.0f;
    9.          public float moveMaxSpeed = 1.0f;
    10.          public float moveLerp = 0.9f;
    11.        
    12.          private Rigidbody rigidBody;
    13.        
    14.          void Start ()
    15.          {
    16.              rigidBody = GetComponent<Rigidbody>();
    17.          }
    18.        
    19.          void Update ()
    20.          {
    21.              Vector3 velocity = (Vector3.forward   * Input.GetAxis("Horizontal") * moveScaleX);
    22.              velocity+= (Vector3.left * Input.GetAxis("Vertical")   * moveScaleY);
    23.              velocity = Vector3.ClampMagnitude(velocity, moveMaxSpeed);
    24.              velocity *= moveLerp;
    25.              rigidBody.velocity = velocity;  
    26.          }
    27.      }
    28. }
    29.  
    normally it would be enough to give the ball a rigidbody and control the ball by tilting the platform he is lying on.
    but, i used this ball script because if i wouldn't , the ball would move to slowly over the platform. so i am controlling the platform AND the Ball. but i need a way to let the ball stick to the platform. the rigidbody settings of the ball are: mass:1 - drag:0 - angular Drag: 0.7 and the physic settings are untouched.

    Thanks for your help upfront!
     
  2. gcoope

    gcoope

    Joined:
    Dec 20, 2012
    Posts:
    11
    Sorry if I missed something, but couldn't you use the in-built lighting system - a directional light for example - to create the shadow on the ball?
     
  3. Der_Kevin

    Der_Kevin

    Joined:
    Jan 2, 2013
    Posts:
    517
    the scene will be a little bit more complex* in the end, so the performance would be pretty bad for mobile if i would use realtime shadows

    *like this:
     
    Last edited: Aug 19, 2015
  4. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Cast a ray from the ball down to the platform, and use the hit's distance to position the blob shadow on the Y axis.

    Also, might I recommend renaming the "bar" variable to "ball".