Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Moving Object on position x axis

Discussion in 'Scripting' started by Shadowing, Apr 17, 2015.

  1. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,647
    I don't know what I'm doing wrong here.
    Its possible the frame work I'm using is preventing me from doing this. Haven't been able to find anything though.

    I'm trying this on FixedUpdate and it does move it right accept the camera is all jaggy. As if something else is trying to make it go back to original position as I'm trying to make it go right.


    Code (csharp):
    1.  transform.position += new Vector3 (3, 0, 0);
    Should this allow me to move an object smoothly?
     
    Enamati likes this.
  2. Stef_Morojna

    Stef_Morojna

    Joined:
    Apr 15, 2015
    Posts:
    289
    Fixed update is not on evry frame, instead it happens a fixed amount of times / second.
    So that means the camera will not move evry frame.

    So I would use: void update();

    Also because void update(); happens evry frame instead of a fixed time you will have to use Time.deltatime.
    Time.deltatime is the amount of time that passes from the last frame.

    Code (CSharp):
    1. void update(); {
    2. transform.position=new Vector3(3 * time.deltatime , 0, 0);
    3. }
     
    Last edited: Apr 21, 2015
  3. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
  4. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,647
    Thanks for the replies guys.

    I don't know why I said it was using Fixed Update. It is using Update.

    Im able to rotate smoothly with no issues.

    I tried the following and the issue still exists. So now im back to thinking something in the frame work is trying to prevent me from doing this. Ive disabled all the code I can find that moves the ship though and it still does it.

    Code (csharp):
    1.  
    2. //transform.position += new Vector3(90 * Time.deltaTime , 0, 0);
    3. transform.Translate(30 * Time.deltaTime, 0, 0);
    4.  



    Does anyone see anything on this page that I'm over looking
    I commented out almost everything

    Code (csharp):
    1.  
    2. /// <summary>
    3. /// Flight system. This script is Core plane system
    4. /// </summary>
    5. using UnityEngine;
    6. using System.Collections;
    7. // included all necessary component
    8. [RequireComponent(typeof(Rigidbody))]
    9. [RequireComponent(typeof(Collider))]
    10. [RequireComponent(typeof(DamageManager))]
    11. [RequireComponent(typeof(WeaponController))]
    12.  
    13. public class FlightSystem : MonoBehaviour
    14. {
    15.  
    16.  
    17. public float Speed = 50.0f;// Speed
    18. public float SpeedMax = 60.0f;// Max speed
    19. public float RotationSpeed = 50.0f;// Turn Speed
    20. public float SpeedPitch = 2;// rotation X
    21. public float SpeedRoll = 3;// rotation Z
    22. public float SpeedYaw = 1;// rotation Y
    23. public float DampingTarget = 10.0f;// rotation speed to facing to a target
    24. public bool AutoPilot = false;// if True this plane will follow a target automatically
    25. private float MoveSpeed = 10;// normal move speed
    26.  
    27.  
    28. [HideInInspector]
    29. public bool SimpleControl = false;// set true is enabled casual controling
    30. [HideInInspector]
    31. public bool FollowTarget = false;
    32. [HideInInspector]
    33. public Vector3 PositionTarget = Vector3.zero;// current target position
    34. [HideInInspector]
    35. public DamageManager DamageManage;
    36. [HideInInspector]
    37. public WeaponController WeaponControl;// weapon system
    38. private Vector3 positionTarget = Vector3.zero;
    39. private Quaternion mainRot = Quaternion.identity;
    40. [HideInInspector]
    41. public float roll = 0;
    42. [HideInInspector]
    43. public float pitch = 0;
    44. [HideInInspector]
    45. public float yaw = 0;
    46. public Vector2 LimitAxisControl = new Vector2 (2, 1);// limited of axis rotation magnitude
    47. public bool FixedX;
    48. public bool FixedY;
    49. public bool FixedZ;
    50. public float Mess = 30;
    51. public bool DirectVelocity = true;// if true this riggidbody will not receive effect by other force.
    52. public float DampingVelocity = 5;
    53. private float StraffRotateSpeed = 150.0f;  // Degrees per second;
    54. public Vector3 euler = Vector3.zero;
    55. public float zLimit = 80.0f;
    56. private string StraffSwitch;
    57. private string StraffDirection;
    58. private float StraffBack = 0.0f;
    59. Vector3 velocityTarget;
    60. void Start ()
    61. {
    62. // define all component
    63. DamageManage = this.gameObject.GetComponent<DamageManager> ();
    64. WeaponControl = this.gameObject.GetComponent<WeaponController> ();
    65. mainRot = this.transform.rotation;
    66. GetComponent<Rigidbody>().mass = Mess;
    67. transform.eulerAngles = euler;
    68. StraffSwitch = "off";
    69. }
    70. /*/
    71. void FixedUpdate ()
    72. {
    73. if (!this.GetComponent<Rigidbody>())
    74. return;
    75. Quaternion AddRot = Quaternion.identity;
    76. velocityTarget = Vector3.zero;
    77. if (AutoPilot) {// if auto pilot
    78. if (FollowTarget) {
    79. // rotation facing to the positionTarget
    80. positionTarget = Vector3.Lerp (positionTarget, PositionTarget, Time.fixedDeltaTime * DampingTarget);
    81. Vector3 relativePoint = this.transform.InverseTransformPoint (positionTarget).normalized;
    82. mainRot = Quaternion.LookRotation (positionTarget - this.transform.position);
    83. GetComponent<Rigidbody>().rotation = Quaternion.Lerp (GetComponent<Rigidbody>().rotation, mainRot, Time.fixedDeltaTime * (RotationSpeed * 0.01f));
    84. this.GetComponent<Rigidbody>().rotation *= Quaternion.Euler (-relativePoint.y * 2, 0, -relativePoint.x * 10);
    85. }
    86. velocityTarget = (GetComponent<Rigidbody>().rotation * Vector3.forward) * (Speed + MoveSpeed);
    87. } else {
    88. // axis control by input
    89. AddRot.eulerAngles = new Vector3 (pitch, yaw, -roll);
    90. mainRot *= AddRot;
    91.  
    92. if (SimpleControl) {
    93. Quaternion saveQ = mainRot;
    94.  
    95. Vector3 fixedAngles  = new Vector3 (mainRot.eulerAngles.x, mainRot.eulerAngles.y, mainRot.eulerAngles.z);
    96.  
    97. if(FixedX)
    98. fixedAngles.x = 1;
    99. if(FixedY)
    100. fixedAngles.y = 1;
    101. if(FixedZ)
    102. fixedAngles.z = 1;
    103.  
    104. saveQ.eulerAngles = fixedAngles;
    105.  
    106.  
    107. mainRot = Quaternion.Lerp (mainRot, saveQ, Time.fixedDeltaTime * 2);
    108. }
    109.  
    110.  
    111. GetComponent<Rigidbody>().rotation = Quaternion.Lerp (GetComponent<Rigidbody>().rotation, mainRot, Time.fixedDeltaTime * RotationSpeed);
    112. velocityTarget = (GetComponent<Rigidbody>().rotation * Vector3.forward) * (Speed + MoveSpeed);
    113.  
    114. }
    115. // add velocity to the riggidbody
    116. if(DirectVelocity){
    117. GetComponent<Rigidbody>().velocity = velocityTarget;
    118. }else{
    119. GetComponent<Rigidbody>().velocity = Vector3.Lerp (GetComponent<Rigidbody>().velocity, velocityTarget, Time.fixedDeltaTime * DampingVelocity);
    120. }
    121. yaw = Mathf.Lerp (yaw, 0, Time.deltaTime);
    122. MoveSpeed = Mathf.Lerp (MoveSpeed, Speed, Time.deltaTime);
    123. }
    124. /*/
    125.  
    126.  
    127. // Input function. ( roll and pitch)
    128. public void AxisControl (Vector2 axis)
    129. {
    130. if (SimpleControl) {
    131. //LimitAxisControl.y = LimitAxisControl.x;
    132. }
    133. //roll = Mathf.Lerp (roll, Mathf.Clamp (axis.x, -LimitAxisControl.x, LimitAxisControl.x) * SpeedRoll, Time.deltaTime);
    134. //pitch = Mathf.Lerp (pitch, Mathf.Clamp (axis.y, -LimitAxisControl.y, LimitAxisControl.y) * SpeedPitch, Time.deltaTime);
    135. }
    136.  
    137.  
    138. // Input function ( yaw)
    139. public void TurnControl (float turn)
    140. {
    141. //yaw += turn * Time.deltaTime * SpeedYaw;
    142. }
    143.  
    144.  
    145.  
    146. // Speed up
    147. public void SpeedUp (float delta)
    148. {
    149. //if(delta >= 0)
    150. //MoveSpeed = Mathf.Lerp (MoveSpeed, SpeedMax, Time.deltaTime * (10 * delta));
    151. }
    152.  
    153.  
    154. public void SpeedUp ()
    155. {
    156. //MoveSpeed = Mathf.Lerp (MoveSpeed, SpeedMax, Time.deltaTime * 10);
    157. }
    158.  
    159.  
    160.  
    161.  
    162.  
    163.  
    164.  
    165.  
    166.  
    167. // My functions
    168.  
    169. public void TurboSpeed (float delta)
    170. {
    171. MoveSpeed = Mathf.Lerp (MoveSpeed, SpeedMax * 2, Time.deltaTime * (10 * delta));
    172. }
    173.  
    174.  
    175.  
    176.  
    177.  
    178.  
    179. public void Straff (float direction){
    180. if (direction != 0) {
    181. SpeedRoll = 0;
    182. StraffSwitch = "on";
    183. euler.z -= direction * StraffRotateSpeed * Time.deltaTime;
    184. euler.z = Mathf.Clamp (euler.z, -zLimit, zLimit);
    185. euler.x = mainRot.eulerAngles.x;
    186. euler.y = mainRot.eulerAngles.y;
    187.  
    188. //transform.eulerAngles = euler;
    189. //transform.position += new Vector3 (3, 0, 0);
    190. transform.position += new Vector3(90 * Time.deltaTime , 0, 0);
    191. //transform.Translate(30 * Time.deltaTime, 0, 0);
    192. StraffDirection = direction > 0 ? "right" : "left";
    193.  
    194. } else {
    195.  
    196. StraffBack = StraffDirection == "right" ? -1.8f : 1.8f;
    197.  
    198. if(StraffSwitch == "on"){
    199.  
    200. euler.z -= StraffBack * StraffRotateSpeed * Time.deltaTime;
    201. if(StraffDirection == "right"){
    202. euler.z = Mathf.Clamp (euler.z, -zLimit, 0);
    203. }else{
    204. euler.z = Mathf.Clamp (euler.z, 0, zLimit);
    205. }
    206.  
    207. euler.x = mainRot.eulerAngles.x;
    208. euler.y = mainRot.eulerAngles.y;
    209. //transform.eulerAngles = euler;
    210. if(euler.z == 0){
    211. StraffSwitch = "off";
    212. SpeedRoll = 3;
    213. }
    214. }
    215. }
    216. //Debug.Log (euler.z);
    217. }
    218. }
    219.  
     
    Last edited: Apr 20, 2015
  5. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,647
    idk maybe its a camera issue and not an issue with the object moving.
    What you guys think after watching the video I posted?
     
  6. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,647
    Well the script I posted isn't the cause I completely disabled it and just did the following code on player input script and still does it.

    Ive also disabled every component in the game.

    Code (csharp):
    1.  
    2. transform.position += new Vector3(90 * Time.deltaTime , 0, 0);
    3.  
     
  7. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,647
    Ahh ok I narrowed it down to the camera causing the issue. Trying to figure out why now
    Idk why it took me so long to switch to scene view to see that there was nothing wrong with the ship moving
     
  8. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,647
    I really hate messing with cameras lol. which is why I bought the controller in the first place :p

    So some how the camera is not set to track smoothly when I move the ship on its x axis

    Code (csharp):
    1.  
    2.  void FixedUpdate ()
    3.  {
    4.  if (!Target)
    5.  return;
    6.  // rotation , moving along the player
    7.  
    8.  Quaternion lookAtRotation = Quaternion.LookRotation (Target.transform.position);
    9.  
    10. this.transform.LookAt (Target.transform.position + Target.transform.forward * Offset.x);
    11.  
    12.  positionTargetUp = Vector3.Lerp(positionTargetUp,(-Target.transform.forward + (Target.transform.up * Offset.y)),Time.fixedDeltaTime * TurnSpeedMult);
    13.  
    14.  Vector3 positionTarget = Target.transform.position + (positionTargetUp * Offset.z);
    15.  
    16.  float distance = Vector3.Distance (positionTarget, this.transform.position);
    17.  
    18.  this.transform.position = Vector3.Lerp (this.transform.position, positionTarget, Time.fixedDeltaTime * (distance  * FollowSpeedMult));
    19.  }
    20.  
    21.  
     
  9. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,647
    Only thing I got running now is this below and problem still exists
    I haven't updated to unity 5.0.1 yet maybe that will fix it.
    Just using First version of unity 5 right now

    Code (csharp):
    1.  
    2. void Update ()
    3. {
    4. transform.position += new Vector3(90 * Time.deltaTime , 0, 0);
    5.  
    6. }
    7.  
     
  10. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,647
    I fixed the issue!!!

    I don't know what made me decide to try this
    Instead of using Update() I changed it to FixedUpdate and the problem went away.
    This doesn't make any sense lol.

    And now my wings won't rotate while using Fixed Update.

    Code (csharp):
    1.  
    2. euler.z -= direction * StraffRotateSpeed * Time.deltaTime;
    3. euler.z = Mathf.Clamp (euler.z, -zLimit, zLimit);
    4. euler.x = mainRot.eulerAngles.x;
    5. euler.y = mainRot.eulerAngles.y;
    6.  
    7. transform.eulerAngles = euler;
    8. transform.position += new Vector3(90 * Time.deltaTime , 0, 0);
    9.  
    so apparently
    Code (csharp):
    1. transform.eulerAngles = euler;
    doesn't work on FixedUpdate


    The guys framework uses Update() for player controllers but uses FixedUpdate for flight controls as shown in the huge script in my past replies.
     
  11. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,647
    ya weird I can't rotate on the z axis while using FixedUpdate.

    This works when using Update()
    Code (csharp):
    1. transform.eulerAngles = euler;
    This some what works when using Update(). It rotates but has another ghost model shown with it.
    Code (csharp):
    1. transform.Rotate(Vector3.forward + euler, Space.World);
    Only works with Update()
    Code (csharp):
    1. transform.rotation *= new Quaternion(0 , 0, 90 * Time.deltaTime,0);
     
    Last edited: Apr 18, 2015
  12. Prisec

    Prisec

    Joined:
    Mar 12, 2020
    Posts:
    3
    You are literally using "* Time.deltaTime" which starts doing it by choosing the optimized time that passed since the last frame
     
  13. EmreKaya06

    EmreKaya06

    Joined:
    Mar 30, 2020
    Posts:
    1
    So how do I get the character to turn left or right when I press the key?
     
  14. chris140323

    chris140323

    Joined:
    Sep 27, 2022
    Posts:
    1
    thank you so much. spent a day trying to figure out why my character wasn't moving in the x-axis but it was in the y-axis, just changed the function to the fixedUpdate section and instantly started working