I'm creating a simple side scroller with a couple people using Unity and am definitely a beginner. The character being used is 3D and runs forward well, but when running backwards he still faces forward. I have the controls setup in the InputManager so pressing A moves backwards and D moves forward but I'm not sure what to do so he faces his respective movement.
Any help would be greatly appreciated and if you need more information besides the following code I have for the movement let me know, it's based off another post I found.
var speed : float = 6.0;
var jumpSpeed : float = 6.0;
var gravity : float = 12.0;
//This variable is now inheriting the Vector3 info (X,Y,Z) and setting them to 0.
private var moveDirection : Vector3 = Vector3.zero;
function MoveAndJump() {
var controller : CharacterController = GetComponent(CharacterController);
if(controller.isGrounded) {
//moveDirection is inheriting Vector3 info. This now sets the X and Z coords to receive the input set to "Horizontal" and "Vertical"
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); //Allows player Input
moveDirection = transform.TransformDirection(moveDirection); //How to move
moveDirection *= speed; //How fast to move
if(Input.GetButtonDown("Jump")) {
animation.Play("Jump");
moveDirection.y = jumpSpeed;
}
}
//Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
//This moves the controller
controller.Move(moveDirection * Time.deltaTime);
if(Input.GetButton("Fire1")){
animation.Play("Attack");
}
if(Input.GetButton("Vertical")){
animation.Play("Run");
}
}
function Update() {
MoveAndJump();
}
For what it's worth another problem I'm having involves having two different animations being able to work at the same time, like run and attack. I figured I should mention while I'm here if anybody knew how to go about that. Thank you again for your time!
I ended up solving this based off a different code I stumbled upon, then called the function inside of Update():
var speed : float;
var jumpSpeed : float;
var gravity : float;
private var moveDirection : Vector3 = Vector3.zero;
function MoveJumpAttack() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, 0);
moveDirection *= speed;
if (moveDirection.sqrMagnitude > 0.01)
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (moveDirection), 90);
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
Related
So the problem is when I move the character(he is not humanoid) his walk animation is not aligned with the direction of his movement, for example, if I press W then he moves forward but his walk animation turns 90 degrees left, I have no idea where the problem might be. Here's the code I have:
public class PlayerControler : MonoBehaviour
{
public float MovSpeed = 3;
public float SmoothTime = 0.1f;
float TurnSmoothVelocity;
public float SpeedSmoothTime = 0.1f;
float SmoothVelocity;
float CurrentSpeed;
Animator animator;
void Start()
{
animator = GetComponent<Animator>();
}
void Update()
{
Vector2 Input = new Vector2(UnityEngine.Input.GetAxisRaw("Horizontal"), UnityEngine.Input.GetAxisRaw("Vertical"));
Vector2 InputDirection = Input.normalized;
if (InputDirection != Vector2.zero)
{
float TargetRotation = Mathf.Atan2(InputDirection.x, InputDirection.y) * Mathf.Rad2Deg;
transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y , TargetRotation, ref TurnSmoothVelocity, SmoothTime);
}
float targetSpeed = MovSpeed * InputDirection.magnitude;
CurrentSpeed = Mathf.SmoothDamp(CurrentSpeed, targetSpeed, ref SmoothVelocity, SmoothTime);
transform.Translate(transform.forward * CurrentSpeed * Time.deltaTime, Space.World);
float animationSpeedPercent = .5f * InputDirection.magnitude;
animator.SetFloat("SpeedPercent", animationSpeedPercent, SpeedSmoothTime, Time.deltaTime);
}
}
The Animated Object should be the Child of the Controller that has this Script assigned.
Just make an EmptyGameObject, add this script and parent your Animated Character under that Object.
If it's still not working correctly there might be something wrong with the Animation. The Script looks right to me.
I'm working on a little space simulation with processing. In the game you can zoom in & out of the solar system. To gove that a neat paralax effect, I want to zoom into the randomly renerated starsky in the background. So far I have everything working, but the starsky is zooming into the top left corner. I know I have to translate the origin point of the stars to (width/2,height/2), but I can't figure out how I do that.
Here is the code:
int starCount = 1200;
float[] xStar = new float[starCount];
float[] yStar = new float[starCount];
float starSpread;
float zoom;
void setup() {
size(1600, 900);
frameRate(30);
calcStars();
zoom = 1;
}
void draw() {
background(#000000);
for(int i=0;i<starCount;i++){
fill(#fff7e6);
noStroke();
ellipse(xStar[i]*starSpread,yStar[i]*starSpread,1,1);
}
starSpread = 1+zoom*0.001;
}
void calcStars(){
for(int i=0;i<starCount;i++){
xStar[i] = -random(0-width);
}
for(int i=0;i<starCount;i++){
yStar[i] = -random(0-height);
}
}
void mousePressed(){
zoom = zoom - 1;
}
(Click mouse to "zoom" out! <- I want the stars to move to the middle not to the upper left corner)
So, I have 2 arrays giving me 1200 random coordinates in the window. "zoom" is a simple float that's controllable with a slider. This variable controlls the spread of all the content. It's mutiplied by 0.001 to make the effect on my stars just slightly.
Now can somebody help me making the zoom happen as I intend it to?
Thanks in advance!
There are a bunch of ways to do this, but the basic approach is this:
Step 1: Calculate how far apart from the center of the screen each star is.
Step 2: Scale that distance.
Step 3: Draw each star that scaled distance away from the center point.
int starCount = 1200;
float[] xStar = new float[starCount];
float[] yStar = new float[starCount];
float zoom = 1;
void setup() {
size(1600, 900);
frameRate(30);
calcStars();
}
void draw() {
background(#000000);
for (int i=0; i<starCount; i++) {
fill(#fff7e6);
noStroke();
float centerX = width/2.0;
float centerY = height/2.0;
float xDistFromCenterX = centerX - xStar[i];
float yDistFromCenterY = centerY - yStar[i];
float scaledXDistFromCenterX = zoom * xDistFromCenterX;
float scaledYDistFromCenterY = zoom * yDistFromCenterY;
ellipse(centerX + scaledXDistFromCenterX, centerY + scaledYDistFromCenterY , 1, 1);
}
}
void calcStars() {
for (int i=0; i<starCount; i++) {
xStar[i] = -random(0-width);
}
for (int i=0; i<starCount; i++) {
yStar[i] = -random(0-height);
}
}
void mousePressed() {
zoom = zoom + .1;
}
This works, and I think it's pretty close to what you were going for, but you might also consider refactoring your code to use a Star object that keeps track of its own position. Whenever you want to zoom, just tell each Star object to move. You could also draw your starts to an image ahead of time, and then just scale that image. Like I said, there are a bunch of ways to do this.
So, I have built a simple car game, and have attached a script that allows it to move on both axes. I have created an animation, so that if the car turns upside down, there is an option to press the 'f' button and flip the car back to normal. Unfortunately, once the animation plays and the car flips back onto it's wheels, the car moves forwards and backwards, but doesn't rotate.
What could be the issue?
Here is the script:
var speed : float = 10.0;
var rotationSpeed : float = 100.0;
var CarFlip : Animator;
function Start () {
CarFlip.enabled = false;
}
function Update () {
var translation : float = Input.GetAxis ("Vertical") * speed;
var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate (0, 0, translation);
transform.Rotate (0, rotation, 0);
if(Input.GetKeyUp(KeyCode.F)){
CarFlip.enabled = true;
}
if(Input.GetKeyDown(KeyCode.B)){
speed = 30;
}
if(Input.GetKeyUp(KeyCode.B)){
speed = 15;
}
}
The Animator updates the transforms every frame so your change in Update() is being over-written.
If you want to override what it has done you need to apply your changes during LateUpdate().
I think the animator is still enabled making the rotation stuck. Maybe try something like this as a test to see if setting animator to false will get the car rotating again:
if(Input.GetKeyDown(KeyCode.F))
{
CarFlip.enabled = true;
}
if(Input.GetKeyUp(KeyCode.F))
{
CarFlip.enabled = false;
}
I am currently trying to rotate a positionVector (0,0,1) around the world's x-axis and then rotate it back to its original position (just trying to get it to work). I read into rotation matrixes and got it working (sorta) but i am pretty stuck now.
As the image and code shows i create a cube at the starting point (0,0,1) and rotate it down in this case 30 degrees. But it seems to rotate more than 30 degrees when rotating clockwise. However when i rotate it back counterclockwise (30 degrees) it does rotate the proper amount. Which results in it not ending up at its starting point as it should (0,0,1).
I was wondering if any of you could shed some light on why this is happening and how to fix it. Thank you guys in advance!
public float RotAngle = 330f;
public GameObject cube;
public GameObject original;
public GameObject relocator;
public GameObject initialTurn;
void Start ()
{
Vector3 pixelPos = new Vector3(0f, 0f, 1f);
original = GameObject.Instantiate(cube,pixelPos,Quaternion.identity) as GameObject;
original.name = "Original";
initialTurn = GameObject.Instantiate(cube, pixelPos, Quaternion.identity) as GameObject;
initialTurn.name = "InitialTurn";
relocator = GameObject.Instantiate(cube, pixelPos, Quaternion.identity) as GameObject;
relocator.name = "Relocator";
}
void Update()
{
initialTurn.transform.position = RotateAroundOrigin(original.transform.position, RotAngle*Mathf.Deg2Rad);
relocator.transform.position = RotateAroundOrigin(initialTurn.transform.position, (RotAngle * -1f) * Mathf.Deg2Rad);
}
Vector3 RotateAroundOrigin(Vector3 startPos,float angle)
{
startPos.Normalize();
startPos.y = (startPos.y * Mathf.Cos(angle)) - (startPos.z * Mathf.Sin(angle));
startPos.z = (startPos.y * Mathf.Sin(angle)) + (startPos.z * Mathf.Cos(angle));
return startPos.normalized;
}
You can rotate a direction vector pretty easily with a Quaternion.
Try this:
Vector3 RotateAroundOrigin(Vector3 startPos,float angle)
{
startPos.Normalize();
Quaternion rot = Quaternion.Euler(angle, 0.0f, 0.0f); // Rotate [angle] degrees about the x axis.
startPos = rot * startPos;
return startPos;
}
I am using the standard GUI of Unity3D.
How can I get the screen position of a GUI element?
Basically you can't. Using better words, as you already noticed GUI.Button simply returns a bool that indicated if the button has been pressed.
Since you are actually re-creating the button every frame (your GUI.Button code is inside a callback such as Update,FixedUpdate, OnGUI,...), and when you are calling GUI.Button you are passing it the Rect bounds by your self, there isn't actually any need of querying any object to retrieve the actual coordinates. Simply store them somewhere.
Rect buttonBounds = new Rect (50,60,100,20);
bool buttonPressed = GUI.Button (buttonBounds, "get postion");
if (buttonPressed)
{
//you know the bounds, because buttonBounds button has been pressed
}
Try this:
var positionGui : Vector2;
positionGui = Vector2 (guiElement.transform.position.x * Screen.width, guiElement.transform.position.y * Screen.height);
you can do something like this
public static Rect screenRect
(float tx,
float ty,
float tw,
float th)
{
float x1 = tx * Screen.width;
float y1 = ty * Screen.height;
float sw = tw * Screen.width;
float sh = th * Screen.height;
return new Rect(x1,y1,sw,sh);
}
public void OnGUI()
{
if (GUI.Button(screenRect(0.4f, 0.6f, 0.2f, 0.1f), "TRY AGAIN"))
{
Application.LoadLevel(0);
}
print ("button position + framesize"+screenRect(0.4f, 0.6f, 0.2f, 0.1f));
}