Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 aprendealer · Feb 16, 2015 at 11:45 AM · c#scripting problem

Movement script not working properly

Hi,

My friends and I are making a 3D game (on C#) which consists of a ball going through obstacles to get to the finish line. The ball is controlled by the tilting of the phone and if you tap the screen, you jump. I have the script which uses tilt and makes the ball move, and is working:

 using UnityEngine;
 using System.Collections;
  
 public class TiltControl : MonoBehaviour
  
 {
     public float speed = 10.0F;
     void Update()
     {
         Vector3 movement = new Vector3 (Input.acceleration.x, 0.0f, -Input.acceleration.z);
         rigidbody.velocity = movement * speed;
  
         if (Input.GetMouseButtonUp(0))
         {
             transform.Translate(Vector3.up * 100 * Time.deltaTime, Space.World);
         }
     }
  
     void OnTriggerEnter (Collider other)
     {
         if (other.gameObject.tag == "pickup")
         {
             other.gameObject.SetActive (false);
         }
     }
 }

And I also have the script which makes the ball jump once the touchscreen is tapped:

 using UnityEngine;
 using System.Collections;
  
 public class Jump : MonoBehaviour
 {
     public float JumpSpeed = 10;
     public float gravity = 9.8f;
     CharacterController controller;
     Vector3 currentMovement;
  
     void Start ()
     {
         controller = GetComponent<CharacterController> ();
     }
  
     // Update is called once per frame
     void Update ()
     {
         if (!controller.isGrounded)
             currentMovement -= new Vector3 (0, gravity * Time.deltaTime, 0);
  
         if (controller.isGrounded && Input.GetMouseButtonUp(0))
         {
             currentMovement.y = JumpSpeed;
         }
  
         controller.Move (currentMovement * Time.deltaTime);
  
     }
 }

But when I try to put them in one script:

 using UnityEngine;
 using System.Collections;
  
 public class TiltControl : MonoBehaviour
  
 {
     public float JumpSpeed = 10;
     public float gravity = 9.8f;
     public float speed = 10.0f;
     CharacterController controller;
     Vector3 currentMovement;
  
     void Start ()
     {
         controller = GetComponent<CharacterController> ();
     }
  
     void Update()
     {
        
         currentMovement = new Vector3 (Input.acceleration.x, -gravity * Time.deltaTime, -Input.acceleration.z);
  
         if (controller.isGrounded && Input.GetMouseButtonUp(0))
         {
             currentMovement.y = JumpSpeed;
         }
          
         controller.Move (currentMovement * Time.deltaTime);
          
     }
  
     void OnTriggerEnter (Collider other)
         {
             if (other.gameObject.tag == "pickup")
             {
                 other.gameObject.SetActive (false);
             }
         }
 }

It just doesnt work.

Can you please tell me what might be wrong?

Thank you.

Comment
Add comment · Show 6
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
avatar image Baste · Feb 16, 2015 at 12:08 PM 3
Share

There's no problem with having the two scripts as separate components. Attach both to your ball, and you're ready to go.

If you really want to combine them, you'll have to give a better description of what the issue is. "It doesn't work" isn't enough information. That could mean that your ball isn't moving. It could be that the game is creating portals to the Hellish Netherrealms and you're currently being eaten by Unimaginable Horrors.

avatar image aprendealer · Feb 16, 2015 at 12:21 PM 0
Share

First of all, you made my day with that answer :D

Now, I attach both the Jump and the Tiltcontrols scripts to the player and I press play and the ball just stands still and jumps when i tap the screen. However, if i disable the Jump script and press play, i can move the ball with tilt.

I don't really know what to do now since they work seperately, but not when they are both attached to the player and enabled.

avatar image Glurth · Feb 16, 2015 at 01:12 PM 0
Share

One issue that I see, though I'm not sure this is the cause of your problem, it does seem a bit odd: In your tilt script you modify the objects transform, and rigid body, but in the jump script, you are modifying the chracter-controller. $$anonymous$$gest you keep to a single method to implement movement. Also, suggest you use a method to $$anonymous$$ODIFY (+=) rather than SET(=) your movement (so you don't overwrite values assigned in other Update() functions) If using rigid bodies, take a look at the AddForce function, it's a great way to modify movement.

avatar image Baste · Feb 16, 2015 at 04:59 PM 0
Share

Yeah, you can probably ditch the charactercontroller and just add force to the ball ins$$anonymous$$d. If it's an actual ball with a ball collider, you might want to add angular force at the top of the ball, in the direction it has to rotate. That'll give you a simulated rolling.

Are you making a game where tilting the phone tilts the floor? In that case, actually tilting the floor and letting gravity do it's thing with the ball is probably easier. That's probably a bit slow, though - tilting the camera and changing the gravity of the ball (or everything) might be better.

avatar image aprendealer · Feb 16, 2015 at 07:22 PM 0
Share

I'm sorry, but I'm really new to this and the only way I could get the ball to jump and only jump again once it touched the ground was by using the character controller, so I have no idea of how I could use addforce in this scenario. But I will try, and thanks a lot for the help guys! :D

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ElijahShadbolt · Oct 09, 2016 at 07:24 AM

This script uses the Rigidbody's built-in AddForce feature to simulate the physics.

Since I don't have a mobile device set up, I used #if statements and keyboard control. Just remove the lines with # and Input.GetAxis.

 using UnityEngine;
 
 public class TiltControl : MonoBehaviour
 {
     public float moveForce = 5f;
     public float jumpImpulse = 5f;
 
     private bool isGrounded = false;
 
     void FixedUpdate()
     {
         Rigidbody rb = GetComponent<Rigidbody>();
         if (rb != null)
         {
             Vector3 force = new Vector3(moveForce, 0f, moveForce);
 #if UNITY_EDITOR
             force.x *= Input.GetAxis("Horizontal");
             force.z *= Input.GetAxis("Vertical");
 #else
             force.x *= Input.acceleration.x;
             force.z *= Input.acceleration.z;
 #endif
             rb.AddForce (force); // gradual change in velocity
 
             if (isGrounded && Input.GetMouseButtonUp(0))
             {
                 rb.AddForce(new Vector3(0f, jumpImpulse, 0f), ForceMode.Impulse); // instant change in velocity
             }
         }
     }
 
     void OnCollisionEnter(Collision data)
     {
         if (data.gameObject.tag == "ground")
         {
             isGrounded = true;
         }
     }
     void OnCollisionExit(Collision data)
     {
         if (data.gameObject.tag == "ground")
         {
             isGrounded = false;
         }
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.tag == "pickup")
         {
             other.gameObject.SetActive(false);
         }
     }
 }
Comment
Add comment · Share
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

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

6 People are following this question.

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

Related Questions

Limit number of face buttons being simultaneously held. 1 Answer

Generic Triggers and Actions 0 Answers

How do I serialize a class? 2 Answers

Shooting projectiles to the position of the mouse click? 0 Answers

Vuforia enabled scripting define 0 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