- Home /
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.
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.
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.
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.
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.
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
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);
}
}
}
Your answer
Follow this Question
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