Unity3D - Why my object doesnt move every frame per second - c#

Im struggling with very weird and propably simple problem,I have created a robot and his task is to move towards target I mean Im using Vector3.MoveTowards function in void update to move my robot per every frame but the problem is that he is moving only one time and stopping, he is making one step instead of for example 100. Im working with Unity3D. Here is source code;
public class Test01 : MonoBehaviour
{
public float speed, stopDist, rotationSpeed, moveSpeed, minSpeed, maxBackSpeed, maxFrontSpeed, turnSpeed, riseSpeed;
public Transform target;
private Rigidbody rb;
private float currentSpeed;
public bool isFinding = false;
private PlaySound signal;
void Start()
{
signal = GameObject.FindGameObjectWithTag("Signal").GetComponent<PlaySound>();
rb = GetComponent<Rigidbody>();
}
private void Update()
{
// If space key button is pressed the robot's isFinding bool is becaming true and robot is starting searching for target.
if (Input.GetKeyDown(KeyCode.Space))
{
isFinding = true;
if (Vector3.Distance(transform.position, target.position) > stopDist)
{
transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
signal.isAlarming = true;
signal.Sound();
}
else if (Vector3.Distance(transform.position, target.position) < stopDist)
{
isFinding = false;
signal.isAlarming = false;
SoundManager.instance.StopSound();
StopChasing();
}
}
}
public void StopChasing()
{
transform.position = this.transform.position;
}

Well currently you have everything nested under the GetKeyDown so it is executed only exactly once.
You could change that doing
if (Input.GetKeyDown(KeyCode.Space))
{
isFinding = true;
}
if(isFinding)
{
if (Vector3.Distance(transform.position, target.position) > stopDist)
{
transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
signal.isAlarming = true;
signal.Sound();
}
else
{
isFinding = false;
signal.isAlarming = false;
SoundManager.instance.StopSound();
StopChasing();
}
}
So pressing Space once activates the isFinding mode until it arrives at Vector3.Distance(transform.position, target.position) < stopDist

Related

Collision update is one physics update late, and it caused object to cancel its velocity

I have a character, he can move and jump. I need to check if it is grounded, so I made a trigger box collider as a characters component, and I use OnTriggerEnter and OnTriggerExit to check if it is grounded, but the exit of collision with object that the character is standing on is detected one physics update late, and when it is detected, the upward velocity appears to become 0, and the character starts falling. Here is my code:
using System.Collections;
using UnityEngine;
public class Player : MonoBehaviour
{
public float speed = 4.0f;
public float jumpSpeed = 8.0f;
private bool doJump = false;
private Rigidbody rb;
bool isGrounded = false;
private float x, z;
private void Awake()
{
rb = GetComponent<Rigidbody>();
}
private void Update()
{
x = Input.GetAxis("Horizontal");
z = Input.GetAxis("Vertical");
if (Input.GetKeyDown(KeyCode.Space))
{
StartCoroutine(Jump());
}
}
void FixedUpdate()
{
if (isGrounded)
{
//this is movement
rb.velocity = (transform.right * x) * speed * Time.fixedDeltaTime + (transform.forward * z) * speed * Time.fixedDeltaTime;
}
if (isGrounded && doJump)
{
doJump = false;
rb.AddForce(0, jumpSpeed, 0, ForceMode.VelocityChange);
}
}
IEnumerator Jump()
{
//need the coroutine to make the player jump even if space pressed a bit earlier than
//the character landed
doJump = true;
yield return new WaitForSeconds(0.15f);
doJump = false;
}
private void OnTriggerStay(Collider other)
{
if (other != this.gameObject)
{
isGrounded = true;
}
}
private void OnTriggerExit(Collider other)
{
if (other != this.gameObject)
{
isGrounded = false;
}
}
}
I tried to not use coroutine for jumping but it doesn't help. What helps is deleting the movement line.
I think you are trying to implement "Pre_Update" function, I have found this solution maybe you can use it:
https://answers.unity.com/questions/614343/how-to-implement-preupdate-function.html
I am not sure but I think if you initialize your physics component in the Start function instead of the Awake function then it might work.

How can I make the NPC move only vertical and horizontal and not diagonal with Vector2/3.MoveTowards and how can I make the NPC move only by tile

So I was trying to make a game just for fun and to learn for future use. So I encountered this problem in making the enemy NPC. I want it to follow me or chase me but I want the NPC to only move horizontal and vertical and I want the NPC to move per tile as well just like my Player.
Here's the video of how it looks
https://www.youtube.com/watch?v=CB_vdt1Z3nA
and here's the NPC script
public class ChaseScript : MonoBehaviour
{
public float speed;
private GameObject player;
private Transform player_transform;
void Start()
{
player = GameObject.Find("Player");
}
void Update()
{
player_transform = player.GetComponent<Transform>();
transform.position = Vector3.MoveTowards(transform.position, player_transform.position, speed * Time.deltaTime);
}
}
Here's my player controller
public void InputMove()
{
if (!isMoving)
{
input.x = Input.GetAxisRaw("Horizontal");
input.y = Input.GetAxisRaw("Vertical");
if (input.x != 0) input.y = 0;
if (input != Vector2.zero)
{
playerAnimation.SetParameterValue(animator);
var movePos = transform.position;
movePos.x += input.x;
movePos.y += input.y;
FacingForward.transform.position = movePos;
if (IsWalkable(movePos))
StartCoroutine(Move(movePos));
}
playerAnimation.SetParameterValueisMoving(animator);
}
if (Input.GetKey(KeyCode.LeftShift))
{
moveSpeed = 6f;
animator.speed = 1.5f;
}
else
{
moveSpeed = 4f;
animator.speed = 1f;
}
}
IEnumerator Move(Vector3 movePos)
{
isMoving = true;
while ((movePos - transform.position).sqrMagnitude > Mathf.Epsilon)
{
transform.position = Vector3.MoveTowards(transform.position, movePos, moveSpeed * Time.deltaTime);
yield return null;
}
transform.position = movePos;
isMoving = false;
}
private bool IsWalkable(Vector3 movePos)
{
if (Physics2D.OverlapCircle(movePos, 0.1f, SolidObjectLayer | NPC) != null)
{
return false;
}
return true;
}
What I did to my Player to move per tile is I just add 1 to transform so It'll be a constant movement but I don't know how to apply it on the NPC with the Vector3.MoveTowards but if it's not possible to do then it's fine
Check if this could work (you can adapt it to your 2D case)
using UnityEngine;
public class ChaseOrthoScript : MonoBehaviour
{
public float speed;
private GameObject player;
private Transform player_transform;
bool isMoving = false;
void Start()
{
player = GameObject.Find("Player");
player_transform = player.GetComponent<Transform>();
transform.LookAt(player_transform.position);
}
void Update()
{
if (transform.InverseTransformPoint(player_transform.position).z > 0) {
transform.position += transform.forward * speed * Time.deltaTime;
isMoving = true;
} else {
if (isMoving) {
float angle = Vector3.Angle(transform.forward, player_transform.position - transform.position);
transform.Rotate(Vector3.up, Mathf.Sign(angle) * 90);
isMoving = false;
} else if (transform.InverseTransformPoint(player_transform.position).z <= 0) { //player is back
transform.Rotate(Vector3.up, 180);
}
}
}
}
note that the player_transform = player.GetComponent<Transform>(); is moved to the Start(). Usually you dont want GetComponents in an update as you need to get it only once. ALso its much cleaner to have a public GameObject player; variable in the script and attach the reference in the editor that the player = GameObject.Find("Player");. Usually you dont want scene elements found by a hardcoded magic value in your code.
Hope that helps.

Unity: raycast groundcheck 2d doesn't work

So I want to check if there is ground under my character
here is the code
public class move2d : MonoBehaviour
{
public float moveSpeed = 7f;
public float distanceGround;
public bool isGrounded = false;
// Start is called before the first frame update
void Start()
{
distanceGround = GetComponent<Collider2D>().bounds.extents.y;
}
// Update is called once per frame
void Update()
{
if (Physics2D.Raycast(transform.position, -Vector2.up, distanceGround + 0.1f))
{
}
else
{
isGrounded = true;
Jump();
}
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
transform.position += movement * Time.deltaTime * moveSpeed;
}
void Jump()
{
if (Input.GetButtonDown("Jump") )
{
gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 8f), ForceMode2D.Impulse);
}
}
}
but it doesn't work and I don't understand why it never enter else statement even there my character is on ground.
When dealing with rigidbody you shouldn't use transform.position not for getting and not for setting values. Rather use Rigidbody2D.position for getting and Rigidbody2D.MovePosition in FixedUpdate for setting.
Also shouldn't it be the other way round? You most probably are grounded if the raycast hits something .. not if it doesn't?
// Already reference these via the Inspector
[SerializeField] private RigidBody2D rigidBody2D;
[SerializeField] private Collider2D _collider2D;
public float moveSpeed = 7f;
public float distanceGround;
private Vector2 movement;
private bool jumped;
private void Awake ()
{
// Alternately get it once on runtime
if(! rigidBody2D) rigidBody2D = GetComponent<RigidBody2D>();
if(!_collider2D) _collider2D = GetComponent<Collider2D>();
}
private void Start()
{
// Micro performance improvement by calculating this only once ;)
distanceGround = _collider2D.bounds.extents.y + 0.1f;
}
// Get user Input every frame
private void Update()
{
// Directly check for the button
if (Input.GetButtonDown("Jump"))
{
// if button presed set the jump flag
jumped = true;
}
// store user input
movement = Vector3.right * Input.GetAxis("Horizontal");
}
// Apply physics in the physics update
private void FixedUpdate()
{
// If jump flag is set
if(jumped)
{
// You are grounded when you hit something, not the other way round
if (Physics2D.Raycast(rigidBody2D.position, Vector2.down, distanceGround))
{
rigidbody2D.AddForce(Vector2.up * 8f, ForceMode2D.Impulse);
}
// reset the flag
jumped = false;
}
// apply the normal movement without breaking the physics
rigidBody2D.MovePosition(rigidBody2D.position + movement * Time.deltaTime * moveSpeed);
}

Unity 2D Jump script

So I was trying to implement double jumping in my game, which doesn't work. And now, somehow, not only can't my players double jump, they can't even jump either!
update: they can jump now, still can't double jump though.
This is my whole movement script:
using UnityEngine;
namespace Players
{
public class Actor : MonoBehaviour
{
//in order to control both players using 1 script.
public int playerIdx;
//Variables.
public float movementSpeed = 150f;
public float jumpForce = 250f;
//Ground stuff.
public LayerMask whatIsGround;
public bool grounded;
//boolean stuff.
private bool facingRight;
private bool moving;
//Needed to check if player is on the ground.
public Transform groundCheck;
//Limit player's movement speed.
public float maxMovementSpeed = 400f;
//Double jump stuff.
private bool doubleJumpReady;
//rb
private Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
doubleJumpReady = true;
rb = GetComponent<Rigidbody2D>();
facingRight = true;
}
// Update is called once per frame
void FixedUpdate()
{
SlowDown();
}
private void LateUpdate()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, 0.1f, whatIsGround);
if (grounded)
doubleJumpReady = true;
}
private void SlowDown()
{
if (moving) return;
//if player is not moving, slow them down.
if (rb.velocity.x > 0.2f)
rb.AddForce(movementSpeed * Time.deltaTime * -Vector2.right);
if (rb.velocity.x < -0.2f)
rb.AddForce(movementSpeed * Time.deltaTime * Vector2.right);
}
public void Move(int dir)
{
//Flip the player.
Flip(dir);
//Moving the player.
moving = true;
float xVel = rb.velocity.x; //Get x velocity.
if ( dir > 0)
rb.AddForce(movementSpeed * Time.deltaTime * Vector2.right * dir);
else if (dir < 0)
rb.AddForce(movementSpeed * Time.deltaTime * Vector2.right * dir);
else if (dir == 0) { } //do nothing.
//Help player turn around faster.
if (xVel > 0.2f && dir < 0)
rb.AddForce(movementSpeed * 3.2f * Time.deltaTime * -Vector2.right);
if (xVel < 0.2f && dir > 0)
rb.AddForce(movementSpeed * 3.2f * Time.deltaTime * Vector2.right);
}
private void Flip(int dir)
{
if (facingRight && dir == -1 || !facingRight && dir == 1)
{
facingRight = !facingRight;
transform.Rotate(0f, 180f, 0f);
}
}
protected void Jump()
{
if (grounded)
{
rb.AddForce(Vector2.up * jumpForce);
grounded = false;
doubleJumpReady = true;
}
else if (!grounded && doubleJumpReady)
{
rb.AddForce(Vector2.up * jumpForce);
doubleJumpReady = false;
}
}
}
}
I don't know if it is because of my jump script, or my player script:
void Update()
{
if (playerIdx == 1)
{
if (Input.GetKey(KeyCode.A))
Move(-1);
if (Input.GetKey(KeyCode.D))
Move(1);
if (Input.GetKey(KeyCode.W))
Jump();
}
if (playerIdx == 2)
{
if (Input.GetKey(KeyCode.LeftArrow))
Move(-1);
if (Input.GetKey(KeyCode.RightArrow))
Move(1);
if (Input.GetKey(KeyCode.UpArrow))
Jump();
}
}
So how can I fix this?
as far as i can see you never reset the
doubleJumpReady = false;
Variable. To fix this simply change the jump code to:
protected void Jump()
{
if (grounded)
{
rb.AddForce(Vector2.up * jumpForce);
grounded = false;
doubleJumpReady = true;
}
else if (!grounded && doubleJumpReady)
{
rb.AddForce(Vector2.up * jumpForce);
doubleJumpReady = false;
}
}
Hope it works ;).
EDIT:
grounded is set by overlapping spheres. Therefore no need to set it here.
Use this code and press your jump btn 2 times and see if the Debug.Log message shows up. Also, your player ID (idx is not needed.) As far as i can see your script is attached two to different objects. Therefore their variables are not shared anyways.
protected void Jump()
{
if (grounded)
{
rb.AddForce(Vector2.up * jumpForce);
doubleJumpReady = true;
}
else if (!grounded && doubleJumpReady)
{
rb.AddForce(Vector2.up * jumpForce);
doubleJumpReady = false;
Debug.Log("I am double jumping");
}
}
And the final problem is, you do not execute one of your jumps you execute both at once.
THis happens due to your execution.
Input.GetKey(KeyCode.UP)
instead use:
Input.GetKeyDown(KeyCode.Up);
GetKeyDown returns true when the button is pressed.
GetKey returns true WHILE the button is pressed.
Hope it works now ;)
I would implement it with a counter, you can set the number of jumps you want.
The code would be like this:
jumpCount = 0;
protected void Jump()
{
if(!grounded && jumpCount < 2)
{
jumpCount++;
rb.AddForce(Vector2.up * jumpForce);
}
if(grounded)
jumpCount = 0;
}
Going off the assumption that you can now perform the normal jump again after reading the comments. I think the reason you can't 'double jump' is that when you call the Jump() method, you don't just call it once, you call it twice, so what happens is the player jumps and then immediately double jumps and so you don't actually notice that the double jump has occurred. You could make it so that your doubleJumpReady boolean is only true after a set amount of time after you have jumped initially using some sort of co-routine or something I implemented for a sort of double jump mechanic once was that the user could press the jump button again to double jump only when the player had reached the maximum height of the initial jump or after.

transform.position teleporting. Not smoothly moving

Simple question but hard time finding an answer.
I have a hook mechanic where on the press of key.B, a hook will fire for 5 seconds then should come back.
This code is working fine, just that when the code allocated to recall the object, it does not comeback smoothly, it teleports.
Here is the code, the problem-specific line in BOLD
public class Hook : MonoBehaviour
{ //Remember Couroutine is pretty much update()
public Transform Target;
private float Thrust; // Int for motion
public Rigidbody rb;
public float HookTravelTime; //Define float for seconds
bool isHookActive = false; //Are we currently moving?
public float timeHookTraveling = 0f;
// Use this for initialization
void Start()
{
Thrust = 75f;
rb = GetComponent<Rigidbody>();
float walkspeed = Thrust * Time.deltaTime;
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.name == "mob")
{
Destroy(col.gameObject);
print("Other code negated");
rb.velocity = Vector3.zero;
transform.position = Vector3.MoveTowards(transform.position, Target.position, Thrust);
isHookActive = false;
timeHookTraveling = 0f;
}
}
void ThrowHook()
{
if (Input.GetKeyDown(KeyCode.B))
{
isHookActive = true;
rb.AddForce(Vector3.forward * Thrust);
}
if (isHookActive )
{
if (timeHookTraveling >= HookTravelTime) //if the hook traveled for more than hookTravelTime(5 seconds in your case)
{
print("hehemeth");
rb.velocity = Vector3.zero; //negate addforce from before
**HERE** transform.position = Vector3.MoveTowards(transform.position, Target.position, Thrust);
isHookActive = false;//reset this bool so your Update will not check this script until you don't activate it in your ThrowHook
timeHookTraveling = 0f;//reset the travel time for your next hook activation
}
else//if you havent hit 5 keep increasing
{
timeHookTraveling += Time.deltaTime;//increase your travel time by last frame's time
}
}
}
// Update is called once per frame
void Update()
{
ThrowHook();
}
}
What am I doing wrong? It should work as intended, right?
Vector3.MoveTowards is needed to be run everyframe, that's why I mentioned * Time.deltaTime in comment.
At 5s, rb's velocity becomes zero and isHookActive becomes false, and thus Vector3.MoveTowards is not called everyframe.
if (Input.GetKeyDown(KeyCode.B))
{
isHookActive = true;
rb.AddForce(Vector3.forward * Thrust);
}
if (isHookActive )
{
if (timeHookTraveling >= HookTravelTime) //if the hook traveled for more than hookTravelTime(5 seconds in your case)
{
print("hehemeth");
rb.velocity = Vector3.zero; //negate addforce from before
isHookActive = false;//reset this bool so your Update will not check this script until you don't activate it in your ThrowHook
timeHookTraveling = 0f;//reset the travel time for your next hook activation
}
else//if you havent hit 5 keep increasing
{
timeHookTraveling += Time.deltaTime;//increase your travel time by last frame's time
}
}
else if (!isHookActive && transform.position != Target.position)
{
transform.position = Vector3.MoveTowards(transform.position, Target.position, Thrust * Time.deltaTime);
}
And a even better way is putting
if (Input.GetKeyDown(KeyCode.B))
{
isHookActive = true;
rb.AddForce(Vector3.forward * Thrust);
}
into FixedUpdate() but not Update() while
if (isHookActive )
{... }
remains in Update().

Categories