I am having some trouble understanding the usage of the transform.Rotate(); method/function. I have coded the following to move around a game object and, I cannot understand how to code the rotation function using the arrow keys as, they are already being used by the code below. I want this game object (a tank in my game) to not only move vertically, horizontally, but, rotate 360 degrees (not only the tank but, the turret as well and, separately) as this would also simulate the real-life movement of the tank and, would help the player with other parts of the game I will be coding later. I did try to code the rotation following a Unity tutorial vid on this but, generate errors I did not make note of. Thank you for the help. =).
using UnityEngine;
using System.Collections;
public class TankGame : MonoBehaviour {
public float speedy;
private Rigidbody rgb;
First thing, learn tu use `` tags, please. Second, if you get errors, paste them in here. And last but not least, what is not working. But I really gotta see, it sounds like you just follow that tutorial and don’t get into learning what you are actually writing there. And thats a bad habit of trying to achieve some good code and knowledge of how to use the engine wisely. Please read the API, learn what your code is doing and write down, what you want to achieve and what logical way could help to reach that goal.[/CODE]
I don’t understand much of your response. I am 4 weeks into learning C#/Unity game design. I’m bound to get stuck understanding even the basics. My learning continues and, this forum/community is for helping those who are encountering issues with what they are learning (hence my presence here). If you are experienced with both C#/Unity, you can understand issues and, help me understand my issues. The code above is “standard” for simple continuous movement of game objects with the standard vectors. I tried making a game object rotate with the corresponding function/method and I wasn’t able to proceed because I am unfamiliar with how I can (if I can) code the rotation of the game object with arrow keys (on a keyboard) that are already being used with the code for the standard vertical/horizontal movement. My question then, is pretty self-explanatory; how do I code the rotation (360 degrees) for this game object within the script I have noted above? Thank you for your help, whatever it was. =)
using UnityEngine;
using System.Collections;
public class TankGame : MonoBehaviour {
public float speedy;
private Rigidbody rgb;
void Start () {
rgb = GetComponent<Rigidbody>();
}
void FixedUpdate () {
float horizMove = Input.GetAxis ("Horizontal");
float verticMove = Input.GetAxis ("Vertical");
Vector3 mve = new Vector3 (horizMove, 0.0f, verticMove);
rgb.AddForce (mve * speedy);
}
}
Next:
Try to make note of them. Run your code again, get the errors, come back with them. Don’t make us guess at it.
Also…
You say you’re trying to make code that rotates, but the supplied code does nothing of the sort. It calls AddForce, which is a linear force. AddTorque is a rotational force, did you mean to call that instead?
Or did you just not add the rotation code? You talk about the transform.Rotate method, did you mean to use that?
Do note… if you want to simulate physics sources with a Rigidbody, you should be avoiding the transform. Let the Rigidbody update the transform instead. One or the other… unless you know what you’re doing.
He was trying to say please use code tags. There is a pinned thread in the forums about it. How to insert code.
Then he was asking what’s wrong. But I think your question was asking how to rotate, but you didn’t have any code that tried rotation, maybe that’s what confused him - I’m not sure.
It is pretty good advice to post your errors here, even if what you tried didn’t work. Then, it’s something for anyone responding to work with.
Another poster here at the same time, will write more if you have updates/more questions later
Code tags acknowledged. I had no idea these exist, are required, preferred, the like. I found a tutorial that helped me understand how to code the rotation movement with the code I referenced above and, still code the vertical movement with the same code. It did require the transform.Rotate() method/function. All is well now, thank you all. =).
I’m back. I’m still having trouble with basic horizontal/vertical movement from key input. Below is my code (hopefully tagged correctly). This game object has a Rigidbody (obviously) attached. The following are the issues I’m having:
When object collides with other objects like cubes, it flips over and, trying to then move object with all 4 arrow keys results in object flying off the game view into oblivion causing me to stop play/re-enter play mode. I am also getting a different result when moving object onto a ramp-like object I designed in Blender (in Blender the colliders are applied/set). When it lands on game plane moving it with left/right keys no longer results in left/right movement, rather movement at an angle. I encountered this as of my last post stating this was fixed but, I only encountered it with the object colliding with other objects not without colliding with any so, I thought all was well. Is there something I’m missing in the editor/inspector (components/properties) or, is it my script, other?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cubeFunctions : MonoBehaviour {
public float spdy;
private Rigidbody rgb;
// Use this for initialization
void Start () {
rgb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
float horzMove = -Input.GetAxis("Horizontal");
float vertMove = -Input.GetAxis("Vertical");
Vector3 v3 = new Vector3 (horzMove, 0f, vertMove);
rgb.AddForce(v3 * spdy);
}
}
Not sure about the blender ramp thing, but you can freeze the rotation of your rigidbody in the inspector (or code) and that will prevent it from tipping over.
Thank you. This appears to work freezing all three axis. The only question that begs to be asked by me now is if this is “the right” way to code/edit this functionality? I mean, if I’m learning to design/code games, I don’t want to use “quick fixes” as opposed to the proper design/code. I do understand from my learning thus far however, that, there is a lot of combination/balance of code/editor usage. If I am coding this common movement from key input with the right edits in the inspector I’m happy. =)
There is code to do the same thing, if that’s what you’re asking. I think basically all that it means is “prevent the physics engine from rotating such and such axis”.
I think you’re right. Since the physics engine is supposed to simulate at least, close to life-like physics, if a game object collides with other objects, depending on force of collision it would tend to flip, rotate, etc., as in real-life. That must be why the freezing of all three axis constraints is available in the Inspector. Thank you for all your help. =).
I’m sorry for being such a pest. My last message/code was for a practice project to learn correctly all this standard horizontal/vertical/rotation functionality. The code below is for my tank game and, the issues I’m currently having are: 1). the tank is colliding right through 2 of the 4 walls that enclose the game scene (all 4 walls are set to kinematic, have default cube components) and flying off into game view oblivion. 2). the turret that’s supposed to rotate like a tanks does is not doing so in a horizontal/360 rotation while remaining flat on top of tank body (cube), rather rotating in what appears to be a Y/vertical axis even though both axis are at 0 values for this and Z. The only way I knew to form the tank as one object (remember, I’m new to this, lol) was to parent the cylinder (cockpit), capsule (turret) and, parent the cylinder to the cube (tank body). I may be even further confused about this as originally I had coded a Rigidbody reference for the tank body (cube) but, that didn’t work so I deleted it. However, the object still has a Rigidbody component attached. Lol. I hope I have provided enough congruent information. Lol. I thank you immensely for any help. =).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TankMove : MonoBehaviour {
public float speedstr;
public float turnSpeed;
void FixedUpdate () {
float hz = Input.GetAxis("Horizontal");
float vt = Input.GetAxis("Vertical");
transform.Rotate(0f, vt * -speedstr * turnSpeed * Time.deltaTime, 0f);
transform.Translate(hz * -speedstr * Time.deltaTime, 0f, 0f);
Code for turret functionality below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TurretMove : MonoBehaviour {
public float roteSpeed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.B)) {
transform.Rotate (1.0f * roteSpeed * Time.deltaTime, 0f, 0f);
}
}
}
try changing your rotation for the turret to the Y-axis.
transform.Translate is unreliable for physics interactions. You should move with force or with velocity to count on collisions for your walls. Moving the position directly (such as setting the position or using translate) can “move through” physics detection.
Thank you. Not long after my post I did start adhering to AddForce/AddTorque in my scripting of the tank as it has a Rigidbody component. I want you to know I’m trying to study/learn/apply all this stuff on my own too. It’s when I reach the frustrated point from remaining stuck that I decide to ask for help. Thank you for your consistent help. I will try this and, more. Hopefully I won’t be back for a while. =).