- Home /
Having trouble moving at right angles properly
Essentially I'm looking to make a game like ChuchuRocket if you are familiar with that seems simple enough for a newbie like me. Ultimately the first main step in this is to setup the "mouse" to run around the field automatically. However currently I'm having no success.
One of the main problems I am having is that I cannot seem to get the rotation/movement of the character clamped to the set movements, IE, 90 degree turns upon colliding with an object. Below is my current (heavily cut down) script for this, but I end up rotating excessively on my turn and wandering out in another direction rather than the hard 90 degree turn. Been doing a lot of reading on Quaternions but no love. :/
using UnityEngine;
using System.Collections;
public class minionMovement : MonoBehaviour
{
public float speed;
void OnCollisionEnter()
{
transform.rotation = Quaternion.Euler(0, 90, 0);
}
void Update ()
{
transform.Translate(Vector3.forward * Time.deltaTime * speed);
}
}
Well, I don't think the rotation should be an issue there (just looking at the code...). But there is one thing I have to say about it. Despite what you are trying to do:
1) if you are using a rigidbody, when the objects collide they actually affect each other (apply forces and change directions) - applying rotations different from the one you are trying to get.
2) If you are just using a collider, it should not change trajectory after detecting collision, but I think there is a better way of achieving this. The problem I think that can happen is that, when the objects collide, they don't necessarily stop really that inch before bumping into the other object; sometimes they get a little bit inside one another before realizing they collided (therefore applying the rotation more times than the ones you wanted - because they get into each other, one of them turns, but they keep touching).
Simple suggestion: RayCast. When your raycast hits, it turns (this way it will only check collisions in one direction - if it collides, turns and doesn't give a crap anymore for the object beside him, just pays attention to the ones in front of him);
Of course, before you try to change anything just comment out the code inside "OnCollisionEnter()" and write "Debug.Log("Trying to turn");". This way you will see if there is rotations applied to the object other than the ones in the code and, at the same time, you get to know if it just detects the amount of collisions you want
Have you tried using the transform.Rotate(); function ins$$anonymous$$d?
Answer by Purpose2 · Apr 26, 2014 at 07:00 PM
Solved the problem using LostInTime's suggestion, it is a bit intensive using raycasts (especially because I plan on having a fair number of these... but its working which will allow me to progress for the time being)
using UnityEngine;
using System.Collections;
public class minionMovement : MonoBehaviour
{
public float speed;
public float interactionDistance;
bool hitAnObject;
RaycastHit hit;
float yRotation;
void Update ()
{
transform.Translate(Vector3.forward * Time.deltaTime * speed);
Debug.DrawRay(transform.position, transform.forward * interactionDistance, Color.green);
if(Physics.Raycast (transform.position, transform.forward, out hit, interactionDistance))
{
hitAnObject = (hit.collider.tag == "Wall");
if(hitAnObject)
{
yRotation = yRotation + 90;
transform.rotation = Quaternion.Euler(0, yRotation, 0);
}
}
else
{
hitAnObject = !hitAnObject;
}
}
}
Ins$$anonymous$$d of doing transform.rotation, can't you just do transform.rotation.y = transform.rotation.y + 90; or transform.rotation = transform.rotation + (0, 90, 0); ?
Answer by WhiteWolf93 · Apr 10, 2014 at 01:17 AM
Instead of moving forward what you need is to transform the direction.
Something like this maybe solves it:
using UnityEngine;
using System.Collections;
public class minionMovement : MonoBehaviour
{
public float speed;
private Vector3 Direction;
void Start()
{
Direction = Vector3.forward;
}
void OnCollisionEnter()
{
transform.rotation = Quaternion.Euler(0, 90, 0);
}
void Update ()
{
Direction = transform.TransformDirection(Direction);
transform.Translate(Direction * Time.deltaTime * speed);
}
}
Sadly is just causing it to bizarrely vibrate... I feel I may be best served by giving what LostInTime suggests - will report back incase other people have similar issues.
Your answer
![](https://koobas.hobune.stream/wayback/20220613142346im_/https://answers.unity.com/themes/thub/images/avi.jpg)