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

Sphere rolling forward

Discussion in 'Editor & General Support' started by Jana1108, Apr 27, 2016.

  1. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    Hi, how can I make a sphere gameobject move forwards while it looks like it's rolling (rotating y axis) in C#?
     
  2. greg-harding

    greg-harding

    Joined:
    Apr 11, 2013
    Posts:
    519
  3. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    Last edited: Apr 27, 2016
  4. greg-harding

    greg-harding

    Joined:
    Apr 11, 2013
    Posts:
    519
    Pretty much everything you need is in the thread and the project - just figure out how it works and adapt it as you need. The project I sent you literally rolls the sphere around x and rotates around y while it moves... take a look at how it works again.
     
  5. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    I tried new vector 3 and increasing the z position by 1 in void update but that made it go super fast
     
  6. greg-harding

    greg-harding

    Joined:
    Apr 11, 2013
    Posts:
    519
    Editing your question after I reply doesn't help my answers look right! :)

    The general ideas about how you can move and rotate objects based on input are in there, with a basic nested structure of gameobjects to make everything pretty clear. You can unparent the camera if needed. You should be able to learn what's going on and change it to solve what you're asking. Otherwise, you're just asking for other people to write your scripts for you without learning much.
     
  7. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,998
  8. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    I already have unparented the camera and I've written a script that functions but not nicely. So it works except the player movement is glitchy. I added an animation to the X axis of the ball so it looks like it's actually rolling, then I wrote a script for the camera to make it follow the player how I want it to. Now I added this code player.transform.Translate(new Vector3(player.transform.position.x,player.transform.position.y, (player.transform.position.z + 1)) * Time.deltaTime/speed); to the player instead of this player.transform.Translate(Vector3.forward * Time.deltaTime * speed); because I figured that if the ball is rotating it won't always be moving forwards, right? Now all I want to do it make the balls z position increase smoothly using a certain speed. The code I have now works but not very efficiently. I don't even know what I'm looking at in this other project.
     
  9. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    Yes thanks! That is exactly what I needed! I am just having one issue, my ball is moving in the opposite direction to what it should be. The rotation is fine, just the movement isn't. I tried changing a few variables but not really sure which one I should change?
     
  10. greg-harding

    greg-harding

    Joined:
    Apr 11, 2013
    Posts:
    519
    The code I wrote comments every single step to explain what it does. I'm not sure how to help further without writing the exact script you need for you.

    The rig accepts input and moves a ball in x/z while rolling. No, the two are not fully connected as we were discussing one example of how something might be rigged up. ie. the ball doesn't roll the correct amount that it has moved in x/y (this was left as something you could add afterwards once you understood the mechanic). In this case you need to convert the distance moved in x/z to an angle to rotate the ball by. @mgear's post sounds like it has a line of code you can use - just make sure you understand it and you should be able to apply it easily enough.
     
  11. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    I think I've got it thanks