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

Can someone please help me with this problem! :O

Discussion in 'Physics' started by Epic7Ate9, Apr 30, 2020.

  1. Epic7Ate9

    Epic7Ate9

    Joined:
    Apr 30, 2020
    Posts:
    7
    Hey im a Unity and C# beginner, today I started the development of my first ever unity game.

    At first you might be like "What problem? I don't see it!"
    well, it's not a REAL problem but it's just not what i want but since im a beginner i need a little help on this as i don't know much about Unity and C# yet, so basically I have a rocket in the game it's a 3d game and theres a rocket right, i set up controls (WAD) with .AddForce() W to fly, and A and D to go to the sides, the problem is W works perfectly fine but whenever I do A or D it just tilts the rocket to the side and makes it drop, instead, i want it to just shift left and right not tilt left and right. going up is exactly how i want it but i wanna move the rocket left and right so it's not just like "-tilts- AHHHHHH- (rocket falls to the floor and camera thats attached shakes violently and i can't get up again)"

    I can't send it for some reason because I don't know how to but yea hope you understand it also heres the code I used it's force based

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FlyingScript : MonoBehaviour
    6. {
    7.     public Rigidbody rb;
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.      
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void FixedUpdate()
    16.     {
    17.         if (Input.GetKey("w"))
    18.         {
    19.             rb.AddForce(0, 40, 0 * Time.deltaTime);
    20.         }
    21.  
    22.         if (Input.GetKey("a"))
    23.         {
    24.             rb.AddForce(-4, 0, 0 * Time.deltaTime);
    25.         }
    26.  
    27.         if (Input.GetKey("d"))
    28.         {
    29.             rb.AddForce(4, 0, 0 * Time.deltaTime);
    30.         }
    31. }
    32.  
     
    Last edited: Apr 30, 2020
  2. Epic7Ate9

    Epic7Ate9

    Joined:
    Apr 30, 2020
    Posts:
    7
    The code is pretty simple but I think a good solution would be like making the W to fly addforce physics based but then maybe have A and D controlled in another way? Is that even possible? If it is, can someone help me with it.
     
  3. Epic7Ate9

    Epic7Ate9

    Joined:
    Apr 30, 2020
    Posts:
    7
    Ok i found the solution i have borders around the area so the rocket cant go free roaming it has to stay in a certain area and i removed the deltatime so it's all good now