- Home /
Collisions causing Rigidbody to go Haywire and move randomly.
Hello. I've been having trouble with collisions and the rigidbody. Whenever I move to a slope or Jump multiple times, the player starts to rotate and move around randomly. I've tried to find a solution online for sometime and tried options such as using Rigidbody.Addforce , but they either do not function the way I need it to or will not work at all. I'm using Transform.translate for the player's movement. The movement code : Running.cs using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Audio;
public class Running : MonoBehaviour
{
public float spd = 60.0f;
public float Magnetude = 10f;
public float top_spd = 200f;
public float olSPD;
static float i = 0;
public float jumpSPD = 10.0f;
public float jumpHieght = 50.0f;
public float MaxHieght = 50.0f;
public Camera cam;
public Text Rings;
private int count;
public AudioSource Sound;
public AudioSource Master;
public AudioClip Ring_Collect;
public AudioClip Music;
private Rigidbody rb;
private void Start()
{
olSPD = spd;
i = 0;
var cam = Camera.main;
count = 0;
DisplayRings();
rb = GetComponent<Rigidbody>();
Master.clip = Music;
Master.Play();
}
void Run()
{
var x = Input.GetAxis("Horizontal") * Time.smoothDeltaTime * spd;
var z = Input.GetAxis("Vertical") * Time.smoothDeltaTime * spd;
var Player = GameObject.Find("PlayerObject");
var camAngle = cam.transform.rotation.y;
//print("CamAngle = " + camAngle);
if (Input.GetAxis("Vertical") < 0.3 & Input.GetAxis("Horizontal") > -0.3 & Input.GetAxis("Vertical") > -0.3 & Input.GetAxis("Horizontal") < 0.3)
{
i = 0;
}
if (spd <= olSPD + 30)
{
if (Input.GetAxis("Vertical") >= 0.3 & Input.GetAxis("Horizontal") >= 0.3)
{
this.transform.eulerAngles = Vector3.up * (cam.transform.eulerAngles.y + 90);
this.transform.RotateAround(this.transform.position, Vector3.up, ((camAngle) + 45));
transform.Translate(-z, 0, 0);
}
else if (Input.GetAxis("Vertical") >= 0.3 & Input.GetAxis("Horizontal") <= -0.3)
{
this.transform.eulerAngles = Vector3.up * (cam.transform.eulerAngles.y + 90);
this.transform.RotateAround(this.transform.position, Vector3.up, ((camAngle) - 45));
transform.Translate(-z, 0, 0);
}
else if (Input.GetAxis("Vertical") <= -0.3 & Input.GetAxis("Horizontal") <= -0.3)
{
this.transform.eulerAngles = Vector3.up * (cam.transform.eulerAngles.y + 90);
this.transform.RotateAround(this.transform.position, Vector3.up, ((camAngle) + 180 + 45));
transform.Translate(z, 0, 0);
}
else if (Input.GetAxis("Vertical") <= -0.3 & Input.GetAxis("Horizontal") >= 0.3)
{
this.transform.eulerAngles = Vector3.up * (cam.transform.eulerAngles.y + 90);
this.transform.RotateAround(this.transform.position, Vector3.up, ((camAngle) + 180 - 45));
transform.Translate(z, 0, 0);
}
else if (Input.GetAxis("Vertical") <= -0.3)
{
var dist = Vector3.Distance(this.transform.position, Player.transform.position);
if (i == 0)
{
this.transform.eulerAngles = Vector3.up * (cam.transform.eulerAngles.y + 90);
this.transform.eulerAngles = this.transform.eulerAngles + new Vector3(0, ((camAngle) + 180), 0);
i = 1;
}
print("1");
transform.Translate(z, 0, 0);
}
else if (Input.GetAxis("Vertical") >= 0.3)
{
this.transform.eulerAngles = Vector3.up * (cam.transform.eulerAngles.y + 90);
this.transform.Rotate(0, (camAngle), 0);
transform.Translate(-z, 0, 0);
}
else if (Input.GetAxis("Horizontal") <= -0.3)
{
this.transform.eulerAngles = Vector3.up * (cam.transform.eulerAngles.y + 90);
this.transform.Rotate(0, (camAngle) - 90, 0);
transform.Translate(x, 0, 0);
}
else if (Input.GetAxis("Horizontal") >= 0.3)
{
this.transform.eulerAngles = Vector3.up * (cam.transform.eulerAngles.y + 90);
this.transform.Rotate(0, (camAngle) + 90, 0);
transform.Translate(-x, 0, 0);
}
}
else if (spd >= olSPD + 30)
{
if (Input.GetAxis("Vertical") >= 0.3)
{
transform.Rotate(0, x, 0);
transform.Translate(-z, 0, 0);
}
else if (Input.GetAxis("Vertical") <= -0.3)
{
if (spd > olSPD + 10)
{
spd -= 90 * Time.deltaTime;
}
else
{
i = 0;
}
transform.Rotate(0, x, 0);
transform.Translate(spd * Time.deltaTime * 0.3f, 0, 0);
}
else if (Input.GetAxis("Horizontal") >= 0.3)
{
transform.Rotate(0, x, 0);
transform.Translate(-x, 0, 0);
}
else if (Input.GetAxis("Horizontal") <= -0.3)
{
transform.Rotate(0, x, 0);
transform.Translate(x, 0, 0);
}
if (Input.GetMouseButton(0))
{
float distFromMid = -((Screen.width / 2) - Input.mousePosition.x) / 100;
x = distFromMid * Time.smoothDeltaTime * spd;
if (Input.GetAxis("Vertical") >= 0.3)
{
transform.Rotate(0, x, 0);
transform.Translate(-z, 0, 0);
}
else if (Input.GetAxis("Vertical") <= -0.3)
{
if (spd > olSPD + 10)
{
spd -= 90 * Time.deltaTime;
}
else
{
i = 0;
}
transform.Rotate(0, x, 0);
transform.Translate(spd * Time.deltaTime * 0.3f, 0, 0);
}
}
}
else { i = 0; }
if (Input.GetAxis("Vertical") < 0.3 & Input.GetAxis("Horizontal") > -0.3 & Input.GetAxis("Vertical") > -0.3 & Input.GetAxis("Horizontal") < 0.3)
{
i = 0;
}
}
void Jump()
{
if(Input.GetButton("Fire1"))
{
var Jump = 0.0f;
if (Jump < MaxHieght)
{
Jump = Mathf.MoveTowards(Jump, MaxHieght, jumpSPD * Time.deltaTime);
}
this.transform.Translate(0, Jump, 0);
}
}
void ChangeWithTime()
{
if (Input.GetAxis("Vertical") > 0.3 | Input.GetAxis("Horizontal") < -0.3 | Input.GetAxis("Horizontal") > 0.3)
{
if (spd < top_spd)
{
spd = Mathf.MoveTowards(spd, top_spd, Magnetude * Time.deltaTime);
}
}
else if (spd > olSPD + 20)
{
spd = Mathf.MoveTowards(spd, olSPD, Magnetude * Time.deltaTime * 20);
transform.Translate(-spd* Time.deltaTime * 0.3f, 0, 0);
}
else if (spd > olSPD)
{
spd = Mathf.MoveTowards(spd, olSPD, Magnetude * Time.deltaTime * 8);
transform.Translate(-spd * Time.deltaTime * 0.3f, 0, 0);
}
else if (spd <= olSPD)
{
spd = olSPD;
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Collect"))
{
Sound.clip = Ring_Collect;
Sound.Play(0);
count += 1;
DisplayRings();
other.gameObject.SetActive(false);
}
}
void DisplayRings()
{
Rings.text = "Rings: " + count;
}
void Update()
{
Run();
ChangeWithTime();
Jump();
}
}
Thank you.
Answer by hexagonius · Jul 20, 2018 at 07:47 AM
You must use FixedUpdate for any changes to rigidbodied gameobjects
this did seem to do something, yet when I use Rigidbody.Adforce (which I think is meant to be the way to move a character properly) the character just topples over. Would there be something else I need to do in order for it to work properly?
Your answer
Follow this Question
Related Questions
Issues Making Character Move With Rigidbody 1 Answer
Stopping my player from moving when hitting a wall. 5 Answers
Player object falling through game environment, collision not detected with ground 0 Answers
Sphere get stuck in floor and fails to jump? 1 Answer
Player is Speeding up in collisions 0 Answers