2D character not moving. Console shows no errors - c#

I couldn't find any mistakes in this code. if there aren't any mistakes in the code, please let me know what's wrong.
```
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
public float speed = 5;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector2 pos = transform.position;
pos.x += h * Time.deltaTime;
pos.y += v * Time.deltaTime;
transform.position = pos;
}
} // class
```

Try putting Horizontal and Vertical values in a Vector2 Then do transform.Translate(Movement * speed * Time.deltaTime); Instead of adding the position. Also you forgot to multiply the position by the speed. The Update should look something like this.
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector2 Movement = Vector2.zero;
Movement.y = v;
Movement.x = h;
transform.Translate(Movement * speed * Time.deltaTime);

I think your issue is that you forgot to include the speed multiplier in your code, so the correct code should be this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
public float speed = 5;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector2 pos = transform.position;
pos.x += h * speed * Time.deltaTime; //Multiply h by speed
pos.y += v * speed * Time.deltaTime; //Multiply v by speed
transform.position = pos;
}
} // class

Related

unity gameObject position always retrun to 0 and to center

hello everyone i started to learn unity game engine and c# and i follow a video course step by step but when i press the play button its immediatly center the game object and when i try to move its moving but always returning to center(position 0,0,0)
public class PlayerMovment : MonoBehaviour
{
public float speed = 5f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector2 pos = transform.position;
pos.x = h * speed * Time.deltaTime;
pos.y = v * speed * Time.deltaTime;
transform.position = pos;
}
}
You need to add your movement to the position, not overwriting it. Try this:
Vector2 pos = transform.position;
pos.x += h * speed * Time.deltaTime;
pos.y += v * speed * Time.deltaTime;
transform.position = pos;

Sprite not moving in unity 2D C#

I'm following a tutorial on making a game but when I wrote what they wrote it wouldn't move.
The script is attached to sprite but when I hit W, A, S or D it doesn't move.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movment : MonoBehaviour
{
// Start is called before the first frame update
public float speed = 5f;
void Start()
{
}
// Update is called once per frame
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector2 pos = transform.position;
pos.x += h * speed * Time.deltaTime;
pos.y += v * speed * Time.deltaTime;
}
}
Since Vector2 is a value type (a struct), Vector2 pos = transform.position will make a copy of the transform position and the original position remains unaffected by any changes you make to pos. To update the position, use transform.position = pos once pos is set to the new position:
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector2 pos = transform.position;
pos.x += h * speed * Time.deltaTime;
pos.y += v * speed * Time.deltaTime;
transform.position = pos;
}
See: What's the difference between struct and class in .NET?

Unity - Player movement problem, you can fly

If you dont collide with something you can fly! its really strange, please help me if you can.
i dont know how to fix it cause i am new in it.
Here is a script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_Movement : MonoBehaviour
{
public float speed = 4.0f;
public float gravity = -9.8f;
private CharacterController _charCont;
// Use this for initialization
void Start()
{
_charCont = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
float deltaX = Input.GetAxis("Horizontal") * speed;
float deltaZ = Input.GetAxis("Vertical") * speed;
Vector3 movement = new Vector3(deltaX, 0, deltaZ);
movement = Vector3.ClampMagnitude(movement, speed); //Limits the max speed of the player
// movement.y = gravity;
movement *= Time.deltaTime; //Ensures the speed the player moves does not change based on frame rate
movement = transform.TransformDirection(movement);
_charCont.Move(movement);
}
}
I don't know that CharacterController but it sounds like it takes the global movement vector.
After
movement = transform.TransformDirection(movement);
you should erase the Y component
movement.y = 0;

Unity. How to turn the player in the direction of movement

As you can see in this video, the object moves in any direction, but its model does not rotate in the direction of movement. How to fix it ??
Link to video
https://youtu.be/n4FDFDlsXK4
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveController : MonoBehaviour
{
private CharacterController controller = null;
private Animator animator = null;
private float speed = 5f;
void Start()
{
controller = gameObject.GetComponent<CharacterController>();
animator = gameObject.GetComponent<Animator>();
}
void Update()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = (transform.right * x) + (transform.forward * z);
controller.Move(move * speed * Time.deltaTime);
var angle = Mathf.Atan2(move.z, move.x) * Mathf.Rad2Deg;
if (x != 0 || z != 0) animator.SetTrigger("run");
if (x == 0 && z == 0) animator.SetTrigger("idle");
}
}
Don't use transform.forward and transform.right to make your move vector, just make it in world space. Then, you can set transform.forward to the move direction.
Also, as derHugo mentioned below in a comment, you should
avoid using exact equality to compare floats. Instead use Mathf.Approximately or use your own threshold like below
avoid setting triggers every frame. instead you can use a flag to determine if you're already idle or already running, and only set the trigger if you're not already doing the thing.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveController : MonoBehaviour
{
private CharacterController controller = null;
private Animator animator = null;
private float speed = 5f;
bool isIdle;
void Start()
{
controller = gameObject.GetComponent<CharacterController>();
animator = gameObject.GetComponent<Animator>();
isIdle = true;
}
void Update()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = new Vector3(x, 0f, z);
controller.Move(move * speed * Time.deltaTime);
var angle = Mathf.Atan2(move.z, move.x) * Mathf.Rad2Deg;
if (move.magnitude > idleThreshold)
{
transform.forward = move;
if (isIdle)
{
animator.SetTrigger("run");
isIdle = false;
}
}
else if (!isIdle)
{
animator.SetTrigger("idle");
isIdle = true;
}
}
}

Rotation Snapping Back To Zero

My code below rotates an object and moves them based on the angle of the joystick on the controller:
using UnityEngine;
using System.Collections;
public class PlayerControllerTEST : MonoBehaviour
{
public float moveSpeed;
public float h;
public float v;
public float moveX;
public float moveZ;
public Rigidbody rb;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
h = Input.GetAxis("Horizontal");
v = Input.GetAxis("Vertical");
moveX = (h * moveSpeed) * Time.deltaTime;
moveZ = (v * moveSpeed) * Time.deltaTime;
transform.Translate(moveX, 0f, moveZ, Space.World);
Quaternion rotate = Quaternion.Euler(0f, (Mathf.Atan2(h, v) * Mathf.Rad2Deg), 0f);
Debug.Log(rotate);
rb.MoveRotation(rotate);
}
}
However when I let go of the joystick, the object is snapping back to an angle of 0 degrees. I'm not sure how I would get it to keep the angle before the joystick is essentially let go.

Categories

Resources