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. Dismiss Notice

Movement Script not working properly

Discussion in 'General Discussion' started by aprendealer, Feb 15, 2015.

Thread Status:
Not open for further replies.
  1. aprendealer

    aprendealer

    Joined:
    Feb 15, 2015
    Posts:
    1
    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:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TiltControl : MonoBehaviour
    5.  
    6. {
    7.     public float speed = 10.0F;
    8.     void Update()
    9.     {
    10.         Vector3 movement = new Vector3 (Input.acceleration.x, 0.0f, -Input.acceleration.z);
    11.         rigidbody.velocity = movement * speed;
    12.  
    13.         if (Input.GetMouseButtonUp(0))
    14.         {
    15.             transform.Translate(Vector3.up * 100 * Time.deltaTime, Space.World);
    16.         }
    17.     }
    18.  
    19.     void OnTriggerEnter (Collider other)
    20.     {
    21.         if (other.gameObject.tag == "pickup")
    22.         {
    23.             other.gameObject.SetActive (false);
    24.         }
    25.     }
    26. }
    And I also have the script which makes the ball jump once the touchscreen is tapped:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Jump : MonoBehaviour
    5. {
    6.     public float JumpSpeed = 10;
    7.     public float gravity = 9.8f;
    8.     CharacterController controller;
    9.     Vector3 currentMovement;
    10.  
    11.     void Start ()
    12.     {
    13.         controller = GetComponent<CharacterController> ();
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update ()
    18.     {
    19.         if (!controller.isGrounded)
    20.             currentMovement -= new Vector3 (0, gravity * Time.deltaTime, 0);
    21.  
    22.         if (controller.isGrounded && Input.GetMouseButtonUp(0))
    23.         {
    24.             currentMovement.y = JumpSpeed;
    25.         }
    26.  
    27.         controller.Move (currentMovement * Time.deltaTime);
    28.  
    29.     }
    30. }
    But when I try to put them in one script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TiltControl : MonoBehaviour
    5.  
    6. {
    7.     public float JumpSpeed = 10;
    8.     public float gravity = 9.8f;
    9.     public float speed = 10.0f;
    10.     CharacterController controller;
    11.     Vector3 currentMovement;
    12.  
    13.     void Start ()
    14.     {
    15.         controller = GetComponent<CharacterController> ();
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.        
    21.         currentMovement = new Vector3 (Input.acceleration.x, -gravity * Time.deltaTime, -Input.acceleration.z);
    22.  
    23.         if (controller.isGrounded && Input.GetMouseButtonUp(0))
    24.         {
    25.             currentMovement.y = JumpSpeed;
    26.         }
    27.          
    28.         controller.Move (currentMovement * Time.deltaTime);
    29.          
    30.     }
    31.  
    32.     void OnTriggerEnter (Collider other)
    33.         {
    34.             if (other.gameObject.tag == "pickup")
    35.             {
    36.                 other.gameObject.SetActive (false);
    37.             }
    38.         }
    39. }
    It just doesnt work.

    Can you please tell me what might be wrong?

    Thank you.
     
    Last edited: Feb 15, 2015
  2. unity_8234C97CC10AB9140436

    unity_8234C97CC10AB9140436

    Joined:
    Sep 11, 2022
    Posts:
    2
    I have A Big Problem Please Help Me
    I have a Screen Shot of my CSharp Script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     // Movement Variable
    8.     public float speed = 10f;
    9.     public float strafeSpeed = 5f;
    10.     //Jump Variable
    11.     public float jumpForce = 10f;
    12.     //Cam Rotation Variable
    13.    
    14.     //Main Variable
    15.     Rigidbody rb;
    16.     public bool isGrounded;
    17.     public GameObject theOtherThing;
    18.    
    19.     public void Start()
    20.     {
    21.         rb = GetComponent<Rigidbody>();
    22.         theOtherThing.SetActive(false);
    23.     }
    24.  
    25.     void FixedUpdate()
    26.     {
    27.         // For MoveMents And Jump
    28.        
    29.         float movX = Input.GetAxisRaw("Horizontal");
    30.         float movZ = Input.GetAxisRaw("Vertical");
    31.         float Jump = Input.GetAxisRaw("Jump");
    32.  
    33.         Vector3 movePlayer = new Vector3(movX, 0, movZ);
    34.         Vector3 jumpPlayer = new Vector3(0, Jump, 0);
    35.  
    36.         rb.MovePosition(transform.position + movePlayer * Time.deltaTime * speed);
    37.         rb.MovePosition(transform.position + jumpPlayer * Time.deltaTime * speed);
    38.  
    39.        
    40.  
    41.        
    42.        
    43.     }
    44.  
    45.    
    46.  
    47.    
    Help Me Please

     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,417
    Don't necro 7 year old threads with your unrelated problems. Simply create your own thread. Also, you must put some effort into describing the problem if you need help. Devs cannot read your mind on what is wrong; code doesn't describe the problems you're having.

    Finally, I would ask that you post scripting questions on the Scripting forum please.

    Thanks.
     
Thread Status:
Not open for further replies.