so I have rigid body and when it collides with another body with low speeds it is working just fine but when it collides with hight speed it goes through the object I've Been have this problem for day and I can't fix it
here's my code
this is my player movement file
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharcterController : MonoBehaviour
{
// Start is called before the first frame update
public Vector3 PlayerMovementVar;
public Rigidbody Rigidbody_comp;
// Start is called before the first frame update
void Start()
{
Rigidbody_comp = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
PlayerMovement();
}
void PlayerMovement()
{
float horizontalAxis = Input.GetAxis("Horizontal")/30;
float verticalAxis = Input.GetAxis("Vertical")/30;
PlayerMovementVar = new Vector3(horizontalAxis,0f,verticalAxis);
transform.Translate(PlayerMovementVar,Space.Self);
}
}
I shouldn't be the one answering this since I have very little knowledge of unity but I'm pretty sure it might be because you are using transform.Translate which I think it avoids collisions try using a character controller instead :)
If you want to use properly Unity physics system you must use Forces instead of Translate direcly your gameobject.
Try this:
Rigidbody_comp.AddForce(PlayerMovementVar);
instead of
transform.Translate(PlayerMovementVar,Space.Self);
RigidBody movement and Transform movement are different from each other, each causes a different type of movement. when moving with a rigidbody, in order to use its physics ability, you should move it instead of the usual transform.Translate.
Rigidbody_comp.AddForce(PlayerMovementVar);
Related
i want to make the characters in my game collide with objects around them and stop moving. the thing is the characters just spawn and keep walking down the map until they're out of the Canvas view.
my characters have Constantmove script that i assigned to them which is this one:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterConstantMove : MonoBehaviour
{
GameManager Game_Manager;
void Start()
{
GameObject gameController = GameObject.FindGameObjectWithTag("GameController");
Game_Manager = gameController.GetComponent<GameManager>();
}
void Update()
{
transform.Translate(Game_Manager.moveVector * Game_Manager.moveSpeed * Time.deltaTime);
}
}
What do i do to make them stop around the desk and just stay idle when they collide there?
You are moving them with transform.Translate() function, which does not include physics. If you want them to collide, you would have to add Rigidbody2D component and move them using velocity property: https://docs.unity3d.com/ScriptReference/Rigidbody2D.html
I am trying to create a simple scriptable object for my shoot ability. I have that aspect working, but as I try to set my Transform to my player, it does not update the shoot position. I am very new to C#, and this script isnt complete. I still need to add the functionality to destroy the created objects. Any help would be greatly appreciated. I suspect I need to add an update function but im am not certain how to do this.
using UnityEngine.InputSystem;
using UnityEngine.AI;
using UnityEngine;
namespace EO.ARPGInput
{
[CreateAssetMenu]
public class Shoot : Ability
{
public Transform projectileSpawnPoint;
public GameObject projectilePrefab;
public float bulletSpeed = 10;
public float bulletLife = 3;
public override void Activate(GameObject parent)
{
var projectile = Instantiate(projectilePrefab, projectileSpawnPoint.position, projectileSpawnPoint.rotation);
projectile.GetComponent<Rigidbody>().velocity = projectileSpawnPoint.forward * bulletSpeed;
Destroy(projectile, bulletLife);
void OnCollisionEnter(Collision collision)
{
Destroy(projectile);
}
}
}
}
I'm still new to Unity and coding also, so take my advice with a load of salt :P.
It may be best to have a transform on your character (say just past the barrel of the player's gun) that you can put as the projectileSpawnPoint. In your code the projectileSpawnPoint is never set. Your first line of code in the "Activate" method should be something like:
public override void Activate(GameObject parent)
{
projectileSpawnPoint = playerGunBarrelTransform.transform.position;
var projectile = Instantiate(projectilePrefab, projectileSpawnPoint.position, projectileSpawnPoint.rotation);
projectile.GetComponent<Rigidbody>().velocity = projectileSpawnPoint.forward * bulletSpeed;
Destroy(projectile, bulletLife);
For destroying the projectile afterward you can keep it as you have it in OnCollision. howeer, with bullets in particular, since they tend to be instantiated A LOT and then destroyed afterward it would be best to use an object pooler for them to instantiate several of them on start and then disable and enable them as needed so you can resuse them instead of making new ones every time.
you have to create a new script that derives from Monobehaviour for your projectiles. attach that script to the projectile prefab and place the OnCollisionEnter method in that script. now your projectiles should get destroyed when touching another collider. make sure that there is a rigidbody component attached to the projectile.
I am currently working on the Flooded Grounds Unity Asset, I'm implementing an Alien NPC, with a script that tells it to follow the player.
The problem that I'm encountering is that the Alien turns his back to the player and I can't figure out why. I also tried rotating it by 180° on the Y axis (both in the transform and inside of the script using transform.rotate.y - when using the transform doesn't make any difference while using it in the update function just makes it constantly snap back and forth).
Here's a video of the issue:
https://drive.google.com/file/d/1tC9zJWKXXDuNZNZJbl2htBUgTPSTB4IG/view?usp=sharing
And here's the script that I'm using:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyFollow : MonoBehaviour
{
public NavMeshAgent enemy;
public Transform Player;
private Rigidbody alienRb;
private Animator alienAnim;
// Start is called before the first frame update
void Start()
{
alienRb = GetComponent<Rigidbody>();
alienAnim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
enemy.SetDestination(Player.position);
if(enemy.velocity.magnitude > 0)
{
alienAnim.SetTrigger("Walk");
}
}
}
Any help would be very appreciated!
Hello Mazza ^^ First of all, I have to say that your alien monster is really cute :D Anyway, did you try transform.LookAt(Player.transform.position)? Also, transform.rotation is might not be affective, maybe you can try alien.transform.Rotate(0.0f, 90.0f, 0.0f, Space.Self); or transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y - 180f, transform.rotation.z)
this is my first question on StackOverflow, please pardon and fix me if I ask something inappropriate.
I was creating a basic animated scene, where my GameObjects (Which are a couple of pre-animated animals) are running throughout the scene.
Here is the script:
using UnityEngine;
using System.Collections;
public class Locomotion : MonoBehaviour
{
void Update()
{
transform.Translate(Vector3.forward * Time.deltaTime);
}
}
Now my question is, how can I make them turn 180 degrees around when they hit a collider? Any modification to my existing code is welcomed
I mostly agree with the fine gentleman in that you should use a Rigidbody component to move your animals.
Something like this:
using UnityEngine;
using System.Collections;
public class Locomotion : MonoBehaviour
{
public RigidBody rb;
void FixedUpdate()
{
rb.velocity=Vector3.forward;
//or you could use rb.AddForce(Vector3.forward)
//but in simple cases such as this one it's not necessary
}
}
Be sure to call all physics calculations from FixedUpdate and not Update, because it you don't your objects can "jitter" in the scene (not going into detail on that).
About the collision detection and the 180:
To detect collisions you have to have a RigidBody component on your objects, as well as a Collider on your objects, as well as on the walls you want to collide with.
The way you check for collisions:
using UnityEngine;
public class Test : MonoBehaviour
{
public Rigidbody rb;
void FixedUpdate()
{
//this is the part that moves your animals,
//i use transform.forward instead of Vector3.forward because this way
//it will point in the forward direction of the animal, even if i rotate it
rb.velocity = transform.forward;
}
private void OnCollisionEnter(Collision collision)
{
//i tagged the walls i want to turn back the animals as "wall" in the unity editor
//and here i check if the thing i collided with is a wall
if (collision.gameObject.CompareTag("wall"))
{
//this line basicly says: look in the direction that is currently behind you
transform.rotation = Quaternion.LookRotation(transform.forward * -1);
}
}
}
I'm making a 2d game in Unity. I have a player sprite with this script attached. The right and left keys move perfect but when I try to get the space bar, it doesn't work
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
public class Player : MonoBehaviour {
public float moveSpeed;
public float jumpHeight;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Rigidbody2D playerBody = GetComponent<Rigidbody2D>();
if (Input.GetKeyDown("space")){
print("fired");
//playerBody.velocity = new Vector2(playerBody.velocity.x, jumpHeight);
}
if (Input.GetKey(KeyCode.LeftArrow))
{
playerBody.velocity = new Vector2(-moveSpeed, playerBody.velocity.y);
}
if (Input.GetKey(KeyCode.RightArrow))
{
playerBody.velocity = new Vector2(moveSpeed, playerBody.velocity.y);
}
}
}
I have also tried Keycode.Space instead of "space" which didn't work either
Also for some reason, I cannot remove or replace the unity3d tag but it's for a 2d unity game.
Your code appears to be all good, so it may be an error with your console window. Make sure in the top right corner of the console you have the comment box highlighted so anytime you print something in the code, it will appear. Also, I would suggest you use forces to make your character jump instead of velocity. In this case, you said the player's vertical velocity to jumpHeight, but never change it back so your character will constantly be moving upward. You should change that line to:
playerBody.AddForce (transform.up * jumpHeight);
and you may need to adjust the jumpHeight accordingly.