I very recently began using unity, and while I have experience in programming, I don’t know what some of this coding does.
I got this programming by watching a top down shooter tutorial on Youtube. I understand some of the coding, but not all of it.
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (CharacterController))]
public class PlayerController : MonoBehaviour {
public float rotationSpeed = 260;
public float walkSpeed =10;
private Quaternion targetRotation;
private CharacterController controller;
// Use this for initialization
void Start () {
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"),0,Input.GetAxisRaw("Vertical"));
if (input != Vector3.zero) {
targetRotation = Quaternion.LookRotation (input);
transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y,targetRotation.eulerAngles.y,rotationSpeed * Time.deltaTime);
}
Vector3 motion = input;
motion *= (Mathf.Abs (input.x) == 1 && Mathf.Abs (input.z) == 1) ? .7f : 1;
motion *= walkSpeed;
motion += Vector3.up * -3;
controller.Move(motion * Time.deltaTime);
}
}
The plan I have is to control a tank from a top down perspecitve, so while this code makes the tank move in the direction that I press, it slides along the ground while it turns as opposed to turning then moving.
I would like to alter the code so that it includes a few if commands.
if the tank is not facing the right way, it will not move.
if the tank is facing close to the movement direction(45degrees) it will move at half speed
if the tank is facing the correct way, it will move at full speed.
if the tank is facing the absolute wrong way
If you could explain what some of the functions do or explain how to alter the code, that would be great.
If that is from unity tutorial it is explained (if not explained there it says refference to earlier tutorials to learn) there too much to explain here and all of it is already either on forum, answers, google or tutorials.
I understand the math portion of the coding, but not what some of the references made while coding… also I was dead asleep at the time of posting so I’m sorry for asking so vaguely. What I don’t understand is the following terminology, or how it works.“vector3” “eulerAngles” “Quaternion” And whatever is happening at the end of line 28.
vector3 is basic coordinate with 3 positions x,y,z, euler angles is like transform.position but for angles (transform.eulerAngles), quaternion is angle also but has another w component and you probably should not bother with it (atleast not yet), also best would be practice, look it up on wiki then do small test code to see in action.
also as i said, you can try modify this script little and see results but dont create procject with this, start from pong or some easy platformer, you wont need this kind of stuff for quite long time
This is a fairly basic character controller. To add your functionality you need to compare the facing of the tank (ie transform.forward) with the direction of input. The dot product is commonly used for this.
All of the Unity structs, like Vector3 and Quaternion can be looked up in the scripting reference.
Line 28 is using the ternary operator from C#. Its basically shorthand for an if-else statement. Written out in full it looks like this.
The basic purpose of this code is to slow down movement in the diagonal direction. I personally would do it like this. Much simpler and much easier to understand.
motion.Normalise();
To be honest you are better off starting this script from scratch. Its not hard to move an object around, and learning to do so will dramatically improve your ability to understand this code. The roll a ball tutorial is a great place to start. Character controllers are amongst the first things most people learn to code.