Sprite movement In Unity 5 - c#

I recently started messing with the Unity 5 Scripting API and I can't seem to make this piece of code work for my Simple Smiley Face Sprite1. All I want it to do is move slightly. I have tried multiple solutions. I believe it is not accepting the key board button or it is not finding the sprite. Thank you for the help in advance!
...Super simple code, I am just experimenting.
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKey ("q")) {
var Sprite1 = GameObject.Find("Sprite1");
Sprite1.transform.Translate(1,3,5);
}
}
}

try using KeyCode.Q:
if(Input.GetKeyDown(KeyCode.Q))
{
var Sprite1 = GameObject.Find("Sprite1");
Sprite1.transform.Translate(1,3,5);
}

if (Input.GetKeyDown(KeyCode.Space))
{
Vector3 forward = transform.TransformDirection (Vector3.forward) * 10;
}
you can simply add this code to your sprite and change the number 10 or any other the speed you want

Related

Making an object Teleport back to start upon reaching a certain point (Unity 2D)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour {
public Transform tf;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate(0, -0.10F, 0 * Time.deltaTime);
}
Hello, I am making a game in which the character gets hit by a car, I want the car to move down, then teleport back up in a loop, that way you need to avoid the cars. Thank you.
Your question is not very clear to me. But, if you just want the car to teleport then you can set the transform.position of the car to the position you want.

Make the CC Collider follow the camera (UNITY)

So I made a script that should In theory make the Character Controller's Collider follow the Player camera. Here is the Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
public class CCCameraFollower : MonoBehaviour
{
public GameObject Camera;
public CharacterController character;
// Start is called before the first frame update
void Start()
{
character = GetComponent<CharacterController>();
}
// Update is called once per frame
void LateUpdate()
{
character.center = Camera.transform.position;
}
}
This works Fine/Okay when I try it out, however as soon as I enter Climb() In my Climber script:
void Climb()
{
InputDevices.GetDeviceAtXRNode(climbingHand.controllerNode)
.TryGetFeatureValue(CommonUsages.deviceVelocity, out Vector3 velocity);
character.Move(transform.rotation * -velocity * Time.fixedDeltaTime);
cachedVelocity = -velocity;
Debug.Log(cachedVelocity);
}
When this is Climb() Runs, this happens:
Image that Shows The Issue
I don't see a reason for this to happen, maybe its very obvious. I don't know... Anyways, my question is: "How do I make the Collider of the CC Follow the Player Camera?".
In the CCCameraFollower.LateUpdate() method you are actually moving the center of the character controller and not the position of the Camera. Change to this and it will probably work as you want.
void LateUpdate()
{
Camera.transform.position = character.center;
}

How To Add Mobile Touch Controllers In Unity

I am working on a game, and till now, i have allowed a user to play the game by moving up and down using the up and down arrow keys on the computer keyboard.
But now i want to make the user be able to do that on a mobile phone by touching up or down, or maybe swiping up or down to move in the game.
I am using c# for this.
This is the current code assigned to the player object:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public float playerSpeed;
private Rigidbody2D rb;
private Vector2 playerDirection;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
float directionY = Input.GetAxisRaw("Vertical");
playerDirection = new Vector2(0, directionY).normalized;
}
void FixedUpdate()
{
rb.velocity = new Vector2(0, playerDirection.y * playerSpeed);
}
}
What do i need to do to achieve this?
You should try to make buttons with trasparency
You have plenty of possibilities here !
But there might have better solutions than others ;)
You could make a UI representing up and down arrow that respond with wathever behaviour you'd like when pressed ORyou could dig a little the Input documentation, espacially what's relative to touches.
From there you can get touch position in screen space an make whatever you want with it !
The choice is lead by the type of game or application you're trying to make. Hope that helped ;)

Adding force to objects that interact with a particle system C#

I currently have a particle system that is suppposed to represent a leafblower. I have got this working to proc on and off on mouse clicks.
The thing is, I want it to "blow" objects out of the way like a realistic leafblower. I have heard some people like to use AddForceAtPosition or something like that, it's just I do not know how to use it.
Currently I am enabling and disabling a box collider when my 'leafblower' is on and having everything the collider touch be knockedback but this proves to have various in game problems.
Here is my code I am working with:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class leafblower : MonoBehaviour {
private Rigidbody rb;
protected bool letPlay;
public ParticleSystem blow;
public Collider lcol;
// Use this for initialization
void Start ()
{
rb = GetComponent<Rigidbody>();
blow = GetComponent<ParticleSystem>();
lcol.enabled = !lcol.enabled;
}
void LeafBlower()
{
if (Input.GetKeyDown(KeyCode.Mouse1))
{
var isBlowing = blow.emission;
isBlowing.enabled = true;
lcol.enabled = !lcol.enabled;
}
else if (Input.GetKeyUp(KeyCode.Mouse1))
{
var isBlowing = blow.emission;
isBlowing.enabled = false;
lcol.enabled = !lcol.enabled;
}
}
// Update is called once per frame
void Update()
{
LeafBlower();
}
void FixedUpdate()
{
}
}
What force should I be adding or what should I put in a void OnCollisionEnter()?
Thank you so much :)
What you probably need is Particle System collision OnParticleCollision.
Please check Unity Scripting API here: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnParticleCollision.html

Unity C# - Rigidbody.AddForce not working as intended

So, I am trying to make a cube move with the WSAD buttons and for some reason, even though I am not touching any buttons, it moves diagonally up and to the left. I am using Unity 4.5.1
Code:
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
public float moveSpeed;
private Vector3 input;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
input = new Vector3(Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
print(input);
}
}
Your problem may be somewhere else, because the code seems just fine. If I were you, I'd try to take a look to this page. You might find the solution to this issue

Categories

Resources