- Home /
AddForce forward also adding downward force
ALL IN C#
Sorry for terrible formatting, It's my first time posting.
I am trying to add a forward force as result of a slider but if I do, it for some reason it also adds a downward velocity. I have looked all over my script but I cannot find any reason why it would do so. My arrows which control rotation and altitude do nothing unexpected except for the upward which also does the same as the forward velocity. Anyone have an explanation?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class PlayerControllerJoystick : MonoBehaviour
{
public Vector3 value;
public Rigidbody rb;
public GameObject joystick;
public float thrust;
public float maxVelocity;
public float rotate;
public float ascent;
public float descent;
// Use this for initialization
private void Start ()
{
rb = GetComponent<Rigidbody> ();
value = joystick.GetComponent<VirtualJoystick> ().inputVector;
}
private void Update ()
{
Vector3 velocity = rb.velocity;
rb.velocity = Vector3.zero;
rb.velocity = -transform.up * velocity.magnitude;
rb.useGravity = false;
}
public void AddRightVelocity ()
{
gameObject.transform.Rotate (0, rotate, 0);
return;
}
public void AddLeftVelocity ()
{
gameObject.transform.Rotate (0, -rotate, 0);
return;
}
public void LoseAltitude ()
{
gameObject.transform.Translate (transform.up * descent);
return;
}
public void GainAltitude ()
{
gameObject.transform.Translate (transform.up * ascent);
return;
}
public void AddForwardVelocity ()
{
rb.AddRelativeForce(0,0,thrust);
return;
}
public void AddBackwardsVelocity ()
{
rb.AddForce (0,0, -thrust);
return;
}
}
Basically, my scripts that activate it just are onEnter variable "x" = true, on exit it is false, and when it is true Broadcast message. Example:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class GainAltitude : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
private bool Ascend = false;
private void Update ()
{
if (Ascend == true)
GameObject.Find ("Player").GetComponent<PlayerControllerJoystick> ().BroadcastMessage ("GainAltitude");
}
public void OnPointerDown (PointerEventData ped)
{
Ascend = true;
}
public void OnPointerUp (PointerEventData ped)
{
Ascend = false;
}
}
Your answer
Follow this Question
Related Questions
C# script not working 1 Answer
Make a player jump tile 1 Answer
Adding Force with a GUI button 5 Answers
AddForce with 2D Character 2 Answers
Multiple Cars not working 1 Answer