Unity - move animated game object with rigidbody.velocity - c#

I have an animated character , and I need move it forward
I add rigibody to my character , and mover class
when I disable animated controller it moves
my mover class
using UnityEngine;
using System.Collections;
public class Mover : MonoBehaviour {
public float speed;
// Use this for initialization
void Start () {
GetComponent<Rigidbody>().velocity = transform.forward * speed;
}
}
any suggestion how I can approach that

I find out the answer
just uncheck Apply Root Motion

Might want to put it in Update() so its not just called one time.
void Update () {
GetComponent<Rigidbody>().velocity = transform.forward * speed * Time.smoothDeltaTime;
}

Related

Trying to transform position of gameobject unity using C#

I need some help in trying to change the position of an gameobject and its speed. I know to translate the gameObject but I don't know how to increase the speed of how it translates. I'm using the unity game engine by the way.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class lightning : MonoBehaviour
{
//This is lightning
private GameObject name;
// Start is called before the first frame update
void Start()
{
name=GameObject.Find("car");
}
// Update is called once per frame
void Update()
{
if (Input.Keydone("r")
door.transform.Translate(-Vector3.right * Time.deltaTime);
}
}
One workaround you can use is a black filter(preferably Image) on UI layer that covers the whole screen. You can change filter's opacity accordingly to achieve the feeling of 'dimmed' screen.
It sounds like you are having trouble with speed and acceleration.
I added a velocity variable and an acceleration variable with the two you can make objects go faster and faster with time.
Since this is Physics I'd recommend you to use a rigidbody on your gameobject
and manipulate its speed or to apply a force (force = mass * acceleration)
so by adding a force it will give the object an acceleration.
//This is lightning
private GameObject name;
private float speedFactor = 20;
private float accelearationFactor = 5;
// Start is called before the first frame update
void Start()
{
name=GameObject.Find("car");
}
// Update is called once per frame
void Update()
{
if (Input.Keydone("r") {
door.transform.Translate(-Vector3.right * speedFactor * Time.deltaTime);
speedFactor += accelearationFactor * Time.deltaTime;
}
}

I can’t find my ”rigidbody” tab on the right Unity

Yo ! I was programming on Unity and I wanted to modify something about the collision detection but when I click on any object but my player 1 on the scene I can’t find that tab. Did I mess up somewhere ? I put a video for more info.
Video : https://www.youtube.com/watch?v=SnmnpgYWKUA&t=1s
// The script that makes the camera follow my player :
using UnityEngine;
public class FollowPerso1 : MonoBehaviour {
public Transform perso1;
public Vector3 offset;
// Use this for initialization
// void Start ()
// Update is called once per frame
void Update () {
transform.position = perso1.position + offset;
}
}
// The script that makes the collision possibles :1
using UnityEngine;
public class Perso1Collision : MonoBehaviour {
public Perso1Movement Movement;
void OnCollisionEnter(Collision collisionInfo)
{
if (collisionInfo.collider.tag == “Obstacle”)
{
Movement.enabled = false;
}
}
}
// The script that makes the movements possible :
using UnityEngine;
public class Perso1Movement : MonoBehaviour {
public Rigidbody rb;
public float forwardForce = 2000f; // <– We declared a variable float to change out forwardForce (REVOIR)… E03
public float sidewaysForce = 500f; // REVOIR
// Use this for initialization
// void Start ()
// voidUpdate : Update is called once per frame (So the force ”speed” will depend on how many FPS your PC has)
void FixedUpdate () // FixedUpdate is better to calculate Physics in Unity (”makes stuff looks smoother when you collide with stuff”. Ref. Brackeys EP.2 HTMVGIU)
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime); // <– NOTE : Gotta understand Time.deltaTime better
if (Input.GetKey(“d”)) // <– QUESTION : Why not ”D” and ”d” instead ? :O
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey(“a”)) // <– QUESTION : Why not ”D” and ”d” instead ? :O
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange); // Understand ForceMode.VelocityChange better E06
}
}
}
Thanks!
Just add it? On the bottom of the inspector click on Add Component and either input RigidBody in the textfield on the top or search it under the physics category (should be the 1st entry)
Rigidbody is no default component, you have to intentionally add it to your GameObjects (infact only Transform is default, but the primitives like cubes ofc need a meshfilter and renderer to display, and conveniently also come with boxcollider "out of the box")
I hope it was that and I could help.
Edit:
If you just start out with unity I would suggest to you going over the official tutorials on the unity-website so you get a general idea of how everything works. They are quite well made.
Here is the Rigidbody tutorial for example: https://unity3d.com/de/learn/tutorials/topics/physics/rigidbodies?playlist=17120

Why does the z change when I move this cube? [c#][Unity]

I am really new to unity so I wanted to make a simple 2d project where you can move a cube. So I made a script to move the cube but when I play the game the Z changes along with the X so it will fall of the map.
Video:
https://www.youtube.com/watch?v=M9oHSc6dN2A&feature=youtu.be
The script I'm using:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
private Vector2 input;
public float movementSpeed = 50f;
private float horizontal;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
horizontal = Input.GetAxis ("Horizontal");
rigidbody.AddForce ((Vector2.right * movementSpeed) * horizontal);
}
}
I am using unity 4
Your rigidbody has Use Gravity checked. Romove that and it should function the way you want. [Wrong axis]
Edit:
A Rigidbody has a constraint property. Freeze z position there.

Movement Script In Unity C#

I have been trying to make a movement script for my player in a 2D game but without success. I do not know why it is not working.
The problem is that the player isn't moving. I have a RigidBody attached and gravity on. (Not sure if gravity makes such a difference but I just thought to mention it.)
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public Rigidbody rb;
public float speed = 10;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
float mx = Input.GetAxisRaw("Horizontal");
float mz = Input.GetAxisRaw("Vertical");
Vector3 movement = new Vector3(mx, 0.0f, mz);
Debug.Log(movement);
rb.AddForce(movement * speed * Time.deltaTime);
}
}
You may wanna make sure you are adding enough force to actually cause the player to move. Try increasing the force variable incrementally until you see a change. Hope this helps!

Unity2D - Companion

When reading this, keep in mind I'm new to both programming and Unity, so I might be missing some terms or tools Unity offer. Please elaborate your answers in an ELI5 manner. Thanks in advance!
I am currently working on some game-physics for a small personal project. Currently I've created a platform, a character and what should be, a following companion.
However, since I'm still not on a level, where I can create perfect code on own hand, I found an "enemy" script and tried to modify it a bit.
It Works to an extend, but it needs some tweaks which I hope I can help aquire with you guys.
This is how it looks now (the orange square is the companion)
It follows the player, and I can tweak the speed to fit as a companion, and not a player. However, as the Picture presents, the companion runs for the center of the player. What I want to create is a companion which follows the player, but still keeps a small gap from the player.
My first thoughts was to create some kind of permanent offset, but I fail to figure out how to do this without messing up the follow function.
I hope you can help me, it will be much appreciated!
Here's the code for reference.
Code attached to Player:
using UnityEngine;
using System.Collections;
public class PlayerCompanion : MonoBehaviour
{
//In the editor, add your wayPoint gameobject to the script.
public GameObject wayPoint;
//This is how often your waypoint's position will update to the player's position
private float timer = 0.5f;
void Update ()
{
if (timer > 0) {
timer -= Time.deltaTime;
}
if (timer <= 0) {
//The position of the waypoint will update to the player's position
UpdatePosition ();
timer = 0.5f;
}
}
void UpdatePosition ()
{
//The wayPoint's position will now be the player's current position.
wayPoint.transform.position = transform.position;
}
}
Code attached to companion:
using UnityEngine;
using System.Collections;
public class FollowerOffset : MonoBehaviour {
//You may consider adding a rigid body to the zombie for accurate physics simulation
private GameObject wayPoint;
private Vector3 wayPointPos;
//This will be the zombie's speed. Adjust as necessary.
private float speed = 10.0f;
void Start ()
{
//At the start of the game, the zombies will find the gameobject called wayPoint.
wayPoint = GameObject.Find("wayPoint");
}
void Update ()
{
wayPointPos = new Vector3(wayPoint.transform.position.x, transform.position.y, wayPoint.transform.position.z);
//Here, the zombie's will follow the waypoint.
transform.position = Vector3.MoveTowards(transform.position, wayPointPos, speed * Time.deltaTime);
}
}
bump, I guess ? :)
You can use smooth follow script. I have created a sample class for you. This class has features to follow any given gameobject with some delay and offset. You will have to tweak some values according to your need.
using UnityEngine;
using System.Collections;
public class PlayerCompanion : MonoBehaviour
{
[SerializeField]
private GameObject wayPoint;
[SerializeField]
public Vector3 offset;
public Vector3 targetPos;//Edit: I forgot to declare this on firt time
public float interpVelocity;
public float cameraLerpTime = .1f;
public float followStrength = 15f;
// Use this for initialization
void Start ()
{
//At the start of the game, the zombies will find the gameobject called wayPoint.
wayPoint = GameObject.Find("wayPoint");
offset = new Vector3 (5,0,0);//input amount of offset you need
}
void FixedUpdate () {
if (wayPoint) {
Vector3 posNoZ = transform.position;
Vector3 targetDirection = (wayPoint.transform.position - posNoZ);
interpVelocity = targetDirection.magnitude * followStrength;
targetPos = transform.position + (targetDirection.normalized * interpVelocity * Time.deltaTime);
transform.position = Vector3.Lerp (transform.position, targetPos + offset, cameraLerpTime);
}
}
}
Attach this class to your player companion, play with different values.
To preserve object orientation your companion schould not be anyways child of your main character.
Your wayPoint doesn't needs to be a GameObject but a Transform instead and your code will looks like better.
If your game is a 2D platform your and your companion needs to be backwards your player it probabli applys to just one axis (X?) so you can decrement your waiPoint in a more directly way by calculating it on your UpdatePosition function like this:
wayPoint.position = transform.position * (Vector3.left * distance);
where your "distance" could be a public float to easily setup.
so on your companion script Update just do:
transform.position = Vector3.MoveTowards(transform.position, wayPoint.position, speed * Time.deltaTime);
I can't test it right now so you could have problems with Vector3 multiply operations, just comment and I'll try to fix as possible ;)

Categories